list erase() function in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of list::erase() function in C++.

What is a List in STL?

List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List perform better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.

What is list::erase()?

list::erase() is an inbuilt function in C++ STL which is declared in header file. erase() is used to remove elements from the list container. We can erase a single element or range of elements from the list container. It reduces the size of the list container by the number of elements to be removed/erased

Syntax

list_container.erase(positon);
list_container.erase(start_position, end_position);

This function can accept either one or two parameters −

Parameters

  • position − This is a single position from which we want to remove an element.

  • start_position, end_position − This defines a range from which we want to remove the elements.

Return Value

This function returns an iterator which is pointing the next to the last element which is removed.

Example

In the below code we have to erase the elements using the erase() function present in a STL and for that we will fetch first element using an iterator that will point to the first element using begin() function and after that we will erase the iterator that contains the first element of a list.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //Create a list
   list<int> myList;
   myList.push_back(2);
   myList.push_back(4);
   myList.push_back(6);
   myList.push_back(8);
   myList.push_back(10);
   cout<<"List before deleting elements: ";
   for (auto i = myList.begin(); i!= myList.end(); i++){
      cout << *i<< " ";
   }
   //iterator that will point to the first element
   list<int>::iterator i = myList.begin();
   myList.erase(i);
   //list after deleting the element
   cout << "\nList after deleting elements: ";
   for (auto i = myList.begin(); i!= myList.end(); i++){
      cout << *i << " ";
   }
   return 0;
}

Output

If we run the above code it will generate the following output

List before deleting elements: 2 4 6 8 10
List after deleting elements: 4 6 8 10

Example

In the below code we have to erase the elements using the erase() function present in a STL and for that we will fetch first element using an iterator(iterator 1) that will point to the firstelement using begin() function and we will take another iterator(iterator 2) to point to the secondelement and after that we will erase the iterator from the range between iterator 1 and iterator 2 and display the result.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int main(){
   //Create a list
   list<int> myList;
   myList.push_back(2);
   myList.push_back(4);
   myList.push_back(6);
   myList.push_back(8);
   myList.push_back(10);
   cout<<"List before deleting elements: ";
   for (auto i = myList.begin(); i!= myList.end(); i++){
      cout << *i << " ";
   }
   //iterator that will point to the first element
   list<int>::iterator i_1, i_2;
   i_1 = myList.begin();
   i_2 = myList.begin();
   //advance() function will increment the position of iterator 2 by 3
   advance(i_2, 4);
   //now it will delete the elements from the range 0 - 4
   myList.erase(i_1, i_2);
   //list after deleting the element
   cout<< "\nList after deleting elements: ";
   for (auto i = myList.begin(); i!= myList.end(); i++){
      cout << *i << " ";
   }
   return 0;
}

Output

If we run the above code it will generate the following output

List before deleting elements: 2 4 6 8 10
List after deleting elements: 10
raja
Published on 02-Mar-2020 09:15:41
Advertisements