码迷,mamicode.com
首页 > 编程语言 > 详细

STL_算法(22)_ STL_算法_替换算法

时间:2016-08-22 09:34:07      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:

replace(b, e, ov, nv)

replace_if(b, e, p, v)

// 一边复制一遍替换

replace_copy(b1, e1, b2, ov, nv)

replace_copy_if(b1, e1, b2, p, v) // 带有一个函数对象或者规则


#include<iostream>
#include<algorithm>
#include<list>
//
#include<functional>

using namespace std;

int main()
{
	list<int> ilist;
	for (int i = 2; i <= 7; i++)
		ilist.push_back(i);
	for (int i = 4; i <= 9; i++)
		ilist.push_back(i);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;


	replace(ilist.begin(), ilist.end(), 6, 42);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	replace_if(ilist.begin(), ilist.end(), bind2nd(less<int>(), 5), 0);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	//
	system("pause");
	return 0;
}
技术分享

预定义的函数对象

negate<type>() // 求反

plus<type>()

minus<type>()

multiplies<type>()

modulus<type>()

equal_to<type>()

not_equal_to<type>()

less<type>()

greater<type>()

less_equal<type>() // <= 

greater_equal<type>()

logical_not<type>()

logical_and<type>()

logical_or<type>()

预定义的函数适配器

bind1st(op, value)

bind1st(op, value)

not1(op)

not2(op)

mem_fun_ref(op)

mem_fun(op)

ptr_fun(op)

STL-算法 修改性算法:

for_each() generate()

copy() generate_n()

copy_backwards() replace()

transform() replace_if()

merge() replace_copy()

swap_ranges() replace_copy_if()

fill()

fill_n()

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

using namespace std;

int main()
{
	list<int> ilist;
	for (int i = 2; i <= 7; i++)
		ilist.push_back(i);
	for (int i = 4; i <= 9; i++)
		ilist.push_back(i);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;


	replace(ilist.begin(), ilist.end(), 6, 42);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	replace_if(ilist.begin(), ilist.end(), bind2nd(less<int>(), 5), 0);
	for (list<int>::iterator iter = ilist.begin(); iter != ilist.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	list<int> ilist2;
	for (int i = 2; i <= 6; i++)
		ilist2.push_back(i);
	for (int i = 4; i <= 9; i++)
		ilist2.push_back(i);
	for (list<int>::iterator iter = ilist2.begin(); iter != ilist2.end(); iter++)
		cout << *iter << ' ';
	cout << endl;

	replace_copy(ilist2.begin(), ilist2.end(), ostream_iterator<int>(cout, " "), 5, 55);
	cout << endl;

	replace_copy_if(ilist2.begin(), ilist2.end(), ostream_iterator<int>(cout, " "), bind2nd(less<int>(), 5), 42);
	cout << endl;

	replace_copy_if(ilist2.begin(), ilist2.end(), ostream_iterator<int>(cout, " "), bind2nd(modulus<int>(), 2), 0);

	//
	system("pause");
	return 0;
}
技术分享









STL_算法(22)_ STL_算法_替换算法

标签:

原文地址:http://blog.csdn.net/taotaoah/article/details/52270473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!