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#include #include // #include
using namespace std; int main() { list 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 ::iterator iter = ilist.begin(); iter != ilist.end(); iter++) cout << *iter << ' '; cout << endl; replace(ilist.begin(), ilist.end(), 6, 42); for (list ::iterator iter = ilist.begin(); iter != ilist.end(); iter++) cout << *iter << ' '; cout << endl; replace_if(ilist.begin(), ilist.end(), bind2nd(less (), 5), 0); for (list ::iterator iter = ilist.begin(); iter != ilist.end(); iter++) cout << *iter << ' '; cout << endl; // system("pause"); return 0; }
预定义的函数对象
negate
plus
minus
multiplies
modulus
equal_to
not_equal_to
less
greater
less_equal
greater_equal
logical_not
logical_and
logical_or
预定义的函数适配器
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#include #include // #include
#include using namespace std; int main() { list 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 ::iterator iter = ilist.begin(); iter != ilist.end(); iter++) cout << *iter << ' '; cout << endl; replace(ilist.begin(), ilist.end(), 6, 42); for (list ::iterator iter = ilist.begin(); iter != ilist.end(); iter++) cout << *iter << ' '; cout << endl; replace_if(ilist.begin(), ilist.end(), bind2nd(less (), 5), 0); for (list ::iterator iter = ilist.begin(); iter != ilist.end(); iter++) cout << *iter << ' '; cout << endl; list 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 ::iterator iter = ilist2.begin(); iter != ilist2.end(); iter++) cout << *iter << ' '; cout << endl; replace_copy(ilist2.begin(), ilist2.end(), ostream_iterator (cout, " "), 5, 55); cout << endl; replace_copy_if(ilist2.begin(), ilist2.end(), ostream_iterator (cout, " "), bind2nd(less (), 5), 42); cout << endl; replace_copy_if(ilist2.begin(), ilist2.end(), ostream_iterator (cout, " "), bind2nd(modulus (), 2), 0); // system("pause"); return 0; }