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; } GitHub: https://github.com/fengbingchun/Messy_Test