这应该是显而易见的,是否有一些简短的语法来获得一个子组?
类似的东西:
std::tuple<std::string, double, int> myTuple;
std::tuple<std::string, int> subTuple = std::get<std::string, int>(myTuple);
答案 0 :(得分:5)
你需要创建自己的模板函数,下面的实现需要C ++ 14(因为它需要std::get<T>
,你可以自己在C ++ 11中实现它):
template<typename... R, typename ...Args>
std::tuple<R...> sub_tuple(const std::tuple<Args...>& original) {
return std::make_tuple(std::get<R>(original)...);
}
int main()
{
std::tuple<std::string, double, int> myTuple = std::make_tuple("Hello", 1201.0, 51);
std::tuple<std::string, int> subTuple = sub_tuple<std::string, int>(myTuple);
std::cout << std::get<0>(subTuple) << " " << std::get<1>(subTuple);
}
注意:这将为原始元组中的每个元素创建一个副本
答案 1 :(得分:0)
您可以使用std::tie
(reference):
std::tuple<std::string, double, int> myTuple = ...;
std::string a;
int b;
std::tie(a, std::ignore, b) = myTuple;