// replace_if.cpp -- 2011-10-03-19.08
#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using std ::vector ;
using std ::equal_to ;
template<class T>
class Print
{
public:
void operator () (const T & t) const
{
std ::cout << t << " " ;
}
} ;
int _tmain(int argc, _TCHAR* argv[])
{
int arr1[] = {1, 2, 3, 4, 5 ,6, 7, 8, 9} ;
vector<int> vec1(arr1, arr1 + sizeof arr1 / sizeof (int)) ;
// replace_if (beg, end, unaryPred, new_val) ;
// 操作前:[beg,end)标示输入序列.unaryPred是二元函数对象.new_val是用来代替指定值的值.
// 操作后:输入序列中所有使unaryPred返回true的元素被替换成new_val.
// 返回值:无.
// 备注: new_val无需同输入序列中元素的类型完全匹配,但必须能够转换成输入序列中元素的类型.
replace_if(vec1.begin(), vec1.end(), bind2nd(equal_to<int> (), 1), 255.111) ;
for_each(vec1.begin(), vec1.end(), Print<int> ()) ;
std ::cin.get() ;
return 0 ;
}
replace_if
最新推荐文章于 2022-10-26 08:30:00 发布