- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete elements in C++ STL list
IIn this tutorial, we will be discussing a program to understand how to delete elements in the C++ STL list.
For this, we will be using the pop_back() and pop_front() function to delete the element from last and the front respectively.
Example
#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; }
Output
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
- Related Articles
- How to insert elements in C++ STL List?
- How to delete last element from a List in C++ STL
- Delete List Elements in Python
- list unique( ) in C++ STL
- list insert( ) in C++ STL
- List::clear() in C++ STL
- list get_allocator in C++ STL
- list swap( ) in C++ STL
- list operator = in C++ STL
- list begin( ) and list end( ) in C++ STL
- list::emplace_front() and list::emplace_back() in C++ STL
- list::front() and list::back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- List push_front() function in C++ STL

Advertisements