1

Yaml-cpp 似乎在我的地图条目之前插入了一个问号。这是结果:

baryons:
  nucleons:
    - proton:
        quarkContent: [u, u, d]
        charge: 1
    - antiproton:
        quarkContent: [au, au, ad]
        charge: -1
    - neutron:
        quarkContent: [d, d, u]
        charge: 0
    - antineutron:
        quarkContent: [ad, ad, au]
        charge: 0
? hyperons:
    - lambda:
        quarkContent: [u, d, s]
        charge: 0
    - antilambda:
        quarkContent: [au, ad, as]
        charge: 0

如您所见,从超子开始,有一个问号。我尝试添加更多条目以查看会发生什么。它似乎在?和之间交替:。这很奇怪。

我尝试删除核子键和超子键的所有内容,我仍然得到这个

baryons:
  nucleons:
    - Hello
? hyperons:
    - Hello

嗯。确实很奇怪。这是我的代码。原谅凌乱

#include "yaml-cpp/yaml.h"
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <typeinfo>
using namespace YAML;

void addParticle(Emitter * e, std::string name, std::string qc1, std::string qc2, std::string qc3, int charge){
    *e << BeginMap << Key << name;
    *e << Value;
        *e << BeginMap;
        *e << Key << "quarkContent" << Value << Flow << BeginSeq << qc1 << qc2 << qc3 << EndSeq;
        *e << Key << "charge" << Value << charge;
        *e << EndMap;
    *e << EndMap;

    *e << BeginMap << Key << "anti" + name;
    *e << Value;
        *e << BeginMap;
        *e << Key << "quarkContent" << Value << Flow << BeginSeq << "a" + qc1 << "a" + qc2 << "a" + qc3 << EndSeq;
        *e << Key << "charge" << Value << charge * -1;
        *e << EndMap;
    *e << EndMap;
};

int main(){
    Emitter e;

    e << BeginMap;
    e << Key << "baryons";
    e << Value;

        e << BeginMap;
        e << Key << "nucleons";
        e << Value;
            e << BeginSeq;
            addParticle(&e, "proton", "u", "u", "d", 1);
            addParticle(&e, "neutron", "d", "d", "u", 0);
            e << EndSeq;
        e << EndMap;

        e << BeginMap;
        e << Key << "hyperons";
        e << Value;
            e << BeginSeq;
            addParticle(&e, "lambda", "u", "d", "s", 0);
            e << EndSeq;
        e << EndMap;

    e << EndMap;

    std::ofstream particlesYAML;
    particlesYAML.open("particles.yml");
    particlesYAML.write(e.c_str(), strlen(e.c_str()));
    particlesYAML.close();
    return 0;
};

在旁注中,我正在缩进以减少混乱。所以也原谅。

另外,我的 YAML 结构好吗?还是我应该修改一些不同的东西?谢谢。

4

1 回答 1

1

当您真的想要一个具有两个键/值对的单个映射时,您正在编写两个连续的映射作为单个节点。具体来说(这是您的代码片段):

    e << BeginMap;
    e << Key << "nucleons";
    e << Value;
        e << BeginSeq;
        addParticle(&e, "proton", "u", "u", "d", 1);
        addParticle(&e, "neutron", "d", "d", "u", 0);
        e << EndSeq;
    // you previously had a EndMap and BeginMap
    e << Key << "hyperons";
    e << Value;
        e << BeginSeq;
        addParticle(&e, "lambda", "u", "d", "s", 0);
        e << EndSeq;
    e << EndMap;

yaml-cpp 之所以给你额外的?,而不是仅仅给出一个错误,是因为你实际上不需要提供YAML::Keyor YAML::Value。您的代码如下所示:

e << YAML::BeginMap;
e << YAML::Key << "foo";  // specifies the key
e << YAML::Value;
e << YAML::BeginMap;      // specifies the value
// stuff
e << YAML::EndMap;
e << YAML::BeginMap;      // specifies a new key (the YAML::Key is implied)
// stuff
e << YAML::EndMap;
                          // no value is provided for that key
e << YAML::EndMap;

所以你有一个包含两个键/值对的映射。第一个是您所期望的(键是foo,值是地图)。然而,对于第二个,是一个映射并且没有提供值,所以它被解释为null. 由于键是一个映射,它必须是一个长格式的键,用那个 extra 指定?

旁注:有一个e.size()可以为您提供输出字符串的长度,因此您无需使用strlen.

于 2013-01-27T23:24:19.797 回答