In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use C++ Tuple". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use C++ Tuple".
Preface
Tuple is a template similar to pair. Each pair has a different member type, but each pair happens to have two members. Different tuole types are also different, but a tuple can have any number of members. The number of members of each determined tuple type is fixed, but the number of members of one tuple type can be different from that of another tuple type.
Tuple is very useful when we want to combine some data into a single object without bothering to define a new data to represent it.
For example, we can construct a tuple
Tupletp = make_tuple (sendPack,nSendSize)
This tuple is equivalent to a structure.
Struct A {char* p; int len;}
With tupletp, you don't have to create this structure, but the function is the same, isn't it more concise and intuitive?
Another way to create a tuple is to use std::tie, which creates a reference to the left value of the tuple.
Auto tp = return std::tie (1, "aa", 2)
The type of tp is actually:
Std::tuple
The first impression of tuple
Tuple supports the following operations
Std::tuple t; / / create an empty tuple object (using the default construct) with elements of type T1 and T2...Tn, initialized with values. Std::tuple T2 (v1, v2,... Vn); / / create a tuple object with elements of T1 and T2... Tn types; each member initializes std::tuple T3 (ref&) with the corresponding vi; / / the element type of tuple can be a reference
Like pair, you can also create a tuple object through make_tuple, and the type of tuple is inferred from the type of the initial value.
Std::make_tuple (v1, v2)
Returns a reference to the first data member of t: if t is a left value, the result is a left value reference; otherwise, the result is a right value reference. In addition, all members of tuple are pulic.
Get (t)
We can think of tuple as a "fast and casual" data structure.
Define and initialize tuple
When we define a std::tuple, we need to indicate the type of each member.
All three members of the tuple threeD; / / are set to 0 tuple someVal ("constans", {3.14pr 2.718}, 42, {0pc1 recorder 2meme 3re4je 5}).
When we create a std::tuple object, we can use the default constructor of tuple, which initializes the value of each member, or we can provide an initial value for each member as in the above someVal initialization, where the constructor is explicit, so we must use the direct initialization method.
Tuple htreeD = {1pm 2pm 3}; tuple htreeD (1pm 2pm 3)
Similar to the make_pair function, the standard library defines the make_tuple function, which we can also use to generate std::tuple objects.
Auto item = mak_tuple ("0999-78345murx", 3mai 20.00)
A function like make_pair,make_tuple uses the type of the initial value to infer the type of tuple. In the above example, item is a tuple of type tuple.
Access members of tuple
A pair always has two members so that the standard library can name them (first and second), but this naming method does not apply to tuple because there is no limit to the number of members of a tuple type. Because the members of tuple are unnamed. To access a member of tuple, use a standard library function template called get. In order to use get, we must specify a display template argument that indicates which member we want to access. We pass get a tuple object that returns a reference to the specified member.
Auto book = get (iterm); / / return the first member of iterm auto cnt = get (iterm); / / return the second member of iterm auto price = get (iterm) / cnt; / / return the third member of iterm
The value in angle brackets must be an integer constant expression, and as usual, we count from 0, meaning that get is the first member.
If you do not know the exact type details of tuple, you can use two auxiliary class templates to query the number and type of members of tuole:
1. A class template, which can be initialized by a tuple type, has a public constexpr static data type named value, of type size_t, which represents the number of members in a given tuple type
Tuple_element::type
two。 A class template that can be initialized with an integer constant and a tuple type. It has a public member named type, which represents the type specified in a given tuple type
Tuple_size::value
Through these two class templates, we can get the number and type of members of the tuple variable we need.
Typedef decltype (item) trans;//trans is the type of item size_t sz = tuple_size
< trans>:: value;// returns the number of members in the trans type object tuple_element::type cnt; / / cnt is item's second member variable type int cnt = get (item)
In order to use tuple_size or tuple_element, we need to know the type of a tuple object. As always, the easiest way to determine the type of an object is to use decltype. In typedef decltype (item) trans;, we use decltype to define a type alias for item and use it to instantiate the two templates.
Tuple_size has a public static data member named value, which represents the number of members in a given tuple. The tuple_element template accepts an index value in addition to a tuple type. It has a public type member named type, which represents the specified member type in a given tuple type. Indexes similar to those used by get,tuple_element are counted starting at 0.
The relationship of the std::tuple and the behavior of the equality operator are similar to those of the container. These operators compare the members of the left tuple and the right tuple one by one. We can compare two tuple only if they have the same number of members. Moreover, in order to use the equality or inequality operator of tuple, it must be legal to use the = = operator for each pair of members; in order to use the relational operator, it must be legal to use < for each pair of members.
Relational and equality operators: when two tuole have the same number of members and the corresponding members are equal, the two tuple are the same.
Tuple duo ("1", "2"); tuple twoD (1 twoD 2); bool b = (duo = = twoD); / / error, unable to compare size_t and string tuple threeD (1 Magi 2, 3); b = (duo = = threeD); / / error, different number of members tuple origin (0 twoD); b = (origin < twoD); / / correct: B is true
Because tuple defines the
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.