Boost: tuple deep learning

Source: Internet
Author: User

# Include <iostream>
# Include <string>
# Include <boost/tuple. hpp>
# Include <boost/tuple/tuple_io.hpp>
# Include <boost/tuple/tuple_comparison.hpp>

Using namespace std;

Int main (){
// Boost: tuple extends the C ++ data type std: pair to store multiple, not just two values.

// In addition to boost: tuple, this chapter also covers boost: any and boost: variant classes to store undefined type values. Among them, the boost: any type variables are as flexible as the variables in the weak type language. On the other hand, boost: variables of the variant type can store some predefined data types, just like when we use union.
Typedef boost: tuple <std: string, std: string> person1;
Person1 p1 ("Boris", "Schaeling ");
Cout <p1 <std: endl;
// Like std: pair, which has the auxiliary function std: make_pair (), a tuples can also be created using its auxiliary function boost: make_tuple.
Std: cout <boost: make_tuple ("Boris", "Schaeling", 43) <std: endl;

// A single tuples can also store values of the reference type.
Std: string s = "Boris ";
Std: cout <boost: make_tuple (boost: ref (s), "Schaeling", 43) <std: endl;

// Because "Schaeling" and 43 are passed by value, they are directly stored in the tuples. Unlike them, the first element of person is a reference pointing to s. Boost: Ref () in boost. ref is used to create such a reference. To create a constant reference, you need to use boost: cref ().

// After learning how to create a tuples, let's take a look at how to access elements in the tuples. Std: pair only contains two elements. Therefore, you can use the first and second attributes to access the elements. However, tuples can contain an infinite number of elements. Obviously, we need to solve the access problem in another way.

Typedef boost: tuple <std: string, std: string, int> person2;
Person2 p2 = boost: make_tuple ("Boris", "Schaeling", 43 );
Std: cout <p2.get <0> () <std: endl;
Std: cout <boost: get <0> (p2) <std: endl;

// We can access the elements in the tuples in two ways: Use the member function get (), or pass the tuples to an independent function boost: get (). When the two methods are used, the index value of the element is specified by the template parameter. In this example, the two methods are used to access the first element in p. Therefore, Boris is output twice.

// In addition, the check on the validity of index values will be executed during the compilation period. Therefore, access to invalid index values may cause compilation errors rather than runtime errors.

// You can also use the get () and boost: get () functions to modify elements in tuples.
Typedef boost: tuple <std: string, std: string, int> person3;
Person3 p3 = boost: make_tuple ("Boris", "Schaeling", 43 );
P3.get <1> () = "Becker ";
Std: cout <p3 <std: endl;

// Get () and boost: get () both return a reference value. In this example, after lastname is modified, the output is: (Boris Becker 43 ).

// Boost. Tuple not only reloads stream operation operators, but also provides comparison operators. To use them, you must include the corresponding header file: boost/tuple/tuple_comparison.hpp.

Typedef boost: tuple <std: string, std: string, int> person4;
Person4 p4 = boost: make_tuple ("Boris", "Schaeling", 43 );
Person4 p5 = boost: make_tuple ("Boris", "Becker", 43 );
Std: cout <(p4! = P5) <std: endl;

// The above example will output 1 because the two tuples p1 and p2 are different.


}

Output after compilation:

(Boris Schaeling)
(Boris Schaeling 43)
(Boris Schaeling 43)
Boris
Boris
(Boris Becker 43)
1


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.