Menu
Coddy logo textTech

Iterating Over a Set

Part of the Logic & Flow section of Coddy's C++ journey — lesson 39 of 56.

Now that you can add, check, and remove elements from a set, let's learn how to iterate through all elements in a set. The range-based for loop provides the cleanest way to visit each element.

Here's how to iterate through a set:

std::set<std::string> fruits = {"banana", "apple", "cherry"};

for (const std::string& fruit : fruits) {
    std::cout << fruit << std::endl;
}

The most important feature to remember is that sets automatically maintain sorted order. When you iterate through the fruits set above, the output will be "apple", "banana", "cherry" - not the order you inserted them. This automatic sorting is one of the key advantages of using std::set.

This sorted iteration makes sets perfect for displaying data in alphabetical or numerical order without needing to sort manually. Whether you're working with numbers, strings, or other comparable types, the set will always present them in their natural sorted sequence.

challenge icon

Challenge

Easy

Create a program that demonstrates the automatic sorting feature of std::set by adding words to a set and then iterating through them to display them in alphabetical order. This challenge will test your understanding of range-based for loops with sets and how sets maintain sorted order.

The following inputs will be provided:

  • An integer n representing the number of words to add to the set
  • Then n strings representing the words to be inserted into the set

Your program should:

  1. Create an empty std::set<std::string>
  2. Read the number of words to add
  3. Use a loop to read each word and insert it into the set using the .insert() method
  4. Use a range-based for loop to iterate through the set and print each word
  5. After printing all words, display the total count of unique words in the set

Use the following exact output format:

For each word in the set (in alphabetical order):

[word]

Summary at the end:

Total unique words: [count]

Use a range-based for loop with the syntax for (const std::string& word : wordSet) to iterate through all elements in the set. The set will automatically present the words in alphabetical order, demonstrating one of the key advantages of using std::set. Remember that if duplicate words are provided in the input, they will be automatically ignored by the set, so only unique words will be displayed.

Cheat sheet

Use a range-based for loop to iterate through all elements in a set:

std::set<std::string> fruits = {"banana", "apple", "cherry"};

for (const std::string& fruit : fruits) {
    std::cout << fruit << std::endl;
}

Sets automatically maintain sorted order during iteration. Elements are displayed in their natural sorted sequence (alphabetical for strings, numerical for numbers), not in insertion order.

Try it yourself

#include <iostream>
#include <set>
#include <string>
using namespace std;

int main() {
    // Read the number of words
    int n;
    cin >> n;
    
    // Create an empty set to store words
    set<string> wordSet;
    
    // TODO: Write your code here
    // Read n words and insert them into the set
    // Use a range-based for loop to iterate through the set and print each word
    // Print the total count of unique words
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow