原文地址:http://weimingtom.iteye.com/blog/965824
1-允许为二元仿函数或判断式绑定一个值,从而将那个值固定下来。
2-可以绑定第一个或者第二个参数[二元仿函数会变成一元仿函数]。
比如:
bind1st//通过绑定第一个参数,使二元的函数对象转化为一元的函数对象
bind2nd//通过绑定第二个参数,使二元的函数对象转化为一元的函数对象
not1//对一元的函数对象取反
not2//对二元的函数对象取反
统计大于等于5的元素个数?
cout<< count_if(lv.begin(), lv.end(),not1(bind2nd(less
查找!(x>= 1 && x<= 10)的元素位置?
std::list
std::not1(
compose2(std::logical_and
std::bind2nd(std::greater_equal
std::bind2nd(std::less_equal
查找!(x>= 1|| x<= 10)的元素位置?
std::list<int>::iterator in_range =std::find_if(L.begin(), L.end(),
compose2(sstd::not2(std::logical_or<bool>()),
std::bind2nd(std::greater_equal<int>(), 1),
std::bind2nd(std::less_equal<int>(), 10))));
由大到小排序?
sort(lv.begin(), lv.end(), not2(less
对每个元素v计算(v+2)*3
for_each(v.begin(),v.end(),compose1(
bind2st(multiplies
bind2st(plus
对每个元素v使用v*1.1*0.9【使用boost库函数】
std::transform(
values.begin(),
values.end(),
values.begin(),
boost::bind(
std::multiplies
std::multiplies
删除元素大于100并且小于1000的元素?
list
remove_if(L.begin(),L.end(),
compose2(logical_and
bind2nd(greater
bind2nd(less
L.erase(new_end, L.end());
统计大于5并且小于等于10的数的个数?【使用boost库函数】
int count=std::count_if(
ints.begin(),
ints.end(),
boost::bind(
std::logical_and
boost::bind(std::greater
boost::bind(std::less_equal
compose1//用于把两个一元函数f(x),g(x)组合成f(g(x))
compose2//用于把两个二元函数f(x),g(x)组合成f(g(x))
对angles中的每个元素v,计算-sin(v*pi / 180),并存入sines中
std::transform(angles.begin(), angles.end(), sines.begin(),
compose1(std::negate<double>(),
compose1(std::ptr_fun(sin),
std::bind2nd(std::multiplies<double>(), pi / 180.))));
计算每个元素x,计算sin(x)/(x +3)
double DBL_MIN = 3.0;
std::transform(L2.begin(), L2.end(), L2.begin(),
compose2(std::divides
std::ptr_fun(sin),
std::bind2nd(std::plus
logical_and
//条件操作,判断两个条件的与/或/非
条件:大于等于1并且小于等于10
compose2(std::logical_and
std::bind2nd(std::greater_equal
std::bind2nd(std::less_equal
查找等于’’或等于’\0’的字符?
constchar* wptr =
std::find_if(str, str + MAXLEN,
compose2(std::logical_or
std::bind2nd(std::equal_to
std::bind2nd(std::equal_to
//select1st
//select2nd
std::map
M[1] = 0.3;
M[47] = 0.8;
M[33] = 0.1;
// 输出1 33 47.
std::transform(M.begin(), M.end(),
std::ostream_iterator
__gnu_cxx::select1st
std::cout << std::endl;
// 输出0.3 0.1 0.8
std::transform(M.begin(), M.end(),
std::ostream_iterator
__gnu_cxx::select2nd
//project1st
std::vector
std::vector
std::vector
std::transform(v1.begin(), v1.end(), v2.begin(),result.begin(),
__gnu_cxx::project1st
assert(std::equal(v1.begin(),v1.end(), result.begin()));
//project2nd
std::vector
std::vector
std::vector
std::transform(v1.begin(),v1.end(), v2.begin(), result.begin(),
project2nd
assert(std::equal(v2.begin(),v2.end(), result.begin()));
//序列相加/相减/相乘/相除/取模
//V3 = V1 + V2
const int N = 1000;
std::vector
std::vector
std::vector
assert(V2.size() >= V1.size() && V3.size() >= V1.size());
std::transform(V1.begin(), V1.end(), V2.begin(), V3.begin(),std::plus
//V2 = -V1
每个元素取反?
const int N = 1000;
std::vector<int> V1(N);
std::vector<int> V2(N);
assert(V2.size() >= V1.size());
std::transform(V1.begin(), V1.end(), V2.begin(),std::negate<int>());
//条件操作,判断是否等于/不等于/小于/大于/小于等于/大于等于某个数
把0移到数组最左边的区间?
int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0};
const int N = sizeof(A)/sizeof(int);
std::partition(A, A + N, std::bind2nd(std::equal_to
std::copy(A, A + N, std::ostream_iterator
std::cout << std::endl;
查找不等于0的元素的位置(或是否存在)?
查找满足条件的位置
std::list
std::bind2nd(std::not_equal_to
assert(first_nonzero == L.end() || *first_nonzero != 0);