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

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

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

首先,因为对象可以在内部修改而不用改动外部接口,因此设计更灵活,更富有弹性。函数对象也具备有存储先前调用结果的数据成员。在使用普通函数时需要将先前调用的结果存储在全程或者本地静态变量中,但是全程或者本地静态变量有某些我们不愿意看到的缺陷。
其次,在函数对象中编译器能实现内联调用,从而更进一步增强了性能。这在函数指针中几乎是不可能实现的。
C++11还提供了lambda表达式来实现函数的灵活调用。详见《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元素的迭代器

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

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

原文链接: https://www.cnblogs.com/erichome/p/16949514.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/4988

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月4日 下午7:12
下一篇 2023年2月4日 下午7:13

相关推荐