C++ STL Set Erase Method

Beginner

Introduction

In this lab, you will learn about the working of a Multiset and its implementation in the C++ programming language. You will specifically learn how to use the erase() method in a Set or Multiset to delete elements.

Create the C++ code file

Use any text editor of your choice to create a new C++ code file named main.cpp in the project directory ~/project.

cd ~/project
touch main.cpp

Write the C++ code

Enter the following code into the main.cpp file. The comments explain the code.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//Function to print the elements of the multiset using an iterator
void show(multiset<int> s)
{
    //declaring an iterator to iterate through the multiset
    multiset<int>::iterator i;

    for (i = s.begin(); i != s.end(); i++)
    {
        cout << *i << "  "; //accessing the elements of the multiset using * as i stores the address to each element
    }

    cout << endl;
}

int main()
{
    cout << "\n\nWelcome to STL Set erase() Method Lab\n\n\n";
    cout << " =====  Program to demonstrate the working of a Multiset, in CPP  ===== \n\n\n\n";

    cout << "*** Multisets are similar to set, with an exception that multiple elements can have same values. *** \n\n";

    //Set declaration (Set of integers)
    multiset<int> s;

    //Filling the elements by using the insert() method.
    cout << "\n\nFilling the Multiset with integers in random order."; //Multiset automatically stores them in order

    s.insert(5);
    s.insert(39);
    s.insert(5);
    s.insert(82);
    s.insert(39);
    s.insert(54);

    cout << "\n\nThe number of elements in the Multiset are: " << s.size();

    cout << "\n\nThe elements of the Multiset are: ";
    show(s);

    multiset<int>::iterator it;

    //Deleting all the elements of the set that are less than 54
    s.erase(s.begin(), s.find(54));

    cout << "\n\nAfter deleting all the elements that are less than 54, the Multiset becomes : ";

    for (it = s.begin(); it != s.end(); it++)
    {
        cout << " " << *it;
    }

    cout << "\n\n\n";

    return 0;
}

Compile and run the C++ code

In the terminal, compile the main.cpp file using the g++ command, then run the compiled file.

g++ main.cpp -o main && ./main

You should see the following output:

Welcome to STL Set erase() Method Lab


 =====  Program to demonstrate the working of a Multiset, in CPP  =====



*** Multisets are similar to set, with an exception that multiple elements can have same values. ***


Filling the Multiset with integers in random order.
The number of elements in the Multiset are: 6

The elements of the Multiset are: 5  5  39  39  54  82

After deleting all the elements that are less than 54, the Multiset becomes :  54  82

Review the code

Take the time to review the code and make sure that you understand how the erase() method is used to delete elements from the Multiset.

//Deleting all the elements of the set that are less than 54
s.erase(s.begin(), s.find(54));

This code uses the erase() method to delete all elements in the Multiset from the beginning up to but not including the element with the value 54. Thus, the resulting Multiset contains only the values 54 and 82.

Summary

In this lab, you learned how to use the erase() method in a C++ Set or Multiset to delete elements. You also learned how to iterate through a multiset using an iterator and print the elements using the show() method. This is useful when debugging or checking the contents of complex data structures like Sets and Multisets.

Other Tutorials you may like