重载运算符 标准库function的用法

问题:int(int, int)算不算一种比较通用的类型??

比如函数: int add(int a, int b);

比如lambda:auto mod = [](int a, int b){return a % b};

比如函数对象类:int operator()(int a, int b);

上面3个的共同特征就是:int(int, int),但是如何让上面的3种形式变成共同的的呢???

答案:使用function类。

std::function<int(int, int)> f1 = add;
std::function<int(int, int)> f2 = mod;
std::function<int(int, int)> f3 = divide();
std::cout << f1(1,2) << std::endl;
std::cout << f2(4,3) << std::endl;
std::cout << f3(6,2) << std::endl;

例子:假设某些处理的,参数永远是2个int,返回值永远是int,想把这些处理放到一个函数表里,比如方到std::map里。

#include <functional>
#include <map>
#include <iostream>

int add(int a, int b){
  return a+ b;
}
auto mod = [](int a, int b){return a % b;};
struct divide{
  int operator()(int a, int b){
    return a / b;
  }
};

int main(){
  /*
  std::map<std::string, int(*)(int, int)> mp;
  mp.insert({"+", add});
  mp.insert({"%", mod});
  divide dv;
  mp.insert({"/", divide()});//bian yi bu guo
  
  std::function<int(int, int)> f1 = add;
  std::function<int(int, int)> f2 = mod;
  std::function<int(int, int)> f3 = divide();
  std::cout << f1(1,2) << std::endl;
  std::cout << f2(4,3) << std::endl;
  std::cout << f3(6,2) << std::endl;
  */
  
  std::map<std::string, std::function<int(int, int)>> mp;
  mp.insert({"+", add});
  mp.insert({"-", std::minus<int>()});
  mp.insert({"*", [](int a, int b){return a * b;}});
  mp.insert({"%", mod});
  mp.insert({"/", divide()});

  std::cout << mp["+"](1, 2) << std::endl;
  std::cout << mp["-"](3, 2) << std::endl;
  std::cout << mp["*"](2, 2) << std::endl;
  std::cout << mp["/"](100, 2) << std::endl;
  std::cout << mp["%"](31, 15) << std::endl;
}

github

有个麻烦的情况,例如有2个名字都叫add的方法。


int add(int a, int b){
  return a+ b;
}

double add(double a, double b){  return a + b;}

mp.insert({"+", add});

当mp.insert({"+", add});的时候,编译器无法通过名字确定是哪个方法,怎么办???

使用函数指针消除二义性。

  int(*add1)(int, int) = add;
  double(*add2)(double, double) = add;
  mp.insert({"+1", add1});
  mp.insert({"+2", add2});

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!

相关课程

3776 8.82元 9.8元 9折
4106 9.8元 100元 0.98折