How to delete last element from a set in C++

Given a Set, the task is to remove the last element from this Set in C++.

Examples:

Input: set = [10 20 30 70 80 90 100 40 50 60]
Output: 10 20 30 40 50 60 70 80 90

Input: set = [1 2 3 4 5]
Output: 1 2 3 4

Sets are a type of associative containers in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element.

The last element of the Set can be deleted by by passing its iterator.

Syntax:



iterator erase (const_iterator positionOfElementToBeDeleted);

Approach: One can easily delete the last element by passing its iterator to the erase function. To reach the iterator which points to the last element, there are two ways:

  1. Method 1:
    prev(setInt.end())

    Below is the implementation of the above approach:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to delete last element
    // of a Set by passing its iterator
      
    #include <iostream>
    #include <set>
    using namespace std;
      
    // Function to print the set
    void printSet(set<int> myset)
    {
      
        // Get the iterator
        set<int>::iterator it;
      
        // printing all the elements of the set
        for (it = myset.begin(); it != myset.end(); ++it)
            cout << ' ' << *it;
        cout << '\n';
    }
      
    // Function to delete last element of set
    // using method 1
    void deleteByMethod1(set<int> myset)
    {
      
        // printing all the elements of the set
        cout << "\nSet originally: ";
        printSet(myset);
      
        // Get the iterator
        set<int>::iterator it;
      
        // Get the positionOfElementToBeDeleted
        // using method 1
        it = prev(myset.end());
      
        // Erase the last element
        // currently pointed by the iterator
        myset.erase(it);
      
        // printing all the elements of the set
        cout << "Set after deletion: ";
        printSet(myset);
    }
      
    // Driver code
    int main()
    {
        set<int> myset;
      
        // Get the set
        for (int i = 1; i < 10; i++)
            myset.insert(i * 10);
      
        // Method 1 to get positionOfElementToBeDeleted
        deleteByMethod1(myset);
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Set originally:  10 20 30 40 50 60 70 80 90
    Set after deletion:  10 20 30 40 50 60 70 80
    
  2. Method 2:
    set::iterator it = setInt.end(); 
    --it;
    

    Below is the implementation of the above approach:

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C++ program to delete last element
    // of a Set by passing its iterator
      
    #include <iostream>
    #include <set>
    using namespace std;
      
    // Function to print the set
    void printSet(set<int> myset)
    {
      
        // Get the iterator
        set<int>::iterator it;
      
        // printing all the elements of the set
        for (it = myset.begin(); it != myset.end(); ++it)
            cout << ' ' << *it;
        cout << '\n';
    }
      
    // Function to delete last element of set
    // using method 2
    void deleteByMethod2(set<int> myset)
    {
      
        // printing all the elements of the set
        cout << "\nSet originally: ";
        printSet(myset);
      
        // Get the iterator
        set<int>::iterator it;
      
        // Get the positionOfElementToBeDeleted
        // using method 2
        it = --myset.end();
      
        // Erase the last element
        // currently pointed by the iterator
        myset.erase(it);
      
        // printing all the elements of the set
        cout << "Set after deletion: ";
        printSet(myset);
    }
      
    // Driver code
    int main()
    {
        set<int> myset;
      
        // Get the set
        for (int i = 1; i < 10; i++)
            myset.insert(i * 10);
      
        // Method 2 to get positionOfElementToBeDeleted
        deleteByMethod2(myset);
      
        return 0;
    }

    chevron_right

    
    

    Output:

    Set originally:  10 20 30 40 50 60 70 80 90
    Set after deletion:  10 20 30 40 50 60 70 80
    


My Personal Notes arrow_drop_up

Student at BCE,Patna Love to code and a stubborn backend programmer Github- shubhamofbce

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.