您的位置:首页 > 其它

stl list.remove_if使用

2010-07-09 11:01 405 查看
#include <cstdlib>
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
using namespace std;
typedef struct _NODE
{
int id;
string str;

_NODE(int id, string str) : id(id), str(str){};

friend ostream& operator<<(ostream &os, const _NODE &nod)
{
return os << "(" << nod.id << ", " << nod.str << ")" << " ";
}
}NODE;
class check
{
public:
bool operator()(const NODE &nod)
{
return nod.id % 2;
}
};
template <class T>
void _printf(const T &begin, const T &end)
{
ostream_iterator<NODE> os(cout, " ");
copy(begin, end, os);
cout << endl;
}
int main(int argc, char *argv[])
{
list<NODE> lis;

for(int i=0; i<10; i++)
{
string str((i % 2 == 0) ? "even" : "odd");

lis.push_back(NODE(i, str));
}

cout << "original list is: " << endl;
_printf(lis.begin(), lis.end());

cout <<"after remove the odd elements: " << endl;
lis.remove_if(check());
_printf(lis.begin(), lis.end());

system("PAUSE");
return EXIT_SUCCESS;
}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息