yaml-cpp returns 数据的顺序是什么?
In which order yaml-cpp returns data?
我编写了以下 yaml 文件:
linear: [0.0,1.0,10.0,0.05]
linear: [1.0,0.5,5.0,0.05]
rotational: [0.0,6.28,20,0.5]
rotational: [6.28,0.0,20,0.5]
我用yaml-cpp用下面的代码解析它:
YAML::Node sequence = YAML::LoadFile(filename_);
int count = 1;
for (YAML::const_iterator it = sequence.begin(); it != sequence.end(); ++it)
{
const std::string& name = it->first.as<std::string>();
const std::vector<double>& parameters = it->second.as<std::vector<double> >();
...
如果我打印 name
和 parameters
值(按照我得到它们的顺序)输出是:
linear: [0,1,10,0.05]
rotational: [6.28,0,20,0.5]
linear: [1,0.5,5,0.05]
rotational: [0,6.28,20,0.5]
有人可以向我解释发生了什么,并建议我如何解决这个问题吗?
谢谢。
YAML映射不允许有duplicate keys,所以YAML文件实际上是非法的。 yaml-cpp这里简单宽大,不报错
此外,YAML 映射没有指定 key order,因此 yaml-cpp 只是选择在内部最方便迭代的顺序。最好假设 unspecified order 意味着 random order,也就是说,你不能依赖它。
我编写了以下 yaml 文件:
linear: [0.0,1.0,10.0,0.05]
linear: [1.0,0.5,5.0,0.05]
rotational: [0.0,6.28,20,0.5]
rotational: [6.28,0.0,20,0.5]
我用yaml-cpp用下面的代码解析它:
YAML::Node sequence = YAML::LoadFile(filename_);
int count = 1;
for (YAML::const_iterator it = sequence.begin(); it != sequence.end(); ++it)
{
const std::string& name = it->first.as<std::string>();
const std::vector<double>& parameters = it->second.as<std::vector<double> >();
...
如果我打印 name
和 parameters
值(按照我得到它们的顺序)输出是:
linear: [0,1,10,0.05]
rotational: [6.28,0,20,0.5]
linear: [1,0.5,5,0.05]
rotational: [0,6.28,20,0.5]
有人可以向我解释发生了什么,并建议我如何解决这个问题吗?
谢谢。
YAML映射不允许有duplicate keys,所以YAML文件实际上是非法的。 yaml-cpp这里简单宽大,不报错
此外,YAML 映射没有指定 key order,因此 yaml-cpp 只是选择在内部最方便迭代的顺序。最好假设 unspecified order 意味着 random order,也就是说,你不能依赖它。