Learning Boost 4 Tuple and Ref

xiaoxiao2021-03-06  40

Learning Boost 4

TUPLE AND REF

Tuple

Boost :: Tuple is a class similar to std :: pair. PAIR and can only have two members (first and second), while tuple's elements can be 0-10.

Use tuple to include Boost / Tuple / Tuple.hpp files.

E.g:

#include

...

TUPLE t1 (1, 2.0, "hello");

T2 (1, 'a');

Tuple t3; // In this case, the two int values ​​of T3 are defined.

T4 (1); // only initialize the first element

TUPLE's element is accessed using a GET function or get member function. E.g:

TUPLE t (1, 2.0, "hello");

Cout << T.GET <0> () << endl; // outputs the first element of T, ie 1

T.GET <1> () = 2.0; // assigns the second element of T to 2.0;

// These are all using the GET member function, the overall GET function is used below.

COUT << Get <0> (t) << endl; //

Get <1> (t) = 2.0;

Tuple's elements can also use the reference type. E.g:

Int a;

TUPLE <& int, int> t (a, 1);

T.GET <0> () = 123;

COUT << a << endl; // should output 123

TUPLE <& int> T1; // This is wrong, the reference type must be initialized

TUPLE <& int> T2 (1); // This is also wrong, you can't use constant initialization reference types

TUPLE can also be output directly, but must include tuple_io.hpp files. E.g:

#include // Tuple_io.hpp already included Tuple.hpp files

...

TUPLE t (2, "hello", 1.0);

COUT << T << endl; // output (2 Hello 1.0)

// TUPLE default use '(' start, ')' end, space separated by spaces. You can use the manipulator to change.

Cout << tuples :: set_open ('[')

<< Tuples :: set_close (']')

<< tuples :: set_delimiter (',')

<< t << endl; // output [2, hello, 1.0]

Tuple also has ==,! =,>,

Like Pair, tuple has a Make_Tuple auxiliary function to construct Tuple. E.g:

Cout << make_tuple (1,1.0, "aaa") << endl; // constructs Tuple

If you want to construct a reference type, use the REF auxiliary function. E.g:

INT A = 123;

Cout << make_tuple (1, ref (a)) << Endl; // Constructs Tuple cout << make_tuple (1, CREF (a)) << end1; // constructs Tuple

INT A, B, C;

Tuple T (A, B, C);

T = make_tuple (1, 2, 3); // At this time a == 1 b ==

2 C

== 3

For such applications, it can use more convenient TIE functions. E.g:

INT A, B, C;

TIE (A, B, C) = make_tuple (1, 2, 3);

If you want some elements, you can use Tuples :: ignore instead of the portion where you don't need to use. E.g:

TIE (a, tuples :: ignore, c) = make_tuple (1, 2, 3);

TUPLE performance:

COUT << Sizeof (tuple <>) << Endl; // 1

Cout << Sizeof (TUPLE ) << Endl; // 4

COUT << Sizeof (Tuple ) << Endl; // 8

COUT << Sizeof (Tuple << endl; // 8

COUT << Sizeof (Tuple ) << Endl; // 8

COUT << Sizeof (tuple ) << endl; // 16

TUPLE access is also the same efficiency as PaIR.

Ref

In Tuple, we use the REF and CREF auxiliary functions, then what REF is?

Ref (a) is actually returned to a Reference_Wrapper class, which saves the pointer to the A.

Template Class Reference_Wrapper

{

PUBLIC:

Typedef t type;

#if Defined (boost_msvc) && (boost_msvc <1300)

Explicit Reference_Wrapper (T & T): T _ (& T) {}

#ELSE

Explicit Reference_Wrapper (T & T): T_ (Boost :: Addressof (t)) {}

#ENDIF

Operator t & () const {return * t_;}

T & GET () const {return * t_;}

T * GET_POINTER () const {return t_;}

Private:

T * t_;

}

The above is all the code of Reference_Wraper. Where operator t & () const returns * T_, then any operation of Reference_Wrapper is applied to * t_, and some values ​​can be transferred to the reference delivery via REFERENCE_WRAPPER. E.g:

#include

#include

Using namespace std;

USING NAMESPACE BOOST;

Template Void Inc (t t) {

T; // Due to the reference_wrapper does not have an overloaded operator, all invited converted to int &, that is, reference to A.

}

int main ()

{

INT A = 1;

INC (Ref (a)); // Constructing Reference_Wrapper

COUT << a << endl; // output 2

Return 0;

}

With Reference_Wrapper, we can easily construct a quoted STL container, note that vector is wrong.

Vector > ref_vector;

Int a [10];

For (int i = 0; i <10; i)

Ref_vector.push_back (ref (a [i]));

转载请注明原文地址:https://www.9cbs.com/read-65700.html

New Post(0)