Menu
Coddy logo textTech

Removing Pairs

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

Sometimes you need to remove key-value pairs from your map when they're no longer needed. The .erase() method provides a straightforward way to delete elements by specifying the key you want to remove.

To remove an element from a map, simply call .erase() with the key as the argument:

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

scores.erase("Bob"); // Removes Bob's entry completely

After calling erase("Bob"), the map will only contain Alice and Carol's scores. If you try to erase a key that doesn't exist in the map, the operation simply does nothing - no error occurs.

This method is particularly useful for maintaining clean data structures, removing outdated information, or implementing features where users can delete entries from your application.

challenge icon

Challenge

Easy

Create a program that manages a contact directory using a std::map. Your program will store contact names and phone numbers, then allow users to remove specific contacts from the directory using the .erase() method.

The following inputs will be provided:

  • An integer n representing the number of initial contacts
  • Then n pairs of inputs:
    • A string representing the contact name
    • A string representing the phone number
  • An integer m representing the number of contacts to remove
  • Then m strings representing contact names to remove

Your program should:

  1. Create a std::map<std::string, std::string> named contacts
  2. Read the number of initial contacts and populate the map with contact names and phone numbers
  3. Print the initial contact directory in the format shown below
  4. Read the number of contacts to remove
  5. For each contact name to remove, use the .erase() method to remove it from the map
  6. Print the updated contact directory after all removals

Use the following exact output format:

Initial directory:

Initial Contact Directory:
[contact1]: [phone1]
[contact2]: [phone2]
[contact3]: [phone3]
...

Updated directory after removals:

Updated Contact Directory:
[remaining_contact1]: [phone1]
[remaining_contact2]: [phone2]
...

If the directory becomes empty after all removals, print:

Updated Contact Directory:
Directory is empty

The contacts should be printed in the order they appear when iterating through the map (alphabetical order by contact name). Use a range-based for loop to iterate through the map both times, accessing each key-value pair with pair.first for the contact name and pair.second for the phone number. Remember that attempting to erase a contact that doesn't exist will not cause an error - the map will simply remain unchanged.

Cheat sheet

To remove elements from a map, use the .erase() method with the key:

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

scores.erase("Bob"); // Removes Bob's entry completely

If the key doesn't exist, .erase() does nothing and no error occurs.

Try it yourself

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

int main() {
    // Read number of initial contacts
    int n;
    cin >> n;
    
    // Create the contacts map
    map<string, string> contacts;
    
    // Read initial contacts
    for (int i = 0; i < n; i++) {
        string name, phone;
        cin >> name >> phone;
        // TODO: Add contact to the map
    }
    
    // Print initial directory
    cout << "Initial Contact Directory:" << endl;
    // TODO: Print all contacts using range-based for loop
    
    // Read number of contacts to remove
    int m;
    cin >> m;
    
    // Remove contacts
    for (int i = 0; i < m; i++) {
        string nameToRemove;
        cin >> nameToRemove;
        // TODO: Remove contact using .erase() method
    }
    
    // Print updated directory
    cout << "Updated Contact Directory:" << endl;
    // TODO: Print remaining contacts or "Directory is empty" if empty
    
    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