删除算法 2-remove_copy()

#include <iostream>
#include <algorithm>
#include <list>
#include <set>
#include <functional>
#include <iterator>

using namespace std;

int main()
{
	list<int> ilist;
	for (int i = 1; i <= 6; ++i)
		ilist.push_back(i);
	for (int i = 1; i <= 9; ++i)
		ilist.push_back(i);

	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); ++iter)
		cout << *iter << ' ';
	cout << endl; 
	cout << endl << endl;
	multiset<int> iset; // multiset可以自动排序,且可以存储相同的数据,
	remove_copy_if(ilist.begin(), ilist.end(),
		inserter(iset, iset.end()),
		bind2nd(less<int>(), 4)); // 删除小于4的数,bind2nd()是预定义函数适配器,less<type>() 是预定义的函数对象,
	for (multiset<int>::iterator iter =iset.begin(); iter != iset.end(); ++iter)
		cout << *iter << ' ';
	cout << endl; // 输出4 4 5 5 6 6 7 8 9
	remove_copy(ilist.begin(), ilist.end(), ostream_iterator<int>(cout, " "), 3);
	cout << endl; // 将ilist中所有的3删除再copy到ilist中,
	remove_copy_if(ilist.begin(), ilist.end(), ostream_iterator<int>(cout, " "), bind2nd(greater<int>(), 4));// 这里是将ilist中的大于4的数删除再copy到输出流cout中,
	cout << endl;


	return 0;
}

你可能感兴趣的:(删除算法 2-remove_copy())