bind1st 和 bind2nd 这两个捆绑函数。
这两个适配器函数和标准库函数对象类都是定义在functional头文件中的,其中,bind是捆绑的意思,1st和2nd分别是first和second的意思。
两者函数声明如下:
bind1st(const Operation& op, const T& x)
bind2nd(const Operation& op, const T& x)
看如下例子
class MyPrint
{
public:
void operator()(int v)
{
cout << v << " ";
}
};
class MyPrints:public binary_function<int,int,void>
{
public:
void operator()(int a,int b) const
{
cout << "a:" << a ;
cout << " b:" << b << endl;
}
};
void main()
{
vector<int> v{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
int addNum = 100;
for_each(v.begin(), v.end(), bind1st(MyPrints(), addNum));
//第一个大于10的数
auto it = find_if(v.begin(), v.end(), bind1st(less<int>(), 10));
cout << *it << endl;
//第一个大于10的数
auto it1 = find_if(v.begin(), v.end(), bind2nd(greater<int>(), 10));
cout << *it1 << endl;
system("pause");
}
结果:
从结果可以看出bind1st,绑定第一个参数,第二个参数是用户自己填写的。