C++中std::pair的使用

This std::pair struct template couples together a pair of values, which may be of different types (T1 and T2). The individual values can be accessed through its public members first and second.

std::pair is a struct template that provides a way to store two heterogeneous objects as a single unit. A pair is a specific case of a std::tuple with two elements.

std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型。pair是一个模板结构体,其主要的两个成员变量是first和second,这两个变量可以直接使用。初始化一个pair可以使用构造函数,也可以使用std::make_pair函数。

下面是测试代码:

#include "pair.hpp"
#include <iostream>
#include <vector>
#include <string>


// reference: https://www.quora.com/How-can-I-use-pair-int-int-v-in-C++-language
int test_pair1()
{
	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'; // foo: 10, 20
	std::cout << "bar: " << bar.first << ", " << bar.second << '\n'; // bar: 10, 65

	return 0;
}

//
// reference: http://stackoverflow.com/questions/2164942/how-can-i-store-a-pair-of-numbers-in-c/2164969#2164969
int test_pair2()
{
	typedef std::pair<int, int> IntPair;

	std::vector<IntPair> pairs;
	pairs.push_back(std::make_pair(1, 2));
	pairs.push_back(std::make_pair(3, 4));

	for (int i = 0; i < pairs.size(); i++) {
		std::cout << pairs[i].first << "    " << pairs[i].second << std::endl;
	}

	return 0;
}


int test_pair3()
{
	std::pair<std::string, int> name1("Som", 15);
	std::pair<std::string, int> name2(name1);
	std::cout << "name2: " << name2.first << "    " << name2.second << std::endl;
	
	std::pair<std::string, int> name3 = std::make_pair("Som", 15);
	if (name1 == name3) {
		std::cout << "they are the same people" << std::endl;
	} else {
		std::cout << "they are not the same people" << std::endl;
	}

	std::pair<std::string, int> name4;
	name4 = name1;
	std::cout << "name4: " << name4.first << "    " << name4.second << std::endl;

	std::pair<std::string, int> name5("Som", 16);
	if (name1 > name5) {
		std::cout << "name1 > name5" << std::endl;
	} else if (name1 < name5) {
		std::cout << "name1 < name5" << std::endl;
	} else {
		std::cout << "name1 == name5" << std::endl;
	}

	std::pair<std::string, int> name6("Take", 11);
	std::cout << "name1: " << name1.first << "    " << name1.second << std::endl;
	std::cout << "name6: " << name6.first << "    " << name6.second << std::endl;
	std::swap(name1, name6); // C++11::endl;
	std::cout << "after std::swap: " << std::endl;
	std::cout << "name1: " << name1.first << "    " << name1.second << std::endl;
	std::cout << "name6: " << name6.first << "    " << name6.second << std::endl;

	return 0;
}

GitHubhttps://github.com/fengbingchun/Messy_Test

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值