Summary of basic usage of C++ pair

  • 2020-10-07 18:50:26
  • OfStack

1. Application of pair

pair is the combination of two data into a set of data. pair can be used when such a requirement is needed. For example, map in stl is to store key and value in 1. Another application is to select pair when a function needs to return two data. The implementation of pair is a structure, and the two main member variables are first second. Since struct is used instead of class, the member variables of pair can be used directly.

Its standard library type, pair type, is defined in #include < utility > In the header file, the definition is as follows:

Class template: template < class T1,class T2 > struct pair

Parameters: T1 is the data type of the first value and T2 is the data type of the second value.

Function: pair combines 1 pair of values (T1 and T2) into one value,

The 1 pair of values can have different data types (T1 and T2),

The two values can be accessed using the two public functions of pair, first and second, respectively.

Definition (constructor) :


pair<T1, T2> p1;      // create 1 empty pair Object (using the default construct), whose two elements are T1 and T2 Type, initialized with a value. 
pair<T1, T2> p1(v1, v2);  // create 1 a pair Object whose two elements are T1 and T2 Type, where first Member initialization v1 . second Member initialization v2 . 
make_pair(v1, v2);     //  In order to v1 and v2 The value of creating 1 A new one pair Object whose element types are v1 and v2 The type of. 
p1 < p2;          //  two pair Less than operations between objects whose definitions follow dictionary order: such as  p1.first < p2.first  or  !(p2.first < p1.first) && (p1.second < p2.second)  It returns true . 
p1 == p2 ;          //  If two objects of first and second If they are equal in turn, the two objects are equal; The operation USES the element == Operators. 
p1.first;          //  Returns the object p1 Called in first Public data member 
p1.second;         //  Returns the object p1 Called in second Public data member 

2. Creation and initialization of pair

pair contains two values, like container 1, and pair is also a template type. But it's a different container;

When you create an pair object, you must provide two type names; the two corresponding type names do not have to be the same type


pair<string, string> anon;    //  create 1 An empty object anon , both element types are string
pair<string, int> word_count;   //  create 1 An empty object  word_count,  The two element types are string and int type 
pair<string, vector<int> > line; //  create 1 An empty object line , the two element types are string and vector type 

Members can also be initialized at definition time:


pair<string, string> author("James","Joy");  //  create 1 a author Object, the two element types are string Type and the default initial value is James and Joy . 
pair<string, int> name_age("Tom", 18);
pair<string, int> name_age2(name_age);  //  Copy construct initialization 

The use of the pair type is quite tedious, if you define multiple objects of the same TYPE, you can use typedef to simplify the declaration:


typedef pair<string,string> Author;
Author proust("March","Proust");
Author Joy("James","Joy");

Assignment between variables:


pair<int, double> p1(1, 1.2);
pair<int, double> p2 = p1;   // copy construction to initialize object
pair<int, double> p3 ; 
p3 = p1;  // operator =

3. Operation of pair object

Access to two element operations is available through first and sencond:


pair<int ,double> p1;
 
p1.first = 1;
 
p1.second = 2.5;
 
cout<<p1.first<<' '<<p1.second<<endl;
 
// Output results: 1 2.5
 
 
string firstBook;
if(author.first=="James" && author.second=="Joy")
  firstBook="Stephen Hero";

4. Generate a new pair object

You can also use make_pair to create new pair objects:


 pair<int, double> p1;
 p1 = make_pair(1, 1.2);
 
cout << p1.first << p1.second << endl;
 
//output: 1 1.2
 
int a = 8;
 
string m = "James";
 
pair<int, string> newone;
 
newone = make_pair(a, m);
cout << newone.first << newone.second << endl;
 
//output: 8 James

5. Get the value of pair element through tie

When some clearcase functions return an pair object, they can receive directly via std::tie. Such as:


std::pair<std::string, int> getPreson() {
  return std::make_pair("Sven", 25);
}
 
int main(int argc, char **argv) {
  std::string name;
  int ages;
 
  std::tie(name, ages) = getPreson();
 
  std::cout << "name: " << name << ", ages: " << ages << std::endl;
 
  return 0;
}

Related articles: