为什么要插入?(问号)就在地图输入之前
Why is it inserting a ? (question mark) right before Map entry?
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::Key
或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
。由于密钥是映射,因此它必须是长格式密钥,该密钥是用该额外?
指定的。
旁注:有一个e.size()
可以为您提供输出字符串的长度,因此您无需使用 strlen
。
相关文章:
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 2D数组来自文本输入,中间有空格
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 检查输入是否不是整数或数字
- 正在尝试了解输入验证循环
- 读取文件并输入到矢量中
- 为什么不;名字在地图上是按顺序排列的吗
- C++如何通过用户输入删除列表元素
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 地图插入将替换以前输入的值
- 如何传递私有地图和输入数据
- 如何从文件中读取输入并在C++中配对到地图中
- 我的使用地图的程序在第一次输入后崩溃
- 使用地图来计数字符串输入
- 输入地图值的标准输入
- 我不知道如何正确地将数据输入到地图中
- 如何使用地图或矢量进行C++记忆,用于任意数量的输入
- 为什么要插入?(问号)就在地图输入之前