请教一个关于vector的有关问题,小弟我一使用vector就出错了`
请问一个关于vector的问题,我一使用vector就出错了```
我在程序里,大概有如下代码:
vector <int> a;
...
...
if (a.empty())
{
}
一调用a.empty这个方法就出错,这时看a的内容,发现里没有元素,并且first和end都是指向???,正常的情况下,应该是显示0个元素的``请问有没有人遇到过这种情况``我是用vs2005的```
------解决方案--------------------
empty
Syntax:
#include <vector>
bool empty() const;
The empty() function returns true if the vector has no elements, false otherwise.
For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) while loop to clear a vector and display its contents in reverse order:
vector <int> v;
for( int i = 0; i < 5; i++ ) {
v.push_back(i);
}
while( !v.empty() ) {
cout < < v.back() < < endl;
v.pop_back();
}
------解决方案--------------------
...
...内容是什么
------解决方案--------------------
啥叫“出错”呢。。。
------解决方案--------------------
把出错内容贴出来.
------解决方案--------------------
现在没环境试。
你不妨往vector里面先加一个元素再调用empty()看看会不会出错
------解决方案--------------------
根本是不同的一个错误....
------解决方案--------------------
楼主 你的程序还没有给你的类实体 分配内存,所以会出错阿~~ 修改如下 ":
#include <vector>
#include <iostream>
using namespace std;
class Test
{
public:
vector <int> mylist;
int getsize()
{
cout < < mylist.size() < <endl;
return mylist.size();
}
};
int main()
{
Test b;
Test *a = &b;
a-> getsize();
return 0;
}
这样就没有问题了~~~
------解决方案--------------------
没有初始化……
我在程序里,大概有如下代码:
vector <int> a;
...
...
if (a.empty())
{
}
一调用a.empty这个方法就出错,这时看a的内容,发现里没有元素,并且first和end都是指向???,正常的情况下,应该是显示0个元素的``请问有没有人遇到过这种情况``我是用vs2005的```
------解决方案--------------------
empty
Syntax:
#include <vector>
bool empty() const;
The empty() function returns true if the vector has no elements, false otherwise.
For example, the following code uses empty() as the stopping condition on a (C/C++ Keywords) while loop to clear a vector and display its contents in reverse order:
vector <int> v;
for( int i = 0; i < 5; i++ ) {
v.push_back(i);
}
while( !v.empty() ) {
cout < < v.back() < < endl;
v.pop_back();
}
------解决方案--------------------
...
...内容是什么
------解决方案--------------------
啥叫“出错”呢。。。
------解决方案--------------------
把出错内容贴出来.
------解决方案--------------------
现在没环境试。
你不妨往vector里面先加一个元素再调用empty()看看会不会出错
------解决方案--------------------
根本是不同的一个错误....
------解决方案--------------------
楼主 你的程序还没有给你的类实体 分配内存,所以会出错阿~~ 修改如下 ":
#include <vector>
#include <iostream>
using namespace std;
class Test
{
public:
vector <int> mylist;
int getsize()
{
cout < < mylist.size() < <endl;
return mylist.size();
}
};
int main()
{
Test b;
Test *a = &b;
a-> getsize();
return 0;
}
这样就没有问题了~~~
------解决方案--------------------
没有初始化……