yaml-cpp Easiest way to iterate through a map with undefined values

夙愿已清 提交于 2019-11-30 06:51:56

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.)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!