C++ auto
The iterating page left one line on the screen in box.cpp: pine, after words.insert("pine"); and for (const std::string& w : words) printing w. 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 keeps that same walk and lets auto ask the compiler to choose the type of w. The screen still shows one line: pine.
auto tells the compiler to deduce a type from the initializer or from the range item. On this page you still write std::set<std::string> words; and words.insert("pine");, then you write for (const auto& w : words) instead of naming std::string yourself. Reading const auto& is reading a const reference whose type comes from each set item. Reading std::cout << w is still reading the current string on that pass. After one insert, the loop runs once and prints pine.
Programs use auto when the type is already clear from the collection and you want a shorter loop header. You do not drop the set or the insert on this page. Extra tools like exceptions wait for the next page on purpose. Here the point is only swapping the loop variable to auto while the walk still prints pine. 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 auto as a short note that says “use the item type already stored in the set.” The set holds strings, so w becomes a const reference to a string. The previous page wrote that type by hand. This page lets the compiler write it. The screen line is still pine, because the walk did not change—only the way the loop names the item type changed.
Inside main, write std::set<std::string> words;, then words.insert("pine");, then for (const auto& w : words) 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 deduces the item type with auto, binds that 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 checked file always inserts before it walks the set with auto.
Say the steps out loud once while you read the file. Empty set. Insert pine. For each auto w, print w. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No second insert. No hand-written std::string in the loop header. 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 & and write const auto w by value, the program can still print pine for this tiny set, but the checked file uses const auto& on purpose. Keep that form in the workshop body.
A common mistake is to keep writing const std::string& and never try auto. Another common mistake is to print a literal "pine" and never use w. A third mistake is to drop the loop and print only size(). A fourth mistake is to drop #include <set> while still using std::set. A fifth mistake is to omit the print line inside the loop body.
This page stops before the next tool, where errors become exceptions you can catch. The next page names that tool once at the end. The auto loop you have just written only shows type deduction while walking one inserted string. Keep the insert before the loop so the order stays clear for a first careful reading of the file.
Remember that auto 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 auto itself.
Open box.cpp. Keep the set includes, the words variable, and insert("pine"). Change the loop header from const std::string& w to const auto& w. The file name on disk stays box.cpp. The checked file is below, and the new line is the auto loop header.
#include <iostream>
#include <string>
#include <set>
int main()
{
std::set<std::string> words;
words.insert("pine");
for (const auto& 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 auto 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 auto, check the form for (const auto& w : words).
📊 auto and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | pine, because the auto loop prints w after insert added pine |
| Last | pine, because the program prints one line and stops |
| Rule | auto deduces the item type; const auto& w walks each string in the set |
| Screen | pine |
Try one change at a time. Temporarily switch the header back to const std::string& w, rebuild, and confirm the screen is still pine. Restore const auto& w. Next, add words.insert("oak"); after the pine insert and rebuild so two lines appear, then remove oak so the checked screen is pine again. Each experiment changes one thing on purpose, so the screen shows either the same walk with a hand-written type or a longer walk. Undo each change before the next one starts, so the file always returns to the checked workshop version with one insert("pine") and for (const auto& w : words) once again after every single careful try back on disk again.
This page ends the auto tool and stops before the next one. The next page is exceptions, which names errors you can catch. The auto loop you have just written only shows walking pine in box.cpp with type deduction. 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 auto walk.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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