📌  相关文章
📜  如何从C ++中的集合中删除最后一个元素

📅  最后修改于: 2022-05-13 01:57:05.144000             🧑  作者: Mango

如何从C ++中的集合中删除最后一个元素

给定一个 Set,任务是在 C++ 中从这个 Set 中删除最后一个元素。

例子:

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

集合是一种关联容器,其中每个元素都必须是唯一的,因为元素的值标识了它。元素的值一旦添加到集合中就不能修改,尽管可以删除和添加该元素的修改值。

Set 的最后一个元素可以通过传递 iterator来删除。

句法:

iterator erase (const_iterator positionOfElementToBeDeleted);

方法:通过将迭代器传递给擦除函数,可以轻松删除最后一个元素。要到达指向最后一个元素的迭代器,有两种方法:

  1. 方法一:
    prev(setInt.end())

    下面是上述方法的实现:

    方案一:

    // C++ program to delete last element
    // of a Set by passing its iterator
      
    #include 
    #include 
    using namespace std;
      
    // Function to print the set
    void printSet(set myset)
    {
      
        // Get the iterator
        set::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 myset)
    {
      
        // printing all the elements of the set
        cout << "\nSet originally: ";
        printSet(myset);
      
        // Get the iterator
        set::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 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
    
  2. 方法二:
    set::iterator it = setInt.end(); 
    --it;
    

    下面是上述方法的实现:

    方案二:

    // C++ program to delete last element
    // of a Set by passing its iterator
      
    #include 
    #include 
    using namespace std;
      
    // Function to print the set
    void printSet(set myset)
    {
      
        // Get the iterator
        set::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 myset)
    {
      
        // printing all the elements of the set
        cout << "\nSet originally: ";
        printSet(myset);
      
        // Get the iterator
        set::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 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