Using tuple__c++ in C + +

Source: Internet
Author: User


This article discusses the use of tuple in C + + standards, rather than the Python language.


When it comes to tuple, it's definitely the first time to think of the Python language, but tuple is not just in Python, but in languages like c#,c++.

Similar in tuple and Python languages in C + +, it is a powerful container that allows for multiple different types of data, and is a generalization of pair.

To use tuple in C + +, you first need to refer to the header file tuple and the namespace Std.


and tuple related to a total of 4 functions, respectively, introduced

1. Make_tuple: for creating tuple

Auto Tup1 = Std::make_tuple ("Hello world!", ' a ', 3.14, 0);

The code above creates a tuple of tuple <const char*, char, double, and int> type.

As can be seen, in tuple can be completely different data types.


2. Tie: Used to disassemble tuple

Auto Tup1 = std::make_tuple (3.14, 1, ' a ');
Double A;
int b;
char c;
Std::tie (A, b, c) = Tup1;

The result of this is a = 3.14, B = 1, c = ' a '.

If you do not want a value for a bit, you can replace it directly with ignore.

Std::tie (Ignore, B, c) = Tup1;

3. Forward_as_tuple: Used to accept the right value reference data generation tuple

Auto tup2 = std::forward_as_tuple (1, "Hello");

The above code creates a tuple of the Tuple<int &&, char (&) [6]> type.

As you can see, the parameters in tuple are all reference to right values. The tie function discussed above can only accept the left value.


4. Tuple_cat: For connecting tuple

Std::tuple<float, string> tup1 (3.14, "PI");
Std::tuple<int, char> tup2 ("a");
Auto Tup3 = Tuple_cat (Tup1, tup2);

To connect Tup1 and Tup2 became TUP3.


For getting elements in tuple, you have the following actions

1. Get<i>: Get the value of the first element

Std::tuple<float, string> tup1 (3.14, "PI");
cout << get<0> (tup1);

This outputs the first element 3.14 in the tup1.


2. Tuple_element: Get specific element data types in tuple

Std::tuple_element<0, Decltype (tup1) >::type

This gets the data type of the first element in the Tup1.

Note: The data type, such as Int,char, is obtained. Instead of a string that says "int" or "char".


3. Size: Get the number of elements in tuple

Std::tuple<float, string> tup1 (3.14, "PI");
cout << Tuple_size<decltype (tup1) >::value;
The output is 2, which indicates that there are two elements in the tuple.


As you can see, tuple is a very powerful data structure, much more powerful than the usual pair,vector.


The above code is tested through VS2015.

Some students said that some of the content involved in C + + 14, the minimum requirements VS2015 to pass. But the tuple class is available in C + + 11 and can be used in VS2013, supplemented in VS2015.

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.