gpt4 book ai didi

c++ - 如何创建一个 std::tuple,其中包含来自索引元组指定的 vector 中的成员?

转载 作者:行者123 更新时间:2023-11-30 00:45:44 27 4
gpt4 key购买 nike

我有一些值的 std::vector,例如 std::vector<std::string> v = {"a", "b", "c", "d", "e"};然后我有一个从参数包创建的数字元组(即它可以有任何大小)。如何创建 vector 成员的元组(这里是 std::string 的元组),它使用索引元组作为 vector 的索引?例如,如果索引元组的值为 0、3、1,那么结果应该与我写的相同 auto result = std::make_tuple("a", "d", "b"); .我正在使用 Visual Studio 2015。

编辑:澄清索引元组

最佳答案

也许是这样的:

template <typename C, typename Tuple, int... Is>
auto project_impl(const C& c, const Tuple& indices,
std::integer_sequence<int, Is...>) {
return std::make_tuple(c[std::get<Is>(indices)]...);
}

template <typename C, typename Tuple>
auto project(const C& c, const Tuple& indices) {
return project_impl(c, indices,
std::make_integer_sequence<int, std::tuple_size<Tuple>::value>());
}

Demo

关于c++ - 如何创建一个 std::tuple,其中包含来自索引元组指定的 vector 中的成员?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42138790/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com