I have pushed 1 element in vector and popped it in this program:
`
#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;
}
Even after popping the element, iterator is outputting 9. Why is it still pointing to the deleted value? Doesn't this behavior make iterators untrustworthy?
Is it because the iterator might be pointing to the memory which was previously the part of the vector? Shouldn't the iterator sync it with the changed size?
g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4)