作者 CppLive | 发表于 2011-06-10
分章分类 : C++, 标准模板库
标签: C++, STL, 容器
1、erase——从容器中删除某个元素或者某个区间
#include <iostream>
#include <set>
using namespace std;
void print (set<int, less<int> >& s)
{
set<int, less<int> >::iterator It;
for ( It = s.begin(); It != s.end(); It++ )
cout << *It << " ";
cout << endl;
}
//--------------------------------------------
int main ()
{
int ary[] = {1,2,3,2,3,4,8,2,5,6};
set<int, less<int> > s;
s.insert(ary,ary+10);
print(s);
// erase '2'
s.erase(2);
print(s);
set<int, less<int> >::iterator It;
It = s.find(5);
// erase '5'
s.erase(It);
print(s);
It = s.find(4);
// erase from It to the end of set
s.erase(It,s.end());
print(s);
return 0;
}
运行结果:
// 1 2 3 4 5 6 8
// 1 3 4 5 6 8
// 1 3 4 6 8
// 1 3
阅读全文