Does C ++ provide a “triple” pattern comparable to a pair of <T1, T2>?

Does C ++ have something like std :: pair , but with three elements?

For instance:

#include <triple.h>
triple<int, int, int> array[10];

array[1].first = 1;
array[1].second = 2;
array[1].third = 3;
+4
source share
4 answers

It is hard to interpret your question, but you can search std::tuple:

#include <tuple>

....

std::tuple<int, int, int> tpl;

std::get<0>(tpl) = 1;
std::get<1>(tpl) = 2;
std::get<2>(tpl) = 3;
+16
source

The class template std::tupleis a set of heterogeneous values ​​of a fixed size, available in the standard library with C ++ 11. This generalization std::pairis presented in the header

#include <tuple>

You can read about it here:

http://en.cppreference.com/w/cpp/utility/tuple

Example:

#include <tuple>

std::tuple<int, int, int> three;

std::get<0>( three) = 0;
std::get<1>( three) = 1;
std::get<2>( three) = 2;
+5
source

, .

However, you can use tuple or double pair ( pair<pair<T1,T2>,T3>). Or - obviously - write the class itself (which should not be difficult).

+1
source

There are only 2 easy ways to get this. 1) To implement it yourself. 2) get a boost and use boost :: tuple http://www.boost.org/doc/libs/1_55_0/libs/tuple/doc/tuple_users_guide.html like this

double d = 2.7; A a;
tuple<int, double&, const A&> t(1, d, a);
const tuple<int, double&, const A&> ct = t;
  ...
int i = get<0>(t); i = t.get<0>();       
int j = get<0>(ct);                      
get<0>(t) = 5;                           
0
source

Source: https://habr.com/ru/post/1540414/


All Articles