Boost library usage _ tuple

Source: Internet
Author: User

Use of tuple
One Boost: tuple
In many cases, we often need to return multiple values for our function. The general practice is to pass in a very large number of pointers or references, but this may be less readable, the user may need an exact document to determine which value is returned. For better readability, we can use class or struct to encapsulate multiple values to be returned, and then return the encapsulated struct or class, however, the disadvantage of using this method is to increase the amount of code in the program. The best solution is to solve this problem through an anonymous struct or class.

Boost: tuple provides us with a method similar to anonymous struct to solve the problem of multiple return values of the function. This not only enhances the readability of the code, but does not increase the amount of code. In fact, STL already has such a special case. std: pair is actually a special case of the two parameters of boost: tuple. You can bind more parameters to boost: tuple, alternatively, you can implement infinite parameters iteratively.
 

Source code analysis

Header file: "boost/tuple. hpp", which contains the tuple class template and core part of the library.

Header file: "boost/tuple/tuple_io.hpp", including the input and output operators for tuple.

Header file: "boost/tuple/tuple_comparison.hpp", which contains the Relational operators of tuple.

For ease of use, some names in the Tuple library are in the namespace boost: such as tuple, make_tuple, tie, and get.

Function Description:

1) constructor
2) copy the constructor
3) t. get <N> () or get <N> (t) to obtain the nth Value
4) make_tuple: Generate tuple
(5) tie, all generated are ref tuple.
6) Reload the comparison operator and can be directly used for comparison
7) Reload the input and output operators and you can directly use IO
8) get_head () and get_tail () functions are used to obtain values.
9) length <> and element <> are used to obtain the size and NTH Value Type of the tuple.
10) If boost: TR1 is used, std: tr1: tuple_size (), std: tr1: tuple_element (), used to obtain the size and NTH value types of tuple.


Three instances

1) tuple constructor, copy constructor, get member function, get global function, and make_tuple global function.

# Include <string>
# Include <iostream>
# Include "boost/tuple. hpp"

Boost: tuples: tuple <int, double> get_values ()
{
Return boost: make_tuple (6, 12.0 );
}
Class base
{
Public:
Virtual ~ Base (){};
Virtual void test ()
{
Std: cout <"base: test ()";
}
};
Class derived: public base
{
Public:
Virtual void test () {std: cout <"derived: test ()";}
};

Void main ()
{
// Test for constructor
Boost: tuple <int, double, std: string> triple (42, 3.14, "My first tuple! ");
Boost: tuple <short, int, long> another;
Boost: tuple <int, int, double> another2 (10 );

// Test for make_tuple, ref and cref function
Int plain = 42;
Int & ref = plain;
Const int & cref = ref;

Boost: tuples: tuple <int> plaint (plain );
Plaint = boost: make_tuple (plain );
Plaint = boost: make_tuple (ref );
Plaint = boost: make_tuple (cref );

Boost: tuples: tuple <int &> reft (ref );
Boost: make_tuple (boost: ref (plain ));
Boost: make_tuple (boost: ref (ref ));
Boost: make_tuple (boost: ref (cref ));

Boost: tuples: tuple <const int &> creft (cref );
Boost: make_tuple (boost: cref (plain ));
Boost: make_tuple (boost: cref (ref ));
Boost: make_tuple (boost: cref (cref ));


// Test for get function
Boost: tuple <int, double, std: string> triple2 (42, 3.14, "The amazing tuple! ");
Int I = boost: tuples: get <0> (triple2 );
Double d = triple2.get <1> ();
Std: string s = boost: get <2> (triple2 );

// Test for function return tuple
Boost: tuples: tuple <int, double> value = get_values ();

// Test for copy constructor
Boost: tuple <int, std: string, derived> tup1 (-5, "Tuples ");
Boost: tuple <unsigned int, std: string, base> tup2;
Tup2 = tup1;
Tup2.get <2> (). test ();
Std: cout <"Interesting value:" <tup2.get <0> () <"";
Const boost: tuple <double, std: string, base> tup3 (tup2 );
// Tup3.get <0> () = 3.14; // error, because tup3 is const

Boost: tuples: tuple <int, int, double> tuple1 (20.000 );
Int head = tuple1.get _ head ();
Int tailhead = tuple1.get _ tail (). get_head ();
Double tail = tuple1.get _ tail (). get_tail (). get_head ();

// For TR1
/* Boost: tuples: tuple <double, char, int> tuplesize;
Std: tr1: tuple_size ();
Std: tr1: tuple_element ();*/

}
 

2) use the tie function template to generate the tuple bound to the ref. tuple is used for comparison and tuple input and output:

# Include <string>
# Include <iostream>
# Include <vector>
# Include <algorithm>
# Include "boost/tuple. hpp"
# Include "boost/tuple/tuple_comparison.hpp"
# Include "boost/tuple/tuple_io.hpp"
Template <int Index>
Class element_less
{
Public:
Template <typename Tuple>
Bool operator () (const Tuple & lhs, const Tuple & rhs) const
{
Return boost: get <Index> (lhs) <boost: get <Index> (rhs );
}
};
Int main ()
{
// Tiers are tuples, where all elements are of non-const reference types.
// They are constructed with a call to the tie function template
Int I; char c; double d;
Boost: tie (I, c, d) = boost: make_tuple (1, "a", 5.5 );
Std: cout <I <"" <c <"" <d <std: endl;

// Test ignore
Char ch;
Boost: tie (boost: tuples: ignore, ch) = std: make_pair (1, "");
Std: cout <ch <std: endl;

// Test for comparison
Boost: tuple <int, std: string> tup1 (11, "Match? ");
Boost: tuple <short, std: string> tup2 (12, "Match? ");
Std: cout <std: boolalpha;
Std: cout <"Comparison: tup1 is less than tup2 ";
Std: cout <"tup1 = tup2:" <(tup1 = tup2) <"";
Std: cout <"tup1! = Tup2: "<(tup1! = Tup2) <"";
Std: cout <"tup1 <tup2:" <(tup1 <tup2) <"";
Std: cout <"tup1> tup2:" <(tup1> tup2) <"";
Std: cout <"tup1 <= tup2:" <(tup1 <= tup2) <"";
Std: cout <"tup1> = tup2:" <(tup1> = tup2) <"";
Tup2.get <0> () = boost: get <0> (tup1); // tup2 = tup1 also works
Std: cout <"Comparison: tup1 equals tup2 ";
Std: cout <"tup1 = tup2:" <(tup1 = tup2) <"";
Std: cout <"tup1! = Tup2: "<(tup1! = Tup2) <"";
Std: cout <"tup1 <tup2:" <(tup1 <tup2) <"";
Std: cout <"tup1> tup2:" <(tup1> tup2) <"";
Std: cout <"tup1 <= tup2:" <(tup1 <= tup2) <"";
Std: cout <"tup1> = tup2:" <(tup1> = tup2) <"";

// Test tuple using in the container
Typedef boost: tuple <short, int, long, float, double, long double> num_tuple;
Std: vector <num_tuple> vec;
Vec. push_back (num_tuple (6, 2 ));
Vec. push_back (num_tuple (7,1 ));
Vec. push_back (num_tuple (5 ));
Std: sort (vec. begin (), vec. end (), element_less <1> ());
Std: cout <"After sorting:" <vec [0]. get <0> () <"<vec [1]. get <0> () <"<vec [2]. get <0> () <"";


// Test for io
Boost: tuple <float, int, std: string> a (1.0f, 2, std: string ("Howdy folks! "));
Std: cout <std: endl <a <std: endl;

Boost: tuple <int, int, int> ii;

Std: cin> ii;
Std: cout <boost: tuples: set_open ("[") <boost: tuples: set_close ("]") <boost: tuples :: set_delimiter (":");
Std: cout <ii <std: endl;

Boost: tuples: tuple <int, int, double> tuple1;
Int head = tuple1.get _ head ();
Double tail = tuple1.get _ tail ();

}
 

4. Note

1) The make_tuple function is similar to std: make_pair. By default, make_tuple sets the element type as non-const and non-reference, which is the simplest and fundamental parameter class.

Type.

2) To set a tuple element to the reference type, you need to use the boost: ref function, which comes from another Boost library named Boost. Ref.

3) if the element needs to be referenced by const, use Boost: cref from boost. Ref.

4) if you want to bind each element variable to ref, you can use the tie function.

Five references

1) Beyond the C ++ Standard Library: An Introduction to Boost
2) boost online document

Link: http://www.cppblog.com/mzty/archive/2007/08/21/30509.html


[Python]
Include <boost/tuple/tuple_io.hpp>
# Include <string>
# Include <iostream>
Using namespace std;
Using boost: tuple;
Class {
Int;
};
 
A class_a;
Boost: tuple <std: string, A> t1 ("abc", class_a );
 
Int main ()
{

// Boost: tuple <int, int, double> tuple_2 = boost: tuple: make_tuple <int, int, do
Double d = 2.0;
A;
Tuple <int, double &, const A &> t (1, d, );
Const tuple <int, double &, const A &> ct = t;
 
Int I = boost: get <0> (t );
Cout <"the I:" <I <endl;
 
I = t. get <0> ();
Cout <"the I:" <I <endl;

Boost: get <1 type = "codeph" text = "/codeph"> (t) = 45.34;
Double d_ret = t. get <1> ();
Cout <"the d_ret:" <d_ret <endl;
 
Int tie_ I;
Char tie_char;
Double tie_doulbe;
Boost: tuple <int, char, double> tuple_temp (3, 'A', 3.1415 );
Boost: tie (tie_ I, tie_char, tie_doulbe) = tuple_temp;
Tuple_temp.get <2> () = 3.132434;
Cout <"the tie_double:" <tuple_temp.get <2> () <endl;
Cout <"the tie_ I:" <tie_ I <endl;
Cout <"the tie_double:" <tie_doulbe <endl;
 
Char char_temp;
Boost: tie (boost: tuples: ignore, char_temp) = std: make_pair (123, 'A ');
Cout <char_temp <endl;

Tuple <float, int, string> tuple_a (1.3, 2, "abd ");
Cout <tuple_a <endl;
Cout <boost: tuples: set_open ('[') <boost: tuples: set_close (']') <boost: tuples
Return 0;
}
 
 
Int main ()
{
 
// Boost: tuple <int, int, double> tuple_2 = boost: tuple: make_tuple <int, int, double> (5, 10, 23.2 );
Double d = 2.0;
A;
Tuple <int, double &, const A &> t (1, d, );
Const tuple <int, double &, const A &> ct = t;
 
Int I = boost: get <0> (t );
Cout <"the I:" <I <endl;
 
I = t. get <0> ();
Cout <"the I:" <I <endl;
 
Boost: get <1 type = "codeph" text = "/codeph"> (t) = 45.34;
Double d_ret = t. get <1> ();
Cout <"the d_ret:" <d_ret <endl;
 
Int tie_ I;
Char tie_char;
Double tie_doulbe;
Boost: tuple <int, char, double> tuple_temp (3, 'A', 3.1415 );
Boost: tie (tie_ I, tie_char, tie_doulbe) = tuple_temp;
Tuple_temp.get <2> () = 3.132434;
Cout <"the tie_double:" <tuple_temp.get <2> () <endl;
Cout <"the tie_ I:" <tie_ I <endl;
Cout <"the tie_double:" <tie_doulbe <endl;
 
Char char_temp;
Boost: tie (boost: tuples: ignore, char_temp) = std: make_pair (123, 'A ');
Cout <char_temp <endl;
 
Tuple <float, int, string> tuple_a (1.3, 2, "abd ");
Cout <tuple_a <endl;
Cout <boost: tuples: set_open ('[') <boost: tuples: set_close (']') <boost: tuples :: set_delimiter ('\ t') <tuple_a;
Return 0;
}


 

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.