pair and tuple in c + +

Posted by DarrenL on Wed, 08 Dec 2021 08:39:56 +0100

1, Complex data structure in c + +

Since c, c + + has also developed. Complex data structures are generally not supported in standard libraries. For example, multi-dimensional arrays, multi key KV, not to mention a complex batch of trees. In fact, it is well understood that as a low-level support language, c/c + + pursues high efficiency and multi platform support, which makes it impossible to optimize the whole data structure in complex data structures, especially for specific platforms. This is also the data structure that many big cattle roll out by themselves (such as jump tables, etc.).
In terms of image, STL library is composed of some "short and concise" functions and classes, so there will be no particularly complex data structures. In fact, there are some complex data structures (red and black trees) at the bottom, such as the bottom implementation of map.

2, pair and tuple

Pair and tuple are data structures used for complex data encapsulation. The former is similar to KV database, one-to-one. The latter can be considered as an extension of pair, which can support more values in one element. The early standard is to support less than ten, while after c++11, it can support more values. In fact, c/c + + can encapsulate each other, which will produce a larger composite structure. For example, two tuples can be combined into pair.
Let's take a look at their definitions in STL:

//Defined in header <utility>
template<
    class T1,
    class T2
> struct pair;

//Defined in header <tuple>
template< class... Types >
class tuple;

For pair, in addition to using constructors, STL provides a convenient way STD:: make_ Pair < > is generally recommended. Pair provides common operations such as exchange and comparison, which is more and more convenient to use. For tuple, STL calls it an indefinite length group. It is more complex than pair, but you can go to std::tie. This std::tie is an lvalue reference of tuple, which is interesting. Tuple also provides an STD:: make_ Tuple < > to create, one has two. It also provides a tuple_ The cat () function can connect multiple tuples according to the name, which is very useful in specific situations.
Similarly, after c++11, the initializer list can be used to realize the overall assignment structure. pair and tuple can be converted through related constructors, but it still doesn't seem very flexible to use.

3, Routine

Let's take a look at the routine of pair:

#include <iostream>
#include <utility>
#include <functional>
 
int main()
{
    int n = 1;
    int a[5] = {1, 2, 3, 4, 5};
 
    // build a pair from two ints
    auto p1 = std::make_pair(n, a[1]);
    std::cout << "The value of p1 is "
              << "(" << p1.first << ", " << p1.second << ")\n";
 
    // build a pair from a reference to int and an array (decayed to pointer)
    auto p2 = std::make_pair(std::ref(n), a);
    n = 7;
    std::cout << "The value of p2 is "
              << "(" << p2.first << ", " << *(p2.second + 2) << ")\n";
}

Operation results:

The value of p1 is (1, 2)
The value of p2 is (7, 3)

Take another look at tuple:

#include <iostream>
#include <tuple>
#include <functional>
 
std::tuple<int, int> f() // this function returns multiple values
{
    int x = 5;
    return std::make_tuple(x, 7); // return {x,7}; in C++17
}
 
int main()
{
    // heterogeneous tuple construction
    //std::tuple<int, int>{1, -1}; // initializer list
    int n = 1;
    auto t = std::make_tuple(10, "Test", 3.14, std::ref(n), n);
    n = 7;
    std::cout << "The value of t is "  << "("
              << std::get<0>(t) << ", " << std::get<1>(t) << ", "
              << std::get<2>(t) << ", " << std::get<3>(t) << ", "
              << std::get<4>(t) << ")\n";
 
    // function returning multiple values
    int a, b;
    std::tie(a, b) = f();
    std::cout << a << " " << b << "\n";
}

Operation results:

The value of t is (10, Test, 3.14, 7, 1)
5 7
  • Related routines are from cppreference.com

4, Summary

These two data structures are very simple applications. Traditional programmers usually encapsulate some data structures to achieve similar functions. However, the things implemented in STL are more or less reviewed by many big cattle. It is acceptable to take them out and do something. In the same sentence, we should be good at learning and not be complacent. This is the driving force and source of progress.

Topics: C++ C++11