Menu
Coddy logo textTech

Iterating Over a Map

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

Now that you know how to create, modify, and remove elements from a map, let's learn how to go through all the key-value pairs in your map. The range-based for loop provides an elegant way to iterate through every element in a std::map.

Here's the basic syntax for iterating over a map:

for (auto pair : myMap) {
    // Access the key with pair.first
    // Access the value with pair.second
}

Each element in the loop is a pair object that contains two important members: first holds the key, and second holds the value. Here's a practical example:

std::map<std::string, int> scores;
scores["Alice"] = 95;
scores["Bob"] = 87;
scores["Carol"] = 92;

for (auto pair : scores) {
    std::cout << pair.first << ": " << pair.second << std::endl;
}

This will print each student's name followed by their score. The auto keyword automatically determines the correct type for each pair, making your code cleaner and easier to read.

challenge icon

Challenge

Easy

Create a program that manages a restaurant menu system using a std::map. Your program will store menu items and their prices, then display the complete menu by iterating through all key-value pairs.

The following inputs will be provided:

  • An integer n representing the number of menu items
  • Then n pairs of inputs:
    • A string representing the dish name
    • A double representing the price of the dish

Your program should:

  1. Create a std::map<std::string, double> named menu
  2. Read the number of menu items
  3. For each menu item, read the dish name and price, then add them to the map using the square bracket notation
  4. Use a range-based for loop to iterate through the entire map and print each dish with its price
  5. Calculate and print the total number of items in the menu

Use the following exact output format:

Restaurant Menu:
[dish1]: $[price1]
[dish2]: $[price2]
[dish3]: $[price3]
...
Total menu items: [number of items]

The menu items should be printed in the order they appear when iterating through the map (alphabetical order by dish name). Use a range-based for loop with auto pair : menu to iterate through the map, accessing each key-value pair with pair.first for the dish name and pair.second for the price. Print each price with exactly 2 decimal places using std::fixed and std::setprecision(2).

Cheat sheet

Use a range-based for loop to iterate through all key-value pairs in a std::map:

for (auto pair : myMap) {
    // Access the key with pair.first
    // Access the value with pair.second
}

Each element is a pair object where first holds the key and second holds the value:

std::map<std::string, int> scores;
scores["Alice"] = 95;
scores["Bob"] = 87;
scores["Carol"] = 92;

for (auto pair : scores) {
    std::cout << pair.first << ": " << pair.second << std::endl;
}

The auto keyword automatically determines the correct type for each pair.

Try it yourself

#include <iostream>
#include <map>
#include <string>
#include <iomanip>
using namespace std;

int main() {
    // Read the number of menu items
    int n;
    cin >> n;
    
    // Create the menu map
    map<string, double> menu;
    
    // Read menu items and prices
    for (int i = 0; i < n; i++) {
        string dish;
        double price;
        cin >> dish >> price;
        // TODO: Add the dish and price to the menu map
    }
    
    // TODO: Print "Restaurant Menu:" header
    
    // TODO: Use range-based for loop to iterate through menu and print each item
    // Format: [dish]: $[price] with 2 decimal places
    
    // TODO: Print total number of menu items
    
    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