删除 C++ STL 列表中的元素
在本教程中,我们将讨论一个程序来了解如何在 C++ STL 列表中删除元素。
为此,我们将使用 pop_back() 和 pop_front() 函数分别从末尾和开头删除元素。
示例
#include<iostream> #include<list> using namespace std; int main(){ list<int>list1={10,15,20,25,30,35}; cout << "The original list is : "; for (list<int>::iterator i=list1.begin(); i!=list1.end();i++) cout << *i << " "; cout << endl; //deleting first element list1.pop_front(); cout << "The list after deleting first element using pop_front() : "; for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++) cout << *i << " "; cout << endl; //deleting last element list1.pop_back(); cout << "The list after deleting last element using pop_back() : "; for (list<int>::iterator i=list1.begin(); i!=list1.end(); i++) cout << *i << " "; cout << endl; }
输出
The original list is : 10 15 20 25 30 35 The list after deleting first element using pop_front() : 15 20 25 30 35 The list after deleting last element using pop_back() : 15 20 25 30
广告