重载运算符 标准库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});

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

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

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

本人微信:xiaoshitou5854

相关文章:

  • 2021-09-11
  • 2021-09-17
  • 2022-12-23
  • 2022-01-01
  • 2021-06-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-24
相关资源
相似解决方案