2017-02-09 19 views
1

我有一些std ::向量的值,例如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年如何創建一個std ::元組,其成員containins通過索引的元組指定的載體?

編輯:澄清指標的元組

+1

那你試試?其實我沒有看到問題。簡單地遍歷元組並將相應的元素添加到輸出元組 – user463035818

+1

btw爲什麼當所有元素都是'string' /所有整數時都使用'tuple'? – user463035818

回答

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>()); 
} 

Demo

相關問題