美文网首页程序员
STL bind1st bind2nd

STL bind1st bind2nd

作者: CodeZY | 来源:发表于2017-02-18 15:04 被阅读0次

头文件是<functional>

bind1st 和 bind2nd 是两个捆绑函数。

bind1st(const Operation& op, const T& x)就是这么一个操作:x op value

bind2nd(const Operation& op, const T& x)就是这么一个操作:value op x

其中value是被应用bind的对象。这两个配接器都用于将一个二元算子转换成一个一元算子。

一般绑定函数和STL greater<int >和less<int>一同应用。


#include<iostream>

#include<vector>

#include<algorithm>

#include<functional>

using namespace std;

int main()

{

vector<ivec;

for(int i=1;i<=15;++i)

{

ivec.push_back(i);

}

//查找其中大于5的元素的个数

//也就是是5<element 成立的元素的个数

int res = count_if(ivec.begin(),ivec.end(),bind1st(less(),5));

cout<<rec<<endl;//统计ivec中大于5元素的个数
//统计5>element成立的元素的个数

int res2 = count_if(ivec.begin(),ivec.end(),bind2nd(less(),5));

cout<<res2<<endl;  //统计ivec中小于5元素的个数

return 0;

}

相关文章

网友评论

    本文标题:STL bind1st bind2nd

    本文链接:https://www.haomeiwen.com/subject/qgklwttx.html