pair 与 make_pair
2015-11-05 00:07
465 查看
<span style="font-family: Arial, Helvetica, sans-serif;">// pair简单讲就是将两个数据整合成一个数据</span>
// 本质上是有first, second两个成员变量的结构体 //make_pair就是构造pair对象的意思 #include<iostream> #include<map> #include<string> void test_pair() { // pair两种构造的方法 // 方法1 std::pair<std::string, double>("This is a StringTest0.", 9.7); // 浮点数默认是double, float的话有会警告。 std::pair<std::string, double> pA("This is a StringTest.", 9.7); // 方法2 std::pair<std::string, double> pB; pB = std::make_pair("This is a StringTest.", 9.9); // pair的输出 std::cout << pA.first << std::endl; std::cout << pA.second << std::endl; // 结合map的使用 std::map<std::string, double> mA; mA.insert(pA); mA.insert(pB); for (std::map<std::string, double>::iterator it = mA.begin(); it != mA.end(); ++it) { std::cout << "First Member: " << it->first << std::endl; std::cout << "Second Member: " << it->second << std::endl; } } int main() { test_pair(); return 0; }
// make_pair example #include <utility> // std::pair #include <iostream> // std::cout int main() { std::pair <int, int> foo; std::pair <int, int> bar; foo = std::make_pair(10, 20); bar = std::make_pair(10.5, 'A'); // ok: implicit conversion from pair<double,char> std::cout << "foo: " << foo.first << ", " << foo.second << '\n'; std::cout << "bar: " << bar.first << ", " << bar.second << '\n'; return 0; }
原文链接:http://blog.csdn.net/huang_xw/article/details/8201671
相关文章推荐
- kafka运行Producer和Consumer时出现Failed to load class org.slf4j.impl.StaticLoggerBinder错误
- eMMC技术教程(一): eMMC是什么?可以做到多大容量(转自youdianhai.com)
- 【Alpha】Daily Scrum Meeting第三次
- leetcode-Climbing Stairs
- Train Problem I
- Mail搭建
- leetcode-Contains Duplicate II
- Drainage Ditches(最大流)
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
- saiku - JCR在saiku中的运用原理
- git main branch 提交远程仓库失败
- 使用AIDL实现进程间通信
- Daily Scrumming* 2015.11.4(Day 16)
- process launch failed: Security
- 实战阿里云-实战Fail2Ban之v0.9.3
- 使用mail命令发邮件
- maven3+项目管理命令总结 (错误解决:Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:2)
- linux系统监控、诊断工具摘录top IO wait lsof
- httpclient提示Cookie rejected: violates RFC 2109: domain must start with a dot
- 人工智能研究