为什么要插入?(问号)就在地图输入之前

Why is it inserting a ? (question mark) right before Map entry?

本文关键字:地图 输入 插入 问号 为什么      更新时间:2023-10-16

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 结构好吗?还是我应该修改为其他内容?谢谢。

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

    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::KeyYAML::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 。由于密钥是映射,因此它必须是长格式密钥,该密钥是用该额外?指定的。

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