如何创建一个std :: tuple,它包含由索引元组指定的向量中的成员?

Mar*_*nik 1 c++ stl variadic-templates

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

编辑:澄清了索引的元组

Igo*_*nik 6

沿着这些方向,也许是:

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

演示