【发布时间】:2013-12-21 14:22:08
【问题描述】:
我刚刚写了一些基本的代码,它会推入一些值,通过使用指向它的迭代器删除一个值(擦除)。该集合不包含该值,但迭代器仍指向已删除的值。
这不是违反直觉吗?为什么会这样?
// erasing from set
#include <iostream>
#include <set>
int main ()
{
std::set<int> myset;
std::set<int>::iterator it;
// insert some values:
for (int i=1; i<10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90
it = myset.begin();
++it; // "it" points now to 20
myset.erase (it);
std::cout << *it << std::endl; // still prints 20
std::cout << "myset contains:";
for (it=myset.begin(); it!=myset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
【问题讨论】: