14.44编写一个简单的桌面计算器使其能处理二元运算。

#include<iostream>
#include<map>
#include<functional>
#include<string>
using namespace std; int add(int a,int b)
{
return a+b;
} struct divide{
int operator()(int denominator,int divisor)
{
return denominator/divisor;
}
}; auto mode=[](int i,int j) { return i%j;}; int main()
{
int (*pf)(int,int)=add;
map<string,function<int(int,int)>> binops;
binops.insert({"+",add});
binops.insert({"+",pf});
binops.insert({"-",minus<int>()});
binops.insert({"*",[](int i,int j) {return i*j;}});
binops.insert({"/",divide()});
binops.insert({"%",mode});
cout<<binops["+"](,)<<endl;
cout<<binops["-"](,)<<endl;
cout<<binops["*"](,)<<endl;
cout<<binops["/"](,)<<endl;
cout<<binops["%"](,)<<endl;
return ;
}

运行结果:

标准库function类型的使用的更多相关文章

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

    重载运算符 标准库function的用法 问题:int(int, int)算不算一种比较通用的类型?? 比如函数: int add(int a, int b); 比如lambda:auto mod = ...

  2. 把《c++ primer》读薄(3-3 标准库bitset类型)

    督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. //开头 #include <bitset> using std::bitset; 问题1.标准库bitset类型( ...

  3. C++标准库string类型

    string类型支持长度可变的字符串,C++标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string类型的目的就是满足对字符串的一般应用. 本文地址:http://www.cn ...

  4. C++标准库vector类型的使用和操作总结

    vector是一种类型对象的集合,它是一种顺序容器,容器中的所有对象必须都是同一种类型.想了解顺序容器的更多内容:C++顺序容器知识总结.vector的对象是可以动态生长的,这说明它在初始化时可以不用 ...

  5. zt C++标准库set类型

    C++标准库set类型 分类: C++编程语言 2012-11-06 10:53 909人阅读 评论(0) 收藏 举报 目录(?)[-] 在set中添加元素 从set中获取元素 set容器只是单纯的键 ...

  6. 【C++ Primer每日刷】之三 标准库 string 类型

    标准库 string 类型 string 类型支持长度可变的字符串.C++ 标准库将负责管理与存储字符相关的内存,以及提供各种实用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与 ...

  7. 把《c++ primer》读薄(3-1 标准库string类型初探)

    督促读书,总结精华,提炼笔记,抛砖引玉,有不合适的地方,欢迎留言指正. 问题1:养成一个好习惯,在头文件中只定义确实需要的东西 using namespace std; //建议需要什么再using声 ...

  8. C++标准库string类型的使用和操作总结

    string是C++标准库最重要的类型之一,string支持长度可变的字符串,其包含在string头文件中.本文摘自<C++PRIMER 第四版·特别版>和个人的一些总结. 一.声明和初始 ...

  9. Python标准库映射类型与可散列数据类型的关系

    这里有两个概念似懂非懂,在这里明确一下: 映射类型: Python>3.2中,collections.abc模块有Mapping和MutableMapping两个抽象基类(Python2.6~3 ...

随机推荐

  1. Hdu 2979 Expensive Drink

    Description There are some water, milk and wine in your kitchen. Your naughty little sister made som ...

  2. Linux Screen命令使用

    参考URL: http://jingyan.baidu.com/article/295430f128d8ea0c7e005089.html ~~~~~~~~~~~~~~~~~~~~~~~~ 其它的不提 ...

  3. Performance Test of List<T>, LinkedList<T>, Queue<T>, ConcurrentQueue<T>

    //Test Group 1 { var watch = Stopwatch.StartNew(); var list = new List<int>(); ; j < ; j++) ...

  4. 打造属于自己的Altium Designer 3D封装库,不需要懂专门的三维设计软件

    看到Andy_2020发的帖子“Altium Designer专题”之后,对Altium Designer的3D功能很感兴趣,着手自己做一个AD的3D封装库.刚开始按照Andy介绍的方法,学了两天So ...

  5. Android软件开发之发送短信与系统短信库解析

    今天我和同学们讨论一下Android平台下如何调用系统方法发送短信.接收短信.系统的短信库相关的问题.进入正题,我们先使用Eclipse工具模拟给自己的模拟器发送一条短信.在Eclipse下打开DDM ...

  6. Android上按钮解决快速点击问题

           //代码2         public abstract class NoDoubleClickListener implements OnClickListener {        ...

  7. String 类;Math 类;

    static void Main(string[] args)        {            while (true)            {               /* strin ...

  8. 如何删除MyEclipse(eclipse)中不需要的workspace

    在安装目录下,打开eclipse/configuration/.settings,用记事本打开org.eclipse.ui.ide.prefs文件 #Wed Mar 11 14:41:21 CST 2 ...

  9. [Boost::Polygon]多边形相减得到新的多边形序列

    #include <iostream> #include <boost/polygon/polygon.hpp> #include <cassert> namesp ...

  10. EF-CodeFirst-表关系-延迟/贪婪加载

    表关系建立: http://blog.csdn.net/niewq/article/details/17232321 一对多: namespace LckLib.EF.V6.Models { publ ...