首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >用yaml-cpp读取映射向量失败

用yaml-cpp读取映射向量失败
EN

Stack Overflow用户
提问于 2022-03-27 23:06:01
回答 1查看 383关注 0票数 3

我想阅读以下yaml数据:

代码语言:javascript
复制
camera:
 response_values: [[0.0: 0.0, 1.0: 1.1, 245.0: 250.1], [0.0: 0.1, 1.0: 1.3, 200.0: 250], [0.0: 0.0, 1.0: 1.1, 245.0: 250.1]]

我想把它读到vector < map <float, float> >里。在这种情况下,向量将有3张地图,每3个条目。

我的尝试是:

代码语言:javascript
复制
#include <yaml-cpp/yaml.h>
#include <fstream>
#include <map>

using namespace std;

int main()
{

  YAML::Node camerafile = YAML::LoadFile("/path/to/camera.yaml");

  YAML::Node camera = camerafile["camera"];

  auto response_values_yaml = camera["response_values"];
  for(YAML::const_iterator it=response_values_yaml.begin();it!=response_values_yaml.end();++it) {
    YAML::Node response_values_map = it->as<YAML::Node>(); // e.g. [0.0: 0.0, 1.0: 1.1, 245.0: 250.1]
    string whole_map = YAML::Dump(response_values_map);

    for(YAML::const_iterator at=response_values_map.begin();at!=response_values_map.end();++at) {
      auto the_thing = at->as<YAML::Node>();
      string the_string = YAML::Dump(the_thing);
      float key = at->first.as<float>();
      float val = at->second.as<float>();
    }
  }
}

调试结果:

whole_map"[{0.0: 0.0}, {1.0: 1.1}, {245.0: 250.1}]"

the_string"{0.0: 0.0}"

the_thing->Node->Ref->Data有一个映射项

然而,一旦程序到达float key = at->first.as<float>();,它就会崩溃。

代码语言:javascript
复制
terminate called after throwing an instance of 'YAML::InvalidNode'
  what():  invalid node; this may result from using a map iterator as a sequence iterator, or vice-versa

GDB跳入yaml-cpp/node/imlp.hthis是一个空节点,m_isValidfalse

尝试as<string>会产生同样的行为。我认为我有一个映射,但我不能用.first.second方法将它解释为一个映射,我认为这就是错误消息告诉我的。我遗漏了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-23 13:51:21

代码语言:javascript
复制
#include <assert.h>
#include <fstream>
#include <iostream>
#include <map>
#include <yaml-cpp/yaml.h>

using namespace std;

int main() {

  YAML::Node camerafile = YAML::LoadFile("./camera.yaml");

  YAML::Node camera = camerafile["camera"];

  auto response_values_yaml = camera["response_values"];
  // it is the sequence iterator
  for (YAML::const_iterator it = response_values_yaml.begin();
       it != response_values_yaml.end(); ++it) {
    YAML::Node response_values_map
        = it->as<YAML::Node>(); // e.g. [0.0: 0.0, 1.0: 1.1, 245.0: 250.1]

    for (YAML::const_iterator at = response_values_map.begin();
         at != response_values_map.end(); ++at) {


      // `at` is a **map iterater** which has a single key/value pair.
      // so it will certainly coredump when you are calling `at->first.as<float>()`
      assert(at->size() == 1);
      // The iterator points to the single key/value pair.
      YAML::Node::const_iterator sub_it = at->begin(); // **The key point.**
      // Then access the key and value.
      float key = sub_it->first.as<float>();
      float value = sub_it->second.as<float>();
      std::cout << " key: " << key << ", val: " << value << '\n';


    }
  }
}

输出将如下所示:

代码语言:javascript
复制
 key: 0, val: 0
 key: 1, val: 1.1
 key: 245, val: 250.1
 key: 0, val: 0.1
 key: 1, val: 1.3
 key: 200, val: 250
 key: 0, val: 0
 key: 1, val: 1.1
 key: 245, val: 250.1
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71640946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档