vector迭代器失效
vector 删除其中一个迭代器后,因后面数据移动,导致迭代器全部失效。有如下情况,一个迭代器指向其尾部元素,然后像后面添加元素(假定此时capacity>size),按理说迭代器不该失效,毕竟内存没有改变,但是VS17依然失效,求解释?
vector<int> vec; vec.push_back(1); vec.push_back(2); vec.push_back(3); vec.push_back(4); vec.push_back(5); vec.push_back(6); vec.push_back(7); cout << vec.size() << endl; //7 cout << vec.capacity() << endl; // 9 auto ite = vec.end(); cout << &vec[0] << endl; vec.push_back(9); cout << &vec[0] << endl; // same address auto it = ite; //it--; //invalid