我在向量中推送了1个元素并在此程序中弹出:
`
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<int> ivec;
ivec.push_back(9);
vector<int>::iterator l=ivec.end();
cout<<"Size before popping = "<<ivec.size()<<"|| last element before deletion is = "<<*--l<<endl<<endl;
ivec.pop_back();
cout<<"size after popping = "<<ivec.size()<<"|| after deletion, last element is = "<<*l<<endl;
return 0;
}
即使在弹出元素之后,迭代器也会输出9.为什么它仍然指向已删除的值?这种行为不会使迭代器不值得信任吗?
是因为迭代器可能指向以前是向量部分的内存吗?迭代器不应该与更改后的大小同步吗?
g ++(GCC)4.8.5 20150623(Red Hat 4.8.5-4)