c/c++ 标准库 bind 函数详解

作者:小石王 时间:2023-03-29 09:10:39 

bind函数定义在头文件 functional 中。可以将 bind 函数看作一个通用的函数适配器,它接受一个可调用对象,生成一个新的可调用对象来“适应”原对象的参数列表。

bind函数:接收一个函数名作为参数,生成一个新的函数。


auto newCallable = bind(callbale, arg_list);

arg_list中的参数可能包含入_1, _2等,这些是新函数newCallable的参数。

在这篇博客lambda 表达式 介绍 中,讨论了find_if的第三个参数的问题,当时是用lambda表达式解决的,有了bind函数后,也可以用bind函数解决。

解决办法:bind(check_size, _1, sz)


auto idx = find_if(svec.begin(),svec.end(),bind(check_size, _1, 6));

其实,newCall= bind(check_size, _1, sz)返回了一个新的函数newCall ,这个newCall 只接受一个参数,正好满足find_if的要求。

•从find_if的角度来看,啊,newCall是含有一个参数的函数,OK,没问题。
•从程序猿的角度看,check_size是含有2个参数的函数,只是提前把sz(6)绑定到了newCall上了,
•当调用newCall(s),实际是调用了check_size(s, 6),相当于newCall也有2个参数,只是第二个参数有个默认值为6。newCall(const string &s, size_t sz = 6); ,所以调用newCall时,传递一个参数就够了。

注意:_1,_2等,是放在了命名空间placeholder中,所以要使用:


//_1,_n定在std::placeholders里面
using namespace std::placeholders;

bind参数用法:


//g是以个有2个参数的可调用对象
auto g = bind(func, a, b, _2, c, _1);//func是有5个参数的函数

调用g(X, Y), 等于 func(a, b, Y, c, X)

例子:


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;
//_1,_n定在std::placeholders里面                        
using namespace std::placeholders;
bool check_size(const string &s, string::size_type sz){
return s.size() >= sz;
}
bool shorter(const string &a, const string &b){
return a.size() < b.size();
}
ostream& print(ostream& os, const string &s, const char &c){
//c = ',';                                  
return os << s << c;
}
int main(){
/*                                      
//用bind实现了和lambda一样的功能                      
vector<string> svec{"aab","d","aa","bb","e","bbb"};              
stable_sort(svec.begin(),svec.end(),[](const string &a, const string &b){  
  return a.size() < b.size();                        
 });                                    
string::size_type sz = 3;                          
auto idx = find_if(svec.begin(),svec.end(),bind(check_size, _1, sz));    
cout << *idx << endl;                            
idx = find_if(svec.begin(),svec.end(),[sz](const string &s){        
  return s.size() >= sz;                          
 });                                    
cout << *idx << endl;                            
*/
/*                                      
//用bind改变原来函数的参数的位置                      
//升序                                    
vector<string> svec{"aab","d","aa","bb","e","bbb"};              
sort(svec.begin(), svec.end(), shorter);                  
for(auto const &s : svec){                          
 cout << s << " ";                              
}                                      
cout << endl;                                
//由于调换了shorter参数的位置,所以变成了降序                
sort(svec.begin(), svec.end(),bind(shorter, _2, _1));            
for(auto const &s : svec){                          
 cout << s << " ";                              
}                                      
cout << endl;                                
*/
//bind引用,必须使用ref或者cref函数,把对象转化成引用,不能用&        
ostream &os = cout;
const char c = ' ';
vector<string> svec{"aab","d","aa","bb","e","bbb"};
for_each(svec.begin(),svec.end(),[&os, c](const string &s){
  os << s << c;
 });
os << endl;
for_each(svec.begin(),svec.end(),bind(print, ref(os), _1, cref(c)));
os << endl;
cout << c << endl;
}

总结

以上所述是小编给大家介绍的c/c++ 标准库 bind 函数详解网站的支持!

来源:https://www.cnblogs.com/xiaoshiwang/archive/2018/09/20/9678769.html

标签:c++,标准库,bind,函数
0
投稿

猜你喜欢

  • Java案例之随机验证码功能实现实例

    2022-05-24 13:28:16
  • java结合email实现自动推送功能

    2023-07-09 00:16:43
  • JDK 7 新特性小结实例代码解析

    2022-04-18 13:03:07
  • Java模板动态生成word文件的方法步骤

    2023-01-08 15:19:58
  • Java中管理资源的引用队列相关原理解析

    2023-04-03 11:37:59
  • SpringBoot如何实现Tomcat自动配置

    2022-04-28 02:44:10
  • Unity3D UGUI实现缩放循环拖动卡牌展示效果

    2022-02-19 10:37:24
  • java常用工具类 Random随机数、MD5加密工具类

    2023-02-14 17:55:08
  • Java设计模式之java桥接模式详解

    2023-10-15 15:19:10
  • Java多线程实现Runnable方式

    2022-06-29 17:09:46
  • C#验证给定字符串是否为数字的方法

    2021-08-24 10:22:21
  • 在实践中了解Java反射机制应用

    2021-06-29 02:40:14
  • SpringBoot使用swagger生成api接口文档的方法详解

    2021-10-22 18:11:48
  • Java开发工具IntelliJ IDEA安装图解

    2022-06-14 02:30:20
  • Android实现MVVM架构数据刷新详解流程

    2023-07-05 13:33:41
  • Java如何自定义异常打印非堆栈信息详解

    2022-05-06 09:43:36
  • 浅谈Glide缓存key的问题

    2023-10-05 05:26:35
  • java web实现邮箱激活与忘记密码

    2023-11-18 09:35:26
  • 基于java构造方法Vector修改元素源码分析

    2023-11-25 10:54:56
  • java面向对象:API(接口)与集合(ArrayList)

    2021-06-07 03:28:19
  • asp之家 软件编程 m.aspxhome.com