1

クラス(いくつかのスカラー値とfloatのベクトルを含む)があり、別のマップの値としてインスタンスを読み書きしたいと思います。

    
// 書きます

 out << YAML :: Key << "my_queue" << YAML :: Value << my_queue;



//読み取ります(他のコードが切り取られます...)
 for(YAML :: Iterator it = doc.begin(); it!= doc.end(); ++ it)
  {{
    std :: string key、value;
    it.first()>>キー;
    it.second()>>値;
    if(key.compare( "my_queue")== 0){
      * it >> my_queue;
    }
 }

このクラスを書くことは完璧に機能しますが、私が何をしてもそれを読むことができないようです。それはInvalidScalarを投げ続けます。

キャッチされたYAML::InvalidScalar yaml-cpp:行20、列13でエラー:無効なスカラー

これは、出力(エラーを報告せずにyaml-cppで記述されたもの)が次のようになることです。

その他の番号:80
my_queue:
  サイズ:20
  データ:
    -3.5
    ---1
    --- 1.5
    -0.25
    ---24.75
    --5.75
    -2.75
    ---33.55
    -7.25
    ---11
    -15
    -37.5
    --3.75
    ---28.25
    -18.5
    -14.25
    ---36.5
    --6.75
    --- 0.75
    --14
  max_size:20
  平均:-0.0355586
  stdev:34.8981
even_more_data:1277150400

ドキュメントには、これがサポートされている使用法、ネストされたマップ、この場合は値の1つとしてシーケンスがあると記載されているようです。それはInvalidScalarであると不平を言いますが、私が最初に行うことは、これがマップであることを示しています。

YAML :: Emitter&operator <<(YAML :: Emitter&out、const MeanStd&w)
{{
  out << YAML :: BeginMap;
  out << YAML :: Key << "size";
  out << YAML :: Value << w.size();

  out << YAML :: Key << "data";
  out << YAML :: Value << YAML :: BeginSeq;
  for(Noor :: Number i = 0; i <w.size(); ++ i){
    out << w [i];
  }
  out << YAML :: EndSeq;
  out << YAML :: Key << "max_size";
  out << YAML :: Value << w.get_max_size();
  out << YAML :: Key << "mean";
  out << YAML :: Value << w.mean();
  out << YAML :: Key << "stdev";
  out << YAML :: Value << w.stdev();

  out << YAML :: EndMap;
  戻る;
}

誰かがこれに問題があると思いますか?

4

2 に答える 2

1

YAMLを読んでいるとき:

std::string key, value;
it.first() >> key;
it.second() >> value; // ***
if (key.compare("my_queue") == 0) {
  *it >> my_queue;
}

マークされた行は、キーと値のペアの値をスカラー(std::stringとして読み取ろうとします。そのため、無効なスカラーであることが通知されます。代わりに、次のようにします。

std::string key, value;
it.first() >> key;
if (key.compare("my_queue") == 0) {
  it.second() >> my_queue;
} else {
  // ...
  // for example: it.second() >> value;
}
于 2010-06-28T20:04:15.420 に答える
0
YAML::Node internalconfig_yaml = YAML::LoadFile(configFileName);
const YAML::Node &node = internalconfig_yaml["config"];
for(const auto& it : node )
{
    std::cout << "\nnested Key: " << it.first.as<std::string>() << "\n";
    if (it.second.Type() == YAML::NodeType::Scalar)
    {
        std::cout << "\nnested value: " << std::to_string(it.second.as<int>()) << "\n";
    }
    if (it.second.Type() == YAML::NodeType::Sequence)
    {
        std::vector<std::string> temp_vect;
        const YAML::Node &nestd_node2 = it.second;
        for(const auto& it2 : nestd_node2)
        {
            if (*it2)
            {
                std::cout << "\nnested sequence value: " << it2.as<std::string>() << "\n";
                temp_vect.push_back(it2.as<std::string>());
             }
        }
        std::ostringstream oss;
        std::copy(temp_vect.begin(), temp_vect.end(),                 
                  std::ostream_iterator<std::string>(oss, ","));
        std::cout << "\nnested sequence as string: " <<oss.str() << "\n";
    }
}
if (it2.second.Type() == YAML::NodeType::Map)
{
 // Iterate Recursively again !!
 
}
于 2021-03-08T06:37:40.393 に答える