C++ set
The map page left one line on the screen in box.cpp: 1, after counts["pine"] = 1; and printing counts["pine"]. 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 stores unique values in a set, then prints how many items it holds after inserting pine. The screen shows one line: 1.
A set holds unique values of one type with no separate key/value pair. On this page you write std::set<std::string> words; and then words.insert("pine");. The type inside the angle brackets is the item type for every value in that set. Then std::cout << words.size() << std::endl; prints the count of unique items now stored. Reading insert is reading an add that ignores a duplicate of the same value. Reading size() is reading how many unique items remain.
Programs use set when you need uniqueness without looking values up by a separate key. You do not keep the map and counts["pine"] path as the lesson body on this page, even though both tools live in the same module. Extra work like iterating the collection waits for the next page on purpose. Here the point is only creating the set, inserting pine once, and printing the size. The build command stays familiar: one source file box.cpp, one output name box, one run that prints 1. 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 set as a box that refuses a second copy of the same label. After one careful insert("pine"), size() answers with 1. The answer is a number, not the string pine. The map page used pine as a key. This page uses pine as the unique value itself. The screen line is still the digit 1, but now that digit comes from the set count after insert.
Inside main, write std::set<std::string> words;, then words.insert("pine");, then std::cout << words.size() << std::endl;, then return 0;. Keep the three includes at the top of the file. Do not print through a map on this page. Do not call size before any insert if you want the checked screen of 1.
Walk the run carefully from the first line of main. The program starts at main. words starts empty with size zero. insert("pine") adds one unique string, so size() becomes 1. Printing words.size() shows 1 on the screen. Then main returns and the program ends. There is one line on the screen. It is 1, not pine. The string pine is still inside the set; this page chooses to show the count. The checked file always inserts before it prints size().
Say the steps out loud once while you read the file. Empty set. Insert pine. Ask size. Print that answer. Return. That short story is the whole workshop path. Nothing else belongs in the checked body. No second insert of a different word. No map include. No extra include beyond the three you already keep. When the screen shows 1, the story matched the source.
If you print size() before any insert, the screen shows 0. That experiment can stay in the prose. The checked file always inserts pine first so the screen is 1.
If you forget #include <set>, the build fails about std::set. The checked file keeps that include next to iostream and string.
A common mistake is to keep the old map body and never add the set. Another common mistake is to insert pine twice and expect the size to become 2. A third mistake is to print the string pine instead of calling size. A fourth mistake is to print a literal 1 and never use the set. A fifth mistake is to drop #include <string> while using std::string as the set item type.
This page stops before the next tool, where you walk through collection items one by one. The next page names that tool once at the end. The set you have just written only shows one insert and one size print in the same function. Keep the insert before the size print so the order stays clear for a first careful reading of the file.
Remember that insert does not print by itself. You still write the std::cout line with size(). After one insert the report is 1. After a duplicate pine insert the report stays 1. After you undo an oak experiment the checked file returns to one pine. Stability of the checked file matters as much as learning the set itself.
Open box.cpp. Remove the map lines from the map demo. Add #include <set> beside the other includes and drop #include <map>. In main, create words, call insert("pine"), and print words.size() as the only screen line. The file name on disk stays box.cpp. The checked file is below, and the new lines are the set type, the insert call, and the size print.
#include <iostream>
#include <string>
#include <set>
int main()
{
std::set<std::string> words;
words.insert("pine");
std::cout << words.size() << 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: 1.
Check that line first on the screen. The set held one unique item after insert, so size() printed 1. 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 shows 0, the insert may be missing. If the screen shows pine, you printed the string instead of size(). If the build fails about set, check #include <set>.
📊 set and what the screen shows
| Piece | What the screen shows |
|---|---|
| First | 1, because insert added one unique item before size was printed |
| Last | 1, because the program prints one line and stops |
| Rule | a set stores unique values; insert adds pine; size() reports the count |
| Screen | 1 |
Try one change at a time. Add a second words.insert("pine"); after the first insert, leave the print as words.size(), and rebuild. The screen still shows 1, because the set kept only one unique pine. Remove the duplicate insert. Next, temporarily insert "oak" as well and rebuild so the screen shows 2, then restore the single pine insert so the checked screen is 1 again. Each experiment changes one thing on purpose, so the screen shows how uniqueness and count interact. Undo each change before the next one starts, so the file always returns to the checked workshop version with one insert("pine") and words.size() once again after every single careful try back on disk again.
This page ends the set tool and stops before the next one. The next page is iterating, which walks collection items one by one. The set you have just written only shows inserting pine and printing the size in box.cpp. You leave with one line on the screen: 1. 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 1 alone right there now after the final successful build run on disk today now after the size call.
🧠 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