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

06 Dec, 2018
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:
Method 1:prev(setInt.end())Below is the implementation of the above approach:Program 1:// C++ program to delete last element// of a Set by passing its iterator  #include <iostream>#include <set>using namespace std;  // Function to print the setvoid 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 1void 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 codeint 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;}Output:Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 30 40 50 60 70 80
Method 2:set::iterator it = setInt.end(); 
--it;
Below is the implementation of the above approach:Program 2:// C++ program to delete last element// of a Set by passing its iterator  #include <iostream>#include <set>using namespace std;  // Function to print the setvoid 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 2void 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 codeint 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;}Output:Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 30 40 50 60 70 80

Method 1:prev(setInt.end())Below is the implementation of the above approach:Program 1:// C++ program to delete last element// of a Set by passing its iterator  #include <iostream>#include <set>using namespace std;  // Function to print the setvoid 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 1void 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 codeint 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;}Output:Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 30 40 50 60 70 80

prev(setInt.end())
Below is the implementation of the above approach:
Program 1:
// C++ program to delete last element// of a Set by passing its iterator  #include <iostream>#include <set>using namespace std;  // Function to print the setvoid 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 1void 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 codeint 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;}
Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 30 40 50 60 70 80

Method 2:set::iterator it = setInt.end(); 
--it;
Below is the implementation of the above approach:Program 2:// C++ program to delete last element// of a Set by passing its iterator  #include <iostream>#include <set>using namespace std;  // Function to print the setvoid 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 2void 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 codeint 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;}Output:Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 30 40 50 60 70 80

set::iterator it = setInt.end(); 
--it;

Below is the implementation of the above approach:
Program 2:
// C++ program to delete last element// of a Set by passing its iterator  #include <iostream>#include <set>using namespace std;  // Function to print the setvoid 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 2void 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 codeint 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;}
Set originally:  10 20 30 40 50 60 70 80 90
Set after deletion:  10 20 30 40 50 60 70 80

cpp-set
STL
Technical Scripter 2018
C++ Programs
Technical Scripter
STL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Const keyword in C++
Program to implement Singly Linked List in C++ using class
cout in C++
Different ways to print elements of vector
Why it is important to write "using namespace std" in C++ program?
Dynamic _Cast in C++
string::npos in C++ with Examples
Maximum value of long long int in C++
How to convert a Vector to Set in C++