[英]Insert value to a std::map from a tuple
I am just learning data structure of graph.And i'm trapped in such a situation. 我只是在学习图形的数据结构。我被困在这种情况下。
I have written my Graph
class like 我写过我的
Graph
类
template <char... Args>
class Graph{};
where the Args
of type char
means the vertex of my Graph
. 其中
char
类型的Args
表示我的Graph
的顶点。 However, when i want to search in my Graph,i need to insert each vertex of char
and its index in Args
as a std::pair<char,size_t>
into a std::map<char,size_t>
.What i have done is that i constructed a std::tuple
like 但是,当我想在我的Graph中搜索时,我需要将每个
char
顶点及其在Args
中的索引作为std::pair<char,size_t>
插入到std::map<char,size_t>
我有什么完成是我构建了一个std::tuple
类的
std::tuple<decltype(Args)...> t(Args...);
Then i want to do like this 然后我想这样做
for(size_t i =0;i<sizeof...(Args);++i)
Map.insert({0,std::get<i>(t)});
which Map means a std::map<size_t,char>
. 哪个Map表示
std::map<size_t,char>
。 It certainly doesn't work because the i
used in std::get<>
is not a constexpr
. 它当然不起作用,因为
i
在std::get<>
中使用的不是constexpr
。 What can i do now is to insert into the Map one by one like 我现在能做的是逐个插入地图
Map.insert({0,std::get<0>(t)});
Map.insert({1,std::get<1>(t)});
Map.insert({2,std::get<2>(t)});
But it is not the result i want.So are there any other solutions for me? 但这不是我想要的结果。那么对我来说还有其他解决方案吗?
Thanks for any help! 谢谢你的帮助!
std::map<char,size_t>
or std::map<size_t,char>
? std::map<char,size_t>
或std::map<size_t,char>
?
I'll go with std::map<size_t,char>
. 我将使用
std::map<size_t,char>
。
You need C++14's std::index_sequence 1 . 你需要C ++ 14的std :: index_sequence 1 。
template <char... Chars, std::size_t... Ints>
void fill_map(std::map<size_t, char> & your_map, std::index_sequence<Ints...>) {
using swallow = int[];
(void)swallow{0, (your_map.emplace(Ints, Chars), 0)... };
}
template <char... Chars>
void fill_map(std::map<size_t, char> & your_map) {
fill_map<Chars...>(your_map, std::make_index_sequence<sizeof...(Chars)>{});
}
use: 采用:
std::map<size_t,char> your_map;
fill_map<Args...>(your_map);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.