我对std::erase
C++ 中的函数感到困惑。
以下代码在std::erase
调用前后获得相同的输出。但如果在执行后遍历列表,std::erase
则在输出中不会显示已擦除的值。帮我理解std::erase
。
#include<bits/stdc++.h>
using namespace std;
int main()
{
list<int> v;
v.push_back(12);
v.push_back(10);
v.push_back(20);
list<int>::iterator it;
it = v.begin();
printf("%u %d\n", it, *it);
v.erase(it);
printf("%u %d\n", it, *it);
for(it= v.begin(); it!= v.end(); it++)
cout<< *it<<" ";
return 0;
}
输出:
"Memory address" 12
"Memory Address" 12
10 20
erase
使您提供的迭代器无效(可能还有其他迭代器,具体取决于容器)。
之后使用和取消引用此迭代器是未定义的行为。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句