Tr1 tuple usage

Source: Internet
Author: User

The tuple in tr1 is equivalent to the pair extension in STL. Pair can only contain two elements, while tuple can contain at least 10 elements.

# Include <tr1/tuple>
Using namespace STD: tr1;

Tuple objects can be generated by constructors or through make_tuple and tie functions.

// Constructor
Tuple <int, int> tii (0, 0 );

// Make_tuple
Tuple <int, int> tii = make_tuple (0, 0 );

// Tie
Int I = 0;
Int J = 0;
Tuple <int, int> tii = tie (I, j );

The difference between tie and make_tuple is that all the elements in the tuple object generated by tie are of reference type.

After a tuple object is generated, you can use tuple_size and tuple_element to obtain the number of elements and the type of each element in the tuple object.

Cout <tuple_size <tii>: value; // output 2, because tii contains two elements.

Cout <type_id (tuple_element <0, tii>: type). Name (); // int should be output. The type of The 0th elements is int.

Here, value is a static member of tuple_size, so it can be obtained directly using.
Similarly, type is a static member of tuple_element. Type_id is an operator that helps convert a type into a readable form.

You can use the get function to return a reference to an element in a tuple object. Therefore, you can read and write specific elements in a tuple object.

Get <0> (tii) = 2; // The current tii value is (2, 0)
Get <1> (tii) = 3; // The current tii value is (2, 3.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.