Menu
Coddy logo textTech

Creating a Map

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

Now that you understand what a map is, let's learn how to create one and add key-value pairs to it. When declaring a std::map, you need to specify both the key type and the value type using angle brackets.

Here's the basic syntax for creating a map:

std::map<KeyType, ValueType> mapName;

For example, to create a map that stores student names as keys and their test scores as values:

std::map<std::string, int> studentScores;

To add elements to your map, you can use the square bracket notation with the key, then assign a value:

studentScores["Alice"] = 95;
studentScores["Bob"] = 87;
studentScores["Carol"] = 92;

This creates three key-value pairs in the map. Each student's name serves as the key that allows you to quickly retrieve their corresponding score.

To iterate over all elements in a map, you can use a range-based for loop with auto. Each element in a std::map is a key-value pair, and you can access the key using .first and the value using .second:

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

Here, auto automatically deduces the type of each element (a key-value pair), pair.first gives you the key (the student's name), and pair.second gives you the value (the score). Note that std::map automatically keeps its elements sorted by key, so iterating over the map will print entries in alphabetical order.

challenge icon

Challenge

Easy

Create a program that builds a student grade tracking system using a std::map. Your program will store student names as keys and their test scores as values.

The following inputs will be provided:

  • An integer n representing the number of students
  • Then n pairs of inputs:
    • A string representing the student's name
    • An integer representing the student's score

Your program should:

  1. Create a std::map<std::string, int> named grades
  2. Read the number of students
  3. For each student, read their name and score, then add them to the map using the square bracket notation
  4. After adding all students, print each student's information in the format shown below
  5. Calculate and print the total number of students stored in the map

Use the following exact output format:

Student Grades:\n[student1]: [score1]\n[student2]: [score2]\n[student3]: [score3]\n...\nTotal students: [number of students]

The students should be printed in the order they appear when iterating through the map (which will be alphabetical order since maps automatically sort by key). Use a range-based for loop to iterate through the map, accessing each key-value pair and printing the student name followed by their score.

Try it yourself

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

int main() {
    // Read the number of students
    int n;
    cin >> n;
    
    // Create the map to store grades
    map<string, int> grades;
    
    // TODO: Write your code here
    // Read each student's name and score, then add to the map
    
    
    // Print the results
    cout << "Student Grades:" << endl;
    // TODO: Use a range-based for loop to print each student's grade
    
    
    // Print total number of students
    cout << "Total students: " << grades.size() << endl;
    
    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