详解C++11中绑定器bind的原理与使用

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

详解C++11中绑定器bind的原理与使用

Hello_Bugs   2022-12-09 我要评论

bind1st和bind2nd什么时候会用到

bind用于绑定可调用 (Callable) 对象(函数对象、指向函数指针、到函数引用、指向成员函数指针或指向数据成员指针)和其参数。返回值为绑定成功后的函数对象

C++11中引入的function机制,其中绑定器主要有三种:bind1st、bind2nd、bind(C++11)

函数对象

尽管函数指针被广泛用于实现函数回调,但C++还提供了一个重要的实现回调函数的方法,那就是函数对象。函数对象(也称“函数符”)是重载了“()”操作符的普通类对象。因此从语法上讲,函数对象与普通的函数行为类似。
用函数对象代替函数指针有几个优点:

首先,因为对象可以在内部修改而不用改动外部接口,因此设计更灵活,更富有弹性。函数对象也具备有存储先前调用结果的数据成员。在使用普通函数时需要将先前调用的结果存储在全程或者本地静态变量中,但是全程或者本地静态变量有某些我们不愿意看到的缺陷。

其次,在函数对象中编译器能实现内联调用,从而更进一步增强了性能。这在函数指针中几乎是不可能实现的。

C++11还提供了limbda表达式来实现函数的灵活调用。详见《C++ Primer Plus》第18章

函数对象实际上是类调用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元素的迭代器

绑定器

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;
}

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

bind1st和bind2nd的底层实现原理

自己实现绑定器,代码如下

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <ctime>
using namespace std;

template<typename Container>
void printContainter(Container _container) {
	typename Container::iterator  it_begin = _container.begin();
	typename Container::iterator  it_end = _container.end();

	for (; it_begin != it_end; ++it_begin) {
		cout << *it_begin << " ";
	}
	cout << endl;
}


//将二元函数对象分装,返回一元函数对象
template<typename Pr, typename T>
class MyBindList2 {

public:
	MyBindList2<Pr,T>(Pr _pr, T _val):_mPr(_pr),_mVal(_val) {}
	bool operator()(T _paremeter) {
		return _mPr(_mVal, _paremeter);
	}
private:
	Pr _mPr;
	T  _mVal;
};

//Iterator 迭代器
//Pr 一元函数对象
template<typename Iterator , typename Pr>
Iterator my_find_if_v2(Iterator it_begin, Iterator it_end, Pr _pre) {
	for (; it_begin != it_end; ++it_begin) {
		if (_pre(*it_begin)) { return it_begin; }
	}
	return it_end;
}



int main() {

	vector<int> v1;
	srand(time(nullptr));
	for (int i = 0; i < 10; i++) {
		v1.push_back((rand() % 100 + 1));
	}

	printContainter<vector<int>>(v1);

	//升序
	sort(v1.begin(), v1.end(), less<int>());

	//sort(v1.begin(), v1.end(), greater<int>()); 降序

	printContainter<vector<int>>(v1);



	/* 找到第一个大于40数字前插入40 */
	//  方法 一
    #if 0

	vector<int>::iterator it_begin = v1.begin();
	vector<int>::iterator it_end   = v1.end();

	for (; it_begin!=it_end; ++it_begin) {
		if (*it_begin > 40) {  break ;}
	}
	if (it_begin != it_end) {
		v1.insert(it_begin,40);
	}
	printContainter<vector<int>>(v1);
   #endif 


 	//  方法 二 自己实现Bind1st
    #if 0 自己实现Bind1st
	vector<int>::iterator it_find = my_find_if_v2<vector<int>::iterator, MyBindList2<less<int>, int>>(v1.begin(), v1.end(), MyBindList2<less<int>, int>(less<int>(), 40));

	v1.insert(it_find, 40);

	printContainter<vector<int>>(v1);

    #endif 

	//  方法 三  调用库的Bind1st

    vector<int>::iterator find_it2=find_if<vector<int>::iterator, binder1st<less<int>>>(v1.begin(), v1.end(), bind1st<less<int>, int>(less<int>(), 40));
	v1.insert(find_it2, 40);

	printContainter<vector<int>>(v1);

	system("pause");
	return 0;



}

上面代码自己实现泛型算法my_find_if,用于找到容器中指定的位置,插入元素my_find_if 是一个函数模板,参数1,参数2是两个迭代器指向起始和结束位置,在这两个迭代器之间进行遍历,遍历是否满足的条件由第三个参数决定,第三个参数是一个一元函数对象,由于STL现成提供的greater,less都是二元函数对象,所以我们自己需要实现一元函数对象,这个一元函数对象通过提供的二元函数对象和参数进行封装,封装后就是myBind1st,myBind1st底层 operator()(parameter1) 中实际调用的函数二元函数对象的operator()( parameter1 ,parameter2)

vector<int>::iterator it_findValue = my_find_if(it_begin, it_end, myBind1st<greater<int>, int>(greater<int>(), 70));

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们