We have used pair. pair in C ++ as a template type, which contains two data values. The two data types can be different. pair can be constructed using make_pair.
pair
p = make_pair(1, "a1");
If multiple parameters are passed in, you need to nest pair, as shown in the following code:
# Include
# Include
Using namespace std; int main (){//
Note: Use '>' instead of '> 'map In the nested template real parameter list.
> M; map
Temp1; temp1.insert (make_pair ("b1", "c1"); map
> Temp2; temp2.insert (make_pair ("a1", temp1); m. insert (make_pair (1, temp2); map
Temp3; temp3.insert (make_pair ("b2", "c2"); map
> Temp4; temp4.insert (make_pair ("a2", temp3); m. insert (make_pair (2, temp4); // traverse the map
>>: Const_iterator itr1; map
>:: Const_iterator itr2; map
: Const_iterator itr3; for (itr1 = m. begin (); itr1! = M. end (); itr1 ++) {cout <itr1-> first <""; itr2 = (itr1-> second ). begin (); cout <itr2-> first <""; itr3 = (itr2-> second ). begin (); cout <itr3-> first <""; cout <itr3-> second <endl;} pair
P = make_pair (1, "a1"); return 0 ;}
The above method is obviously very troublesome. The variable length parameter template was introduced in C ++ 11, so a new data type was invented: tuple, tuple is an N-tuples, and one can be input, two or more different types of data avoid the ugly practice of nesting pair. Create a tuples through make_tuple () and access the elements of the tuples through get <> ().
#include
#include
#include
using namespace std;int main(){auto t1 = make_tuple(1, "a1", "b1", "c1");cout << get<0>(t1) << " ";cout << get<1>(t1) << " ";cout << get<2>(t1) << " ";cout << get<3>(t1) << " ";cout << endl;vector
> tv;tv.push_back(make_tuple(1, "a1", "b1", "c1"));tv.push_back(make_tuple(2, "a2", "b2", "c2"));vector
>::iterator itr;for (itr=tv.begin(); itr!=tv.end(); itr++){cout << get<0>(*itr) << " ";cout << get<1>(*itr) << " ";cout << get<2>(*itr) << " ";cout << get<3>(*itr) << endl;}return 0;}