yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

Add range based for loop to tutorial

Open RFRIEDM-Trimble opened this issue 3 years ago • 0 comments

As per the parsing tutorial, there is an example iterating over a collection YAML node like you would on a map.

YAML::Node primes = YAML::Load("[2, 3, 5, 7, 11]");
for (std::size_t i=0;i<primes.size();i++) {
  std::cout << primes[i].as<int>() << "\n";
}
// or:
for (YAML::const_iterator it=primes.begin();it!=primes.end();++it) {
  std::cout << it->as<int>() << "\n";
}

If a user is using C++11, they can get range based for loop. It's even easier syntax than the examples give.

Proposal: Add this snipped to the tutorial

// or:
for (auto& prime : primes {
  std::cout << prime.as<int>() << "\n";
}

Reference

RFRIEDM-Trimble avatar Jul 14 '22 21:07 RFRIEDM-Trimble