C++是否提供"三重"模板,与<T1,T2>对相比?

use*_*317 12 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;
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 30

您可能正在寻找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;
Run Code Online (Sandbox Code Playgroud)

  • 我相信 `std::tuple` 有一个巨大的缺点!无法通过索引访问。如果所有类型都相同,那么使用“std::array&lt;3&gt;”可能会更好。 (3认同)

4pi*_*ie0 10

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

#include <tuple>
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读:

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;
Run Code Online (Sandbox Code Playgroud)


Paw*_*arz 2

不,没有。

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