当前位置: 首页 > news >正文

(转)c++ std::pair 与 std::make

转自http://code.jiaosha.org/question.aspx?url=wince159___79144d1723769b40f3de32cb.html std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型。例如std::pair 或者 std::pair等。pair实质上是一个结构体,其主要的两个成员变量是first和second,这两个变量可以直接使用。初始化一个pair可以使用构造函数,也可以使用std::make_pair函数,make_pair函数的定义如下: template pair make_pair(t1 a, t2 b) { return pair(a, b); } 一般make_pair都使用在需要pair做参数的位置,可以直接调用make_pair生成pair对象。另一个使用的方面就是pair可以接受隐式的类型转换,这样可以获得更高的灵活度。但是这样会出现如下问题:例如有如下两个定义: std::pair(1, 1.1); std::make_pair(1, 1.1); 其中第一个的second变量是float类型,而make_pair函数会将second变量都转换成double类型。这个问题在编程是需要引起注意。下面是一段pair与make_pair的例子程序: 1 #include 2 #include 3 #include 4 using namespace std; 5 6 int main () { 7 pair product1 ("tomatoes",3.25); 8 pair product2; 9 pair product3; 10 11 product2.first = "lightbulbs"; // type of first is string 12 product2.second = 0.99; // type of second is double 13 14 product3 = make_pair ("shoes",20.0); 15 16 cout << "the price of " << product1.first << " is $" << product1.second << "\n"; 17 cout << "the price of " << product2.first << " is $" << product2.second << "\n"; 18 cout << "the price of " << product3.first << " is $" << product3.second << "\n"; 19 return 0; 20 } 其运行结果如下: 1 the price of tomatoes is $3.25 2 the price of lightbulbs is $0.99 3 the price of shoes is $20 为了更好的了解pair与make_pair的机制,下面是其定义: 1 // template struct pair 2 template struct pair 3 { // store a pair of values 4 typedef pair<_ty1 _ty2> _myt; 5 typedef _ty1 first_type; 6 typedef _ty2 second_type; 7 8 pair(): first(_ty1()), second(_ty2()) 9 { // construct from defaults 10 } 11 12 pair(const _ty1& _val1, const _ty2& _val2): first(_val1), second(_val2) 13 { // construct from specified values 14 } 15 16 template 18 pair(const pair<_other1 _other2>& _right) 19 : first(_right.first), second(_right.second) 20 { // construct from compatible pair 21 } 22 23 void swap(_myt& _right) 24 { // exchange contents with _right 25 std::swap(first, _right.first); 26 std::swap(second, _right.second); 27 } 28 29 _ty1 first; // the first stored value 30 _ty2 second; // the second stored value 31 }; 32 33 34 template inline 35 pair<_ty1 _ty2> make_pair(_ty1 _val1, _ty2 _val2) 36 { // return pair composed from arguments 37 return (pair<_ty1 _ty2>(_val1, _val2)); 38 }

转载于:https://www.cnblogs.com/cumtb3S/archive/2011/11/22/2258419.html

相关文章:

  • php 使用 ffmpeg 转换视频,截图,并生成缩略图
  • a erlang crawler
  • 彻底删除该死的rac
  • Geolocation --- 基于浏览器的定位服务
  • NHibernate初学者指南(14):配置的三种方式
  • SkScalar
  • TabControl
  • WinINet function(1)
  • Archlinux KDE环境中文化
  • 【转载】ESFramework介绍之(31)―― 消息分类及对应的处理器
  • 表空间的增删改
  • cs1003:语法错误,应输入“:“
  • dsp中的gel文件
  • 代码质量与上线压力
  • 开启Apache Server Status
  • iOS | NSProxy
  • js如何打印object对象
  • learning koa2.x
  • Redis 中的布隆过滤器
  • Windows Containers 大冒险: 容器网络
  • 大整数乘法-表格法
  • 基于遗传算法的优化问题求解
  • 使用权重正则化较少模型过拟合
  • 追踪解析 FutureTask 源码
  • #考研#计算机文化知识1(局域网及网络互联)
  • (C语言)逆序输出字符串
  • (待修改)PyG安装步骤
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (分布式缓存)Redis哨兵
  • (四)鸿鹄云架构一服务注册中心
  • (四)汇编语言——简单程序
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)Sql Server 保留几位小数的两种做法
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .mysql secret在哪_MySQL如何使用索引
  • .net反混淆脱壳工具de4dot的使用
  • .NET牛人应该知道些什么(2):中级.NET开发人员
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • [Android]Android P(9) WIFI学习笔记 - 扫描 (1)
  • [C#][opencvsharp]opencvsharp sift和surf特征点匹配
  • [hdu 4405] Aeroplane chess [概率DP 期望]
  • [MT8766][Android12] 取消WIFI热点超过10分钟没有连接自动关闭设定
  • [NowCoder]牛客OI周赛3
  • [one_demo_14]一个简单的easyui的demo
  • [one_demo_7]求走到第50个台阶的走法多少种
  • [Real world Haskell] 中文翻译:第一章 快速上手
  • [RK3568 Android11] Binder通信整体框架
  • [SageMath] 关于SageMath本地环境的搭建与基本使用
  • [SDUT](3361) 数据结构实验之图论四:迷宫探索 ---DFS(图)
  • [svc]logstash和filebeat之间ssl加密
  • [Tcpdump] 网络抓包工具使用教程
  • [THUPC 2024 初赛] 二进制 (树状数组单点删除+单点查询)(双堆模拟set)
  • [uniapp] uni-ui+vue3.2小程序评论列表组件 回复评论 点赞和删除
  • [北航软工]技术规格说明书
  • [笔记]8组LVDS_TX和LVDS_RX的调试心得