我有以下YAML:
Configuracion:
vel_personaje: 3
merge_scroll: 30
Tipos:
nombre: arbol
imagen: img/tree
ancho_base: 2
alto_base: 2
pixel_ref_x: 30
pixel_ref_y: 40
fps: 10
delay: 5
nombre: casa
imagen: img/house
nombre: auto
imagen: img/tree
ancho_base: 2
alto_base: 2问题是,我可以有多少"Tipos“,但有时他们有8个参数,有时只有2个,或两者之间的任何数字。我想弄清楚如何用yaml-cpp来读取这些值,但我做不到。我试过以下几种方法,但没有运气。
while (contador < tipos_size){
try {
name = tipos["nombre"].as<string>();
contador++;
} catch (YAML::Exception& yamlException) {
name = "pepe";
}
try {
imagen = tipos["imagen"].as<string>();
contador++;
} catch (YAML::Exception& yamlException) {
imagen = "img/def";
}
try {
ancho_base = tipos["ancho_base"].as<int>();
contador++;
} catch (YAML::Exception& yamlException) {
ancho_base = 1;
}
try {
alto_base = tipos["alto_base"].as<int>();
contador++;
} catch (YAML::Exception& yamlException) {
alto_base = 1;
}
try {
pixel_ref_x = tipos["pixel_ref_x"].as<int>();
contador++;
} catch (YAML::Exception& yamlException) {
pixel_ref_x = 10;
}
try {
pixel_ref_y = tipos["pixel_ref_y"].as<int>();
contador++;
} catch (YAML::Exception& yamlException) {
pixel_ref_y = 10;
}
try {
fps = tipos["fps"].as<int>();
contador++;
} catch (YAML::Exception& yamlException) {
fps = 24;
}
try {
delay = tipos["delay"].as<int>();
contador++;
} catch (YAML::Exception& yamlException) {
delay = 100;
}我会感谢你的帮助。谢谢!
编辑代码:
for (YAML::Node tipo : tipos) {
try {
name = tipo["nombre"].as<string>();
} catch (YAML::Exception& yamlException) {
name = "pepe";
}发布于 2015-09-09 23:49:09
看起来,您希望Tipos是一个映射序列,而不是一个映射:
Configuracion:
vel_personaje: 3
merge_scroll: 30
Tipos:
- nombre: arbol
imagen: img/tree
ancho_base: 2
alto_base: 2
pixel_ref_x: 30
pixel_ref_y: 40
fps: 10
delay: 5
- nombre: casa
imagen: img/house
- nombre: auto
imagen: img/tree
ancho_base: 2
alto_base: 2您可以迭代如下:
for (YAML::Node tipo : tipos) {
// handle tipo
}https://stackoverflow.com/questions/32490418
复制相似问题