Tuple and data type of C++11

Source: Internet
Author: User


C++11 introduces a multivariate array of tuples, which is used to hold arrays of different data.
There are two types of initialization methods:
Tuple () constructor
Make_tuple () Create function


There are several commonly used functions as follows:
Head () Gets the value of the first element
Tail () Gets the value of all remaining elements
Get<n> () Gets the value of the nth element, n must be a constant, cannot iterate through a tuple using a loop
Tuple_element<n, Decltype (tup) >::type Gets the type of the nth element
Tuple_size<decltype (TUP) >::value Gets the number of elements in the tuple tup


In addition about type:
Auto automatic type deduction, which infers the data type of a variable from an initialization expression
Decltype get a type from a variable or an expression
It sounds like a mixed-up look at the code.

#include <map>//Use a tuple to include the header file # include <iostream>using namespace Std;int main () {tuple<int, char, String>tup0 (1, ' A ', "Hello World"); int e1 = Tup0.head ();//1int e2 = Tup0.tail (). Head (); ' A ' unsigned short cnt = Tuple_size (Decltype (TUP0)):: value; Gets the number of elements: 3tuple<char, string> tup1 = Tup0.tail (); Tuple<double, string> tup2 = make_tuple (1.1, "ABC"); auto D0 = get<0> (tup2);//1.1auto D1 = get<1> (tup2); "ABC" cout <<typeid (D0). Name () << endl;//Gets the data type of the element: Doubledecltype (d0) d2 = d0;//The data type of the inverse D0, and uses it to declare a variable D0, Then assign a value of D0return 0;}


Tuple and data type of C++11

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.