ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2018-09-20 09:03:23  阅读:105  来源: 互联网

标签:


标准库 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++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

标签:
来源: https://www.cnblogs.com/xiaoshiwang/p/9678769.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有