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 20After 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
EasyCreate 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
nrepresenting the number of elements to initially add to the set - Then
nintegers to be inserted into the set - An integer
mrepresenting the number of elements to remove - Then
mintegers representing the elements to remove from the set
Your program should:
- Create an empty
std::set<int> - Read the number of elements to add and insert them into the set using
.insert() - Print the initial set size after all insertions
- Read the number of elements to remove
- For each element to remove, use
.erase()to attempt removal - After each removal attempt, print the current size of the set
- 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 20If 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;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items6Sets (Unique Elements)
Introducing std::setCreate Set & Add ElementsChecking for ElementsRemoving ElementsIterating Over a SetRecap - Unique Numbers