Add range based for loop to tutorial

Open
#1,121 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
Under an hour
Newbie friendliness
55/100
Issue type
Documentation
Clarity
Clearly specified
Activity status
Stale
Tech stack
cpp
Domain
documentation

Research direction

The issue points to the Basic Parsing and Node Editing section of the parsing tutorial. Read that section and add a C++11 range-based for example alongside the existing collection iteration examples. Done means the tutorial includes the proposed example with corrected syntax and it matches the surrounding examples.

Written by the indexing model from the issue text.

Description

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

Dominant language
C++
Stars
6.1k
Forks
2.2k
Avg merge
22h 2m
Merged PRs (30d)
1

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from jbeder/yaml-cpp

All issues in jbeder/yaml-cpp

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.