1. set的删除操作
#include<iostream>
#include<set>
#include<iterator>
using namespace std;
typedef std::set<int> INT_SET;
int main()
{
INT_SET setMy;
for(int i=0;i<10;i++)
{
setMy.insert(i);
}
copy(setMy.begin(),setMy.end(),ostream_iterator<int>(cout,""));
cout<<endl;
int iValueToDelete=5;
INT_SET::iterator position;
for(position=setMy.begin();position!=setMy.end();)
{
if(*position==iValueToDelete)
{
//这里要注意,position++
setMy.erase(position++);
}
else
{
cout<<*position;
position++;
}
}
cout<<endl;
return 0;
}
2. map的删除操作
#include<iostream>
#include<map>
#include<iterator>
using namespace std;
typedef std::map<int,int> INT_MAP;
int main()
{
INT_MAP mapMy;
for(int i=0;i<10;i++)
{
mapMy.insert(INT_MAP::value_type(i,i*10+1));
}
INT_MAP::iterator position;
for(position=mapMy.begin();position!=mapMy.end();position++)
{
std::cout << "(" << position->first << "," << position->second <<")";
}
cout<<endl;
int iValueToDelete=5;
for(position=mapMy.begin();position!=mapMy.end();)
{
if(position->first==iValueToDelete)
{
//这里要注意,position++
mapMy.erase(position++);
}
else
{
cout<<"<"<<position->first<<":"<<position->second<<">";
position++;
}
}
cout<<endl;
return 0;
}