Reputation: 1109
I have a std::vector of some values, for example std::vector<std::string> v = {"a", "b", "c", "d", "e"};
and then I have a tuple of numbers created from a parameter pack (i.e. it may have any size). How can I create a tuple of the vector members (tuple of std::string here) which uses the indexes tuple as indexes to the vector? For example if the tuple of indexes has values 0, 3, 1, then the result should be same as I wrote auto result = std::make_tuple("a", "d", "b");
. I am using Visual Studio 2015.
edit: clarified the tuple of indexes
Upvotes: 1
Views: 112
Reputation: 52621
Something along these lines, perhaps:
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>());
}
Upvotes: 6