Loading...

C++ Iterating

The set page left one line on the screen in box.cpp: 1, after words.insert("pine"); and printing words.size(). This page stays in the Memory and Collections module on purpose. The workshop file on disk stays box.cpp so you keep one familiar build path. The new work walks every item in the set with a range-based for loop and prints each string. The screen shows one line: pine.
A range-based for loop visits each item in a collection without writing an index by hand. On this page you still write std::set<std::string> words; and words.insert("pine");, then you write for (const std::string& w : words) and print w inside the loop body. Reading the loop header is reading “for each string w in words.” Reading std::cout << w is reading the current item on that pass. After one insert, the loop runs once and prints pine.
Programs use range-based for when you want every item without managing begin and end iterators yourself. You do not keep printing only size() as the lesson path on this page. Extra tools like auto wait for the next page on purpose. Here the point is only inserting pine and walking the set to print each value. The build command stays familiar: one source file box.cpp, one output name box, one run that prints pine. Keep #include <iostream>, #include <string>, and #include <set> together at the top. You compile only box.cpp again for this workshop file on disk.
Think of the loop as a careful walk past each shelf in the set. After one insert("pine"), there is one shelf to visit. The loop binds that item to w and prints it. The previous page proved the count was 1. This page proves the stored string is pine by printing it during the walk. Both facts stay true for the same set at the same time.

Inside main, write std::set<std::string> words;, then words.insert("pine");, then a range-based for that prints w, then return 0;. Keep the three includes at the top of the file. Do not print only words.size() as the lesson path on this page. Do not leave the set empty if you want the checked screen of pine.
Walk the run carefully from the first line of main. The program starts at main. words starts empty. insert("pine") adds one unique string. The for loop takes that one item as w and prints it. Then main returns and the program ends. There is one line on the screen. It is pine, not 1. The size is still one; this page chooses to show the item. The checked file always inserts before it walks the set.
Say the steps out loud once while you read the file. Empty set. Insert pine. For each w, print w. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No second insert. No size print as the lesson line. No extra include beyond the three you already keep. When the screen shows pine, the story matched the source.

If you run the loop before any insert, the screen stays empty. That experiment can stay in the prose. The checked file always inserts pine first so the screen is pine.
If you forget the colon form and try an old index loop on a set, the lesson path breaks. The checked file always writes for (const std::string& w : words).
A common mistake is to keep printing only size() and never walk the items. Another common mistake is to print a literal "pine" and never use w. A third mistake is to leave the old size-only body and never add the loop. A fourth mistake is to drop #include <set> while still using std::set. A fifth mistake is to omit the braces or the print line inside the loop body.
This page stops before the next tool, where auto lets the compiler pick a type. The next page names that tool once at the end. The loop you have just written only shows walking one inserted string in the same function. Keep the insert before the loop so the order stays clear for a first careful reading of the file.
Remember that the loop does not insert pine by itself. You still call insert first. After one insert the walk prints pine once. After you undo an oak experiment the checked file returns to one pine print. After you restore the checked loop body the screen returns to that same single word. Stability of the checked file matters as much as learning the walk itself.

Open box.cpp. Keep the set includes and the words variable. Keep insert("pine"). Change the print from words.size() to a range-based for that prints each w. The file name on disk stays box.cpp. The checked file is below, and the new lines are the loop header and the print of w.

box.cpp walks the set and prints each w

cpp
#include <iostream>
#include <string>
#include <set>

int main()
{
    std::set<std::string> words;
    words.insert("pine");
    for (const std::string& w : words)
    {
        std::cout << w << std::endl;
    }
    return 0;
}

Rebuild with g++ -std=c++17 box.cpp -o box, then run ./box. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 box.cpp -o box. The screen shows one line: pine.
Check that line first on the screen. The set held pine after insert, so the loop printed pine. The table gives each row its piece, so compare your run with it line by line before you move on to experiments. If the screen is empty, the insert may be missing. If the screen shows 1, you still print size() instead of walking items. If the build fails about the loop, check the colon form for (const std::string& w : words).

📊 iterating and what the screen shows

Piece What the screen shows
First pine, because the loop prints w after insert added pine
Last pine, because the program prints one line and stops
Rule range-for visits each item; w holds the current string from the set
Screen pine

Try one change at a time. Add words.insert("oak"); after the pine insert, leave the loop as it is, and rebuild. The screen shows two lines, pine then oak in set order. Remove the oak insert so the file returns to one item. Next, temporarily print words.size() again instead of the loop to see 1, then restore the range-for so the checked screen is pine again. Each experiment changes one thing on purpose, so the screen shows either the walk or the count. Undo each change before the next one starts, so the file always returns to the checked workshop version with one insert("pine") and the range-for print of w once again after every single careful try back on disk again.
This page ends the iterating tool and stops before the next one. The next page is auto, which lets the compiler choose a type for you. The loop you have just written only shows walking pine in box.cpp. You leave with one line on the screen: pine. Count the lines before you leave. There is exactly one printed line on that final screen, and that single printed line on the screen is still just pine alone right there now after the final successful build run on disk today now after the range-for walk.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top