Basic usage summary of C ++ pair

Basic usage summary of C ++ pair

1, pair of application

pair is the combination of two data into a set of data, when it is desired to use such a demand can pair, such as stl is to map the value in a key and stores them. Another application is, when a function needs to return two data, they can select the pair. The pair is to achieve a structure, the main two member variables are first second because it is not using the struct class, so you can directly use the member variable pair.

Their library type --pair type defined in #include <utility> header file, is defined as follows:

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

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

Function: pair A pair of values ​​(T1 and T2) are combined into one value,

        This data type may have different values ​​(T1 and T2),

        Two values ​​may be a function of two public first pair and second access, respectively.

Define (constructor):

. 1 pair <T1, T2> P1;             // Create an empty pair of objects (the default configuration), which two elements are types T1 and T2, the value is initialized. 
2 pair <T1, T2> P1 (V1, V2);     // create a pair object, which two elements T1 and T2 are types, which first member is initialized to v1, second member is initialized to v2. 
. 3 the make_pair (v1, v2);           // to values v1 and v2 pair create a new object whose type elements are v1 and v2 type. 
. 4 P1 <P2;                     // less than operator between two objects pair, which is defined dictionaries sequence follows: The p1.first <p2.first or (p2.first <p1.first) && (p1.second <p2.second! ) returns true. 
. 5 P1 == P2;                   // if the first and second objects are sequentially equal to two, then the two objects are equal; the arithmetic element used == operator. 
6 p1.first;                    // returns the object p1 named first public data members 
7p1.second;                  // returns the object p1 in the public data member named second

2, pair creation and initialization

pair comprising two values, and as containers, also a pair template type. But it is different from the container described before;

When you create a pair object, you must provide two types of name, type the name of the two corresponding types need not be the same

 

. 1 pair < string , string > anon;         // Create an empty object anon, two types of elements are string 
2 pair < string , int > word_count;      // Create an empty object word_count, two string elements and types are int type 
. 3 pair < string , vector < int >> Line;   // Create an empty object line, are the two elements of type string and vector type

 

Of course, members can be initialized in the definition:

. 1 pair < string , string > author ( " James " , " Joy " );     // Create an author object, two types of elements, respectively, a string type, and default initial value James and Joy. 
2 pair < String , int > name_age ( " Tom " , 18 is );
 . 3 pair < String , int > name_age2 (name_age);     // copy constructor initializes

pair corresponding to the type of use of cumbersome, if the same pair defining a plurality of types of objects can be simplified using typedef statement:

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

Between the variable assignment:

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

3, the operation target pair

Access to two operating elements can be accessed by first and sencond:

 1 pair<int ,double> p1;
 2  
 3 p1.first = 1;
 4  
 5 p1.second = 2.5;
 6  
 7 cout<<p1.first<<' '<<p1.second<<endl;
 8  
 9 //输出结果:1 2.5
10  
11  
12 string firstBook;
13 if(author.first=="James" && author.second=="Joy")
14     firstBook="Stephen Hero";

 

4, generate a new pair objects

Make_pair can also be used to create new objects pair:

 1  pair<int, double> p1;
 2  p1 = make_pair(1, 1.2);
 3  
 4 cout << p1.first << p1.second << endl;
 5  
 6 //output: 1 1.2
 7  
 8 int a = 8;
 9  
10 string m = "James";
11  
12 pair<int, string> newone;
13  
14 newone = make_pair(a, m);
15 cout << newone.first << newone.second << endl;
16  
17 //output: 8 James

 

5, by obtaining the value of the element pair tie

Clear conditions when certain objects pair will function as a return value, can be directly received by std :: tie. such as:

 1 std::pair<std::string, int> getPreson() {
 2     return std::make_pair("Sven", 25);
 3 }
 4  
 5 int main(int argc, char **argv) {
 6     std::string name;
 7     int ages;
 8  
 9     std::tie(name, ages) = getPreson();
10  
11     std::cout << "name: " << name << ", ages: " << ages << std::endl;
12  
13     return 0;
14 }

 

Article reprinted from  https://blog.csdn.net/sevenjoin/article/details/81937695

Guess you like

Origin www.cnblogs.com/YangKun-/p/12519094.html