C++11 New data structure tuple

Source: Internet
Author: User

About tuple

A tuple is one of C + + 11 's new standard libraries, which represents an n-tuple array, which is equivalent to a struct with n members, except that the members of the struct are anonymous. A tuple is a pair-like template , a tuple is like a generalized version of a pair, a pair can hold only two members, and a tuple can have multiple members, and the same is true for a pair and a tuple to allow its members to be of different types.

Use of tuple

The most common use of a tuple is to use it as the return value of a function, which allows the function to return multiple values. (although C + + can also be implemented by structs to return multiple worthwhile capabilities, it is more cumbersome relative to a tuple.) )

Use of tuple defines tuple
tuple<int,double,string> testtuple1;//新建tuple类型tuple<int,double,string> testtuple2(1,2.0,"test");//新建tuple类型的变量,其类型为tuple<int,double,string>auto testtuple3=make_tuple("test2",123,3.14);//通过初值来初始化tuple,其类型从初值的类型中推断。
Accessing tuple values

With the pair using first and second to get the element is different, because the number of elements of a tuple is not fixed, so you cannot use this way, the members of the tuple are unnamed, to access a tuple member, you need to use a standard library function template called GET, Since it is a function template, you need to make its template arguments,

It is important to note that N in,get< n> (type) requires a constant expression, not a variable.

auto str=get<0>(testtuple3);//访问第一个元素,注意从0开始,auto int_v=get<1>(testtuple3);//访问第二个元素。get<2>(testtuple3)=2.0;//更改第三个元素的值。
Tuple helper Functions

method to get the number of elements of a tuple

size_t sz=tupe_size<testtuple3>::value;//返回3

tupe_size< Tupletype >::value is a class template.

Gets the data type of the first element of the tuple type:

// tuple_element<i,tupletype>::type 是类模板,返回第i个元素的数据类型。cout<<tuple_element<i,tupletest3>::type<<endl;
Relational operators

Two tuple variables can be compared directly to the size, but there are prerequisites. It needs to meet two conditions:
1. Two tuples have the same number of members.
2. Each pair of members of two tuple uses the = = operator to be legal (equal or not), and if it is a relational operator, the < of each member is legal.

bool b=(testtuple1==testtuple2);bool b=(testtuple2<testtuple3);

In addition, because tuple defines the < and = = operators, we can pass the tuple series to the sorting algorithm. And in unordered containers, you can use a tuple variable as the keyword type.

Use a tuple to return multiple values to the function
tuple<int,double,string> get_tuple(){       tuple<int,double,string> tp;    ...    return tp;}auto tp=get_tuple();cout<<get<0>(tp)<<get<1>(tp)<<get<2>(tp)<<endl;

Reference material: "C + + Primer 5th"

C++11 New data structure tuple

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.