yaml-cpp最简单的方法来迭代具有未定义值的映射 [英] yaml-cpp Easiest way to iterate through a map with undefined values
问题描述
我的YAML看起来像这样:
characterType:
type1:
attribute1:something
attribute2:something
type2:
attribute1:something
attribute2:something
我不知道有多少类型这些键的名称将是什么。这就是为什么我试图迭代地图。
struct CharacterType {
std :: string attribute1;
std :: string attribute2;
};
命名空间YAML {
模板<>
struct convert< CharacterType> {
static bool decode(const Node& node,CharacterType& cType){
cType.attribute1 = node [attribute1] as< std :: string& ();
cType.attribute2 = node [attribute2]。as< std :: string>();
return true;
}
};
}
---------------------
std :: vector< CharacterType> cTypeList;
for(YAML :: const_iterator it = node [characterType]。begin(); it!= node [characterType]。end(); ++ it){
cTypeList.push_back(it-> as< CharacterType>());
}
前面的代码在编译时没有任何麻烦,我得到这个错误:
terminate抛出 YAML :: TypedBadConversion< CharacterType>
我也尝试使用子索引而不是迭代器,得到相同的错误。
我确定我做错了,我只是不能
迭代地图时,迭代器指向一个键/值对节点,而不是单个节点。例如:
YAML :: Node characterType = node [characterType];
for(YAML :: const_iterator it = characterType.begin(); it!= characterType.end(); ++ it){
std :: string key = it-> first.as< std :: string>(); //< - key
cTypeList.push_back(it-> second.as< CharacterType>()); // < - value
}
你的代码编译,即使你的节点是一个map节点, YAML :: Node
是有效的动态类型,所以它的迭代器必须(静态)序列迭代器和映射迭代器。)
I'd like to obtain every node in a map without knowing the keys.
My YAML looks like this:
characterType :
type1 :
attribute1 : something
attribute2 : something
type2 :
attribute1 : something
attribute2 : something
I don't know how many "type"s will be declared or what the name of those keys will be. That's why I'm trying to iterate through the map.
struct CharacterType{
std::string attribute1;
std::string attribute2;
};
namespace YAML{
template<>
struct convert<CharacterType>{
static bool decode(const Node& node, CharacterType& cType){
cType.attribute1 = node["attribute1"].as<std::string>();
cType.attribute2 = node["attribute2"].as<std::string>();
return true;
}
};
}
---------------------
std::vector<CharacterType> cTypeList;
for(YAML::const_iterator it=node["characterType"].begin(); it != node["characterType"].end(); ++it){
cTypeList.push_back(it->as<CharacterType>());
}
The previous code doesn't give any trouble when compiling but then at execution time I get this error:
terminate called after throwing an instance of YAML::TypedBadConversion<CharacterType>
I've also tried using a subindex instead of the iterator, getting the same error.
I'm sure I'm doing something wrong, I just can't see it.
When you iterate through a map, the iterator points to a key/value pair of nodes, not a single node. For example:
YAML::Node characterType = node["characterType"];
for(YAML::const_iterator it=characterType.begin();it != characterType.end();++it) {
std::string key = it->first.as<std::string>(); // <- key
cTypeList.push_back(it->second.as<CharacterType>()); // <- value
}
(The reason that your code compiled, even though your node is a map node, is that YAML::Node
is effectively dynamically typed, so its iterator has to act (statically) as both a sequence iterator and a map iterator.)
这篇关于yaml-cpp最简单的方法来迭代具有未定义值的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!