现在的位置: 首页 > 综合 > 正文

【STL】pair与make_pair的简单例子

2013年10月14日 ⁄ 综合 ⁄ 共 631字 ⁄ 字号 评论关闭
// pair简单讲就是将两个数据整合成一个数据
// 本质上是有first, second两个成员变量的结构体
extern 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;
	}
}

抱歉!评论已关闭.