[pair type in C++] is used to combine two different types of elements into one unit, namely key-value

1. Definition and initialization

pair<type1, type2> p;
// 初始化方法有3种
pair<int, string> p = make_pair(1, "one");
pair<int, string> p = {
    
    1, "one"};
pair<int, string> p(1, "one");

2. How to use

  • There are two member variables, p.first/p.second represent key and value respectively
  • If the function has a return value

Same type, you can use array {}
different types, you can pair

Guess you like

Origin blog.csdn.net/m0_48086806/article/details/132228463
Recommended