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

📅  最后修改于: 2021-05-30 05:36:08             🧑  作者: Mango

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

例子:

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

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

列表是一种关联容器,其中每个元素都必须是唯一的,因为元素的值可以标识它。尽管可以删除并添加该元素的修改后的值,但是一旦将该元素的值添加到列表中就无法对其进行修改。

可以通过传递其迭代器来删除List的最后一个元素。

句法:

iterator erase (const_iterator positionOfElementToBeDeleted);

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

  1. 方法1:
    prev(listInt.end())

    下面是上述方法的实现:

    程序1:

    // C++ program to delete last element
    // of a List by passing its iterator
      
    #include 
    #include 
    using namespace std;
      
    // Function to print the list
    void printList(list mylist)
    {
      
        // Get the iterator
        list::iterator it;
      
        // printing all the elements of the list
        for (it = mylist.begin(); it != mylist.end(); ++it)
            cout << ' ' << *it;
        cout << '\n';
    }
      
    // Function to delete last element of list
    // using method 1
    void deleteByMethod1(list mylist)
    {
      
        // printing all the elements of the list
        cout << "\nList originally: ";
        printList(mylist);
      
        // Get the iterator
        list::iterator it;
      
        // Get the positionOfElementToBeDeleted
        // using method 1
        it = prev(mylist.end());
      
        // Erase the last element
        // currently pointed by the iterator
        mylist.erase(it);
      
        // printing all the elements of the list
        cout << "List after deletion: ";
        printList(mylist);
    }
      
    // Driver code
    int main()
    {
        list mylist;
      
        // Get the list
        for (int i = 1; i < 10; i++)
            mylist.push_back(i * 10);
      
        // Method 1 to get positionOfElementToBeDeleted
        deleteByMethod1(mylist);
      
        return 0;
    }
    
    输出:
    List originally:  10 20 30 40 50 60 70 80 90
    List after deletion:  10 20 30 40 50 60 70 80
    
  2. 方法2:
    list::iterator it = listInt.end(); 
    --it;
    

    下面是上述方法的实现:

    程式2:

    // C++ program to delete last element
    // of a List by passing its iterator
      
    #include 
    #include 
    using namespace std;
      
    // Function to print the list
    void printList(list mylist)
    {
      
        // Get the iterator
        list::iterator it;
      
        // printing all the elements of the list
        for (it = mylist.begin(); it != mylist.end(); ++it)
            cout << ' ' << *it;
        cout << '\n';
    }
      
    // Function to delete last element of list
    // using method 2
    void deleteByMethod2(list mylist)
    {
      
        // printing all the elements of the list
        cout << "\nList originally: ";
        printList(mylist);
      
        // Get the iterator
        list::iterator it;
      
        // Get the positionOfElementToBeDeleted
        // using method 2
        it = --mylist.end();
      
        // Erase the last element
        // currently pointed by the iterator
        mylist.erase(it);
      
        // printing all the elements of the list
        cout << "List after deletion: ";
        printList(mylist);
    }
      
    // Driver code
    int main()
    {
        list mylist;
      
        // Get the list
        for (int i = 1; i < 10; i++)
            mylist.push_back(i * 10);
      
        // Method 2 to get positionOfElementToBeDeleted
        deleteByMethod2(mylist);
      
        return 0;
    }
    
    输出:
    List originally:  10 20 30 40 50 60 70 80 90
    List after deletion:  10 20 30 40 50 60 70 80
    
    要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”