求救!!!错在哪里?
#pragma warning(disable:4786)
#include <iostream>
#include <string>
#include <vector>
#include <deque>
using namespace std;
void main()
{
vector <string> vcs;
vcs.push_back( "this is A ");
vector <string > ::iterator it=vcs.begin();
int i=9;
for(;it!=vcs.end();++it)
{
cout < < "caplity of vector is : " < <vcs.size() < <endl;
cout < < "---> " < <*it < <endl; //去掉此句会有一个超过vector
//大小的循环,高手能解释一下为什么?
if(i==9)
{
vcs.push_back( "this is BBBBB ");
cout < < "vcs.push! " < <endl;
}
i=8;
}
}
上面的程序运行时发生错误;
但是把vector换成deque后,运行时就算没有错误了错误了,
[解决办法]
vcs.push_back( "this is BBBBB ");
这个之后 迭代器it已经失效了,你还使用自然会错误