Menu
Coddy logo textTech

Removing Elements

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

Sometimes you need to remove elements from a set when they're no longer needed. The .erase() method allows you to remove a specific element by providing its value.

Here's how to remove an element from a set:

std::set<int> numbers = {10, 20, 30, 40};
numbers.erase(20);  // Removes the element 20

After calling .erase(20), the set will only contain {10, 30, 40}. If you try to erase an element that doesn't exist in the set, nothing happens - the set remains unchanged and no error occurs.

This makes .erase() safe to use even when you're not certain the element exists. It's particularly useful for maintaining clean collections where you need to remove specific items based on user input or program logic.

challenge icon

Challenge

Easy

Create a program that demonstrates removing elements from a std::set using the .erase() method. This challenge will test your understanding of how to safely remove specific elements from a set and observe the results.

The following inputs will be provided:

  • An integer n representing the number of elements to initially add to the set
  • Then n integers to be inserted into the set
  • An integer m representing the number of elements to remove
  • Then m integers representing the elements to remove from the set

Your program should:

  1. Create an empty std::set<int>
  2. Read the number of elements to add and insert them into the set using .insert()
  3. Print the initial set size after all insertions
  4. Read the number of elements to remove
  5. For each element to remove, use .erase() to attempt removal
  6. After each removal attempt, print the current size of the set
  7. Finally, iterate through the remaining elements in the set and print them in sorted order

Use the following exact output format:

Initial set size:

Initial size: [size]

After each removal attempt:

After removing [number]: size = [current_size]

Final remaining elements:

Remaining elements: [element1] [element2] [element3] ...

Remember that .erase() safely handles attempts to remove elements that don't exist in the set - the set remains unchanged and no error occurs. Use a range-based for loop to print the remaining elements, and they will automatically be displayed in sorted order since sets maintain their elements in sorted sequence.

Cheat sheet

Use .erase() to remove a specific element from a set by providing its value:

std::set<int> numbers = {10, 20, 30, 40};
numbers.erase(20);  // Removes the element 20

If you try to erase an element that doesn't exist in the set, nothing happens - the set remains unchanged and no error occurs, making .erase() safe to use.

Try it yourself

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

int main() {
    // Read number of elements to add
    int n;
    cin >> n;
    
    // Create an empty set
    set<int> mySet;
    
    // Read and insert n elements
    for (int i = 0; i < n; i++) {
        int element;
        cin >> element;
        // Insert element into set
    }
    
    // Read number of elements to remove
    int m;
    cin >> m;
    
    // TODO: Write your code below
    // 1. Print initial set size
    // 2. For each element to remove, use .erase() and print size after each removal
    // 3. Print remaining elements in the set
    
    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