C ++是否提供了&#34;三重&#34;模板,与<t1,t2 =“”>?</t1,>相当

时间:2014-05-13 17:19:44

标签: c++ stl std-pair

C ++是否有类似std::pair但有3个元素的东西?

例如:

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

array[1].first = 1;
array[1].second = 2;
array[1].third = 3;

4 个答案:

答案 0 :(得分:28)

您可能正在寻找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;

答案 1 :(得分:9)

类模板std::tuple是异构值的固定大小集合,自C ++ 11以来可在标准库中使用。它是std::pair的概括,并在标题

中显示
#include <tuple>

你可以在这里阅读:

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

示例:

#include <tuple>

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

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

答案 2 :(得分:1)

不,没有。

但您可以使用tuple或“双对”(pair<pair<T1,T2>,T3>)。或者 - 显然 - 自己写课(这应该不难)。

答案 3 :(得分:-2)

只有两种简单的方法可以做到这一点。 1)自己实现它。 2)获得提升并像这样使用boost :: tuple http://www.boost.org/doc/libs/1_55_0/libs/tuple/doc/tuple_users_guide.html

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;