文章目录
Vector迭代器增、删、改导致的literator失效问题
增
capacity不足
当vector插入数据后,超过容量的情况
- 例子
//capacity()不足,在末尾添加元素
void showInfo(vector<int> &v) {
cout << "Capacity:" << v.capacity() << endl;
cout << "Size:" << v.size() << endl;
cout << "vector:(value,pos)"
<<"\t";
for (auto& a : v) {
cout <<"("<< a <<"," << &a << ")"
<< "\t";
}
cout << endl
<< "iterator:(value,pos)"
<< "\t";
cout << "(" << *i1 << "," << &(*i1) << ")"
<< "\t";
cout << "(" << *i2 << "," << &(*i2) << ")"
<< "\t";
cout << "(" << *i3 << "," << &(*i3) << ")" //解引用后迭代器是未定义的
<< "\t" << endl;
cout << endl;
}
void test() {
vector<int> v = { 1,2};
i1=v.begin();
i2=++v.begin();
i3=v.end();
cout << "尾部插入前&