从容器中删除元素
我是这样编写的:程序代码:
#include <set> #include <iostream> #include <algorithm> using namespace std; int main() { set<int>S; for(int i = 0; i < 10; i++) { S.insert(i); } S.erase(std::remove(S.begin(), S.end(), 5), S.end()); set<int>::iterator it; for(it = S.begin(); it != S.end(); ++it) { cout << *it << endl; } return 0; }
但是编译的时候老是不通过,G++ 4.6.3的错误提示是:
程序代码:
In file included from /usr/include/c++/4.6/algorithm:63:0, from remove.cpp:3: /usr/include/c++/4.6/bits/stl_algo.h: 在函数‘_FIter std::remove(_FIter, _FIter, const _Tp&) [with _FIter = std::_Rb_tree_const_iterator<int>, _Tp = int]’中: remove.cpp:13:46:从此处实例化 /usr/include/c++/4.6/bits/stl_algo.h:1106:13: 错误: 向只读位置‘__result.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = int, std::_Rb_tree_const_iterator<_Tp>::reference = const int&]()’赋值
我的代码有什么问题?我找不出错误啊!