Search code examples
c++yaml-cpp

How can I decode a list of lists?


I have the following YAML file I need to decode in Yaml-CPP

WorldMatrix:
- [0.9951964247911349, 0.018388246064889716, -0.09615585520185603, -0.5403611888912607]
- [0.0668777651703494, 0.5895969306048771, 0.8049241106757379, 0.49102218903854067]
- [0.0714943396973693, -0.8074882858766219, 0.5855349926035782, 3.057906332726323]
- [0.0, 0.0, 0.0, 1.0]

I have gotten as far as such, but I can't figure out how to continue:

YAML::Node config = YAML::LoadFile(path);
for(YAML::const_iterator it=config.begin(); it != config.end(); ++it){


}

Solution

  • If you want to store it using std::vector, there's a shortcut:

    YAML::Node config = YAML::LoadFile(path);
    std::vector<std::vector<double>> worldMatrix =
        config["WorldMatrix"].as<std::vector<std::vector<double>>>();
    

    If you simply want to iterate over it and do whatever you like:

    for (YAML::Node row : config["WorldMatrix"]) {
      for (YAML::Node col : row) {
        double value = col.as<double>();
        // do something with value
      }
    }