pair 与 make_pair

<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


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值