小编Mar*_*rKS的帖子

通过 boost 属性树发出 YAML 迭代(递归)

我有一个小但复杂的树结构。使用 boost 属性树作为容器,我尝试迭代该树,然后使用yaml-cpp库将其发送到 yaml 文件。

例如,我有一个小的嵌套属性树:

fibonacci:
  type: series
  entities:
    golden_ratio:
      ratio: 2.3
    function:
      power_series: 2
Run Code Online (Sandbox Code Playgroud)

我希望我的 yaml 文件看起来像这样。

我编写了一个递归函数来迭代树并发送到 yaml。

//Member variable
YAML::Emitter m_out

void iterator(const boost::property_tree::ptree& tree, const std::string& key)
{
    for (const auto& item: tree)
    {
        if (item.second.data().empty()) //check if map node
        {
            m_out << YAML::BeginMap;
            m_out << YAML::Key << item.first;
        }
        else if (!item.second.data().empty()) //else it is key/value pair
        {
            m_out << YAML::Key << item.first;
            m_out << YAML::Value << item.second.data();
        }
        if (!item.second.empty()) //If …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-propertytree yaml-cpp

2
推荐指数
1
解决办法
2545
查看次数

字符和int连接何时导致seg错误?

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int number = 900;
    cout << std::string("Hello, World!" + number) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我知道正确的解决方案是使用std::to_string(number).但问题是,为什么我没有得到分段错误?我甚至通过valgrind来运行它.字符串长度约为13,我移动指针900.

c++

1
推荐指数
1
解决办法
52
查看次数

标签 统计

c++ ×2

boost ×1

boost-propertytree ×1

yaml-cpp ×1