bind用于绑定可调用 (Callable) 对象(函数对象、指向函数指针、到函数引用、指向成员函数指针或指向数据成员指针)和其参数。返回值为绑定成功后的函数对象
C++11中引入的function机制,其中绑定器主要有三种:bind1st、bind2nd、bind(C++11)

函数对象
首先说说函数对象,之所以说函数对象,是因为绑定器、function都涉及到该部分概念。函数对象实际上是类调用operator()()小括号运算符重载,实现像在“调用函数”一样的效果,因此还有个别名叫“仿函数”。函数对象示例代码如下:

class Print {
public:
    void operator()(string &s) { cout << s << endl; }
};
int main() {
    string s = "hello world!";
    Print print; //定义了一个函数对象print
    print(s);
    return 0;
}

上面代码print(s);语句,看似像函数调用,其实是类对象print调用其小括号运算符重载print.operator(string &s)。print就是一个函数对象,至此对函数对象就有了基本的认识
为什么需要绑定器?在使用STL时经常会遇到STL算法中需要传递某元函数对象,比如在写sort时,第三个参数决定了我们的排序规则,用来接收一个“比较器”函数对象,该函数对象是一个二元的匿名函数对象,形如greator()或者less()。二元函数对象的意思是,这个函数对象的小括号运算符重载函数接收两个参数,那么几元就表示接收几个参数。
我们知道系统自带的greater()和less()模板类对象是二元匿名函数对象,但是像泛型算法find_if第三个参数接收一元函数对象,所以需要通过绑定器将其转换为一元函数对象,可以通过bind1st和bind2nd去绑定,顾名思义,前者对二元函数对象的第一个参数进行绑定,后者对二元函数对象的第二个参数进行绑定,两个绑定器均返回一元函数对象

sort(vec.begin(), vec.end(), greater<int>()); //从大到小对vector进行排序
find\_if(vec.begin(), vec.end(), bind1st(greater<int>(), 70));
find\_if(vec.begin(), vec.end(), bind2nd(less<int>(), 70));

下面给出bind1st绑定过程图,二元函数对象绑定了第一个数为70,变为一元函数对象,传递给find_if泛型算法,此时find_if所实现的功能就是:找出有序降序数组中第一个小于70的数,所以find_if返回指向65元素的迭代器

<一>bind1st和bind2nd什么时候会用到

绑定器
C++ STL
bind1st 将operator()的第一个形参绑定成一个确定的值
bind2nd 将operator()的第二个形参绑定成一个确定的值

代码1

#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <ctime>
using namespace std;
template<typename Container>
void printerContainer(Container  & _container) {
	typename Container::iterator it_begin = _container.begin();
	typename Container::iterator it_end   = _container.end();
	while (it_begin != it_end) {
		cout << *it_begin <<" " ;
		++it_begin;
	}
}
int main() {
	vector < int>  vec;
	srand(time(nullptr));
	for (int i = 0; i < 20; i++) {
		vec.push_back((rand() % 100 + 1));		
	}
	printerContainer<vector < int>>(vec);
	vector< int>::iterator it_begin= vec.begin();
	vector< int>::iterator it_end  = vec.end();
	sort(it_begin, it_end);//默认小到大排序
	cout << endl;
	printerContainer<vector < int>>(vec);
	cout << endl;
	//greater二元函数对象
	sort(it_begin, it_end,greater<int>());//大到小排序
	printerContainer<vector < int>>(vec);
	cout << endl;
	//将70按顺序插入容器中,找到第一个小于70的元素
	//库里提供的less,greater都是二元的函数对象
	//greater a>b
	//less    a<b;
	//绑定器   + 二元函数对象 => 一元函数对象
	//bind1st: + greater bool operator()(70, const _Ty& _Right) 	
	//greater
	//constexpr bool operator()(const _Ty& _Left, const _Ty& _Right) const
	//{	// apply operator> to operands
	//	return (_Left > _Right);
	//}
	//bind2nd: + less bool operator()(const _Ty& _Left, 70) 
	vector<int>::iterator  it_findValue=find_if(it_begin, it_end, bind1st<greater<int>, int>(greater<int>(), 70));
	if (it_findValue != it_end) {
		vec.insert(it_findValue, 70);
	}
	printerContainer<vector < int>>(vec);
	cout << endl;
	system("pause");
	return 0;
}

绑定器 + 二元函数对象 => 一元函数对象

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。