本文转自http://www.cnblogs.com/CUCmehp/archive/2009/04/28/1445691.html
不多说了直接贴代码吧.
class
Pred{
public :
bool operator ()( int a){
if (a >= 6 && a <= 7 )
return true ;
return false ;
}
};
void f(pair < int , int > p)
{
cout << p.first << " " ;
}
int main(){
int a[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
list < int > b = list < int > ();
b.assign(a,a + 10 );
vector < int > d;
d.assign(a,a + 10 );
map < int , int > c = map < int , int > ();
for (list < int > ::iterator it = b.begin();it != b.end();it ++ ){
c[ * it] =* it;
}
// 删除8,9
for (list < int > ::iterator it = b.begin();it != b.end();){
if ( * it >= 8 &&* it < 10 ){
it = b.erase(it);
} else {
it ++ ;
}
}
// 删除5,6,注意这种写法只适应于list,不适应于vector,因为vector没有remove_if方法
b.erase(remove_if(b.begin(),b.end(),Pred()),b.end());
// 删除3,4
for (vector < int > ::iterator it = d.begin();it != d.end();){
if ( * it >= 3 &&* it <= 4 ){
it = d.erase(it);
} else {
it ++ ;
}
}
ostream_iterator < int > os(cout, " " );
copy(b.begin(),b.end(),os);
cout << endl;
copy(d.begin(),d.end(),os);
cout << endl;
// 删除5,6
for (map < int , int > ::iterator it = c.begin();it != c.end();){
if (( * it).first >= 5 && ( * it).first <= 6 ){
c.erase(it ++ );
}
else {
it ++ ;
}
}
for_each(c.begin(),c.end(),f);
}
public :
bool operator ()( int a){
if (a >= 6 && a <= 7 )
return true ;
return false ;
}
};
void f(pair < int , int > p)
{
cout << p.first << " " ;
}
int main(){
int a[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };
list < int > b = list < int > ();
b.assign(a,a + 10 );
vector < int > d;
d.assign(a,a + 10 );
map < int , int > c = map < int , int > ();
for (list < int > ::iterator it = b.begin();it != b.end();it ++ ){
c[ * it] =* it;
}
// 删除8,9
for (list < int > ::iterator it = b.begin();it != b.end();){
if ( * it >= 8 &&* it < 10 ){
it = b.erase(it);
} else {
it ++ ;
}
}
// 删除5,6,注意这种写法只适应于list,不适应于vector,因为vector没有remove_if方法
b.erase(remove_if(b.begin(),b.end(),Pred()),b.end());
// 删除3,4
for (vector < int > ::iterator it = d.begin();it != d.end();){
if ( * it >= 3 &&* it <= 4 ){
it = d.erase(it);
} else {
it ++ ;
}
}
ostream_iterator < int > os(cout, " " );
copy(b.begin(),b.end(),os);
cout << endl;
copy(d.begin(),d.end(),os);
cout << endl;
// 删除5,6
for (map < int , int > ::iterator it = c.begin();it != c.end();){
if (( * it).first >= 5 && ( * it).first <= 6 ){
c.erase(it ++ );
}
else {
it ++ ;
}
}
for_each(c.begin(),c.end(),f);
}