Library and its application

xiaoxiao2021-03-06  46

library and its application

(Danny Kalev Posted on 2004-12-27 16:11:06)

The C Standards Committee is performing the extension of Standard Library. This extension includes a new Tuple library. Below I will discuss this library and its application. (There is currently not included in the standard library file, so you need to download it from Boost: http://sourceforge.net/project/showfiles.php? Group_id = 7586

1, what is TUPLE?

TUPLE is a set of fixed heterogeneous objects. TUPLE types There are many useful applications, such as packaging a function of multiple return values ​​and simultaneous assignments and comparisons for multiple objects.

The size of the tuple refers to the number of elements it contains. The current TUPLE library supports Tuple of 0-10 elements. Each element can be different types. The following example creates a TUPLE with two elements (Float and Void *) and a matching initialization routine.

#include tuple t (2.5, null);

If the initialization routine is ignored, the default initialization process is used:

TUPLE T; // Initialized to (0.0, String ())

2, auxiliary function

The Tuple library includes several auxiliary functions, for example, the make_tuple () function instantiates a TUPLE type according to its parameters:

VoidFunc (INT N); Make_Tuple (FUNC); // Returns: tuple make_tuple ("test", 9); // TUPLE

The tuple_size () function returns a size of a tuple:

INT n = tuple_size > :: value; // 2

The TUPLE_ELEMENT () function retrieves the type of a single element. This function receives an index and tuple type:

// Get thefirst Element's type, i.e., floatt = tuple_element <0, tuple > :: type;

To access the elements itself, use the get () function template. Template parameters (which is also the parameters enclosed in angle brackets) are indexes of elements, and the parameters in parentheses are TUPLE types:

TUPLE TPL; int N = get <0> (tpl); // read 1st elementget <1> (t) = 9.5; // Assign the 2nd Element

3, application

Tuple can be used to encapsulate multiple return values ​​of a function. E.g:

TypeDeftuple mychar_t; mychar_tmygetenv (const mychar_t &);

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

New Post(0)