标签:
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; }
标签:
原文地址:http://blog.csdn.net/taotaoah/article/details/52270473