首页 > 解决方案 > 对 YAML::Node 使用结构绑定

问题描述

当我们想要遍历 YAML::Node 时可以使用结构绑定吗?

当前代码:

for(auto it = node.begin(); it != node.end(); ++it)
{
  auto a = it->first.as<std::string>();
  auto b = it->second;
  // Some code bellow
}

基于范围的 for 循环也可以正常工作:

for(const auto& n : node) {
  auto a = n.first.as<std::string>();
  auto b = n.second;
  // Some code bellow
}

我希望得到类似的东西:

for(auto [a,b] : node)
{
  // Some code bellow
}

这可能吗?如何将结构绑定与 YAML::Node 结合使用?更改的原因是代码更具可读性。这种类型的代码在多个地方使用,结构绑定是实现更漂亮的解决方案的好方法。

标签: c++yaml-cpp

解决方案


推荐阅读