list4
来源:互联网 发布:软件改变生活 编辑:程序博客网 时间:2024/06/07 06:48
#include <iostream>
#include <list>
bool single_digit ( int value) { return (value<10); }
// a predicate implemented as a class:
struct is_odd {
bool operator() ( int value) { return (value%2)==1; }
};
int main ()
{
int myints[]= {17,89,7,89,89};
std::list<int> mylist (myints,myints+4);
mylist.remove(89);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
return 0;
}
#include <list>
bool single_digit ( int value) { return (value<10); }
// a predicate implemented as a class:
struct is_odd {
bool operator() ( int value) { return (value%2)==1; }
};
int main ()
{
int myints[]= {17,89,7,89,89};
std::list<int> mylist (myints,myints+4);
mylist.remove(89);
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
int myints[]= {15,36,7,17,20,39,4,1};
std::list<int> mylist (myints,myints+8); // 15 36 7 17 20 39 4 1
mylist.remove_if (single_digit); // 15 36 17 20 39
mylist.remove_if (is_odd()); // 36 20
std::cout << "mylist contains:";
for (std::list<int>::iterator it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
0 0