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

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

1414315-20181106214320230-961379709.jpg

本人微信:xiaoshitou5854

转载于:https://www.cnblogs.com/xiaoshiwang/p/10176879.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值