Related Articles

Related Articles

deque::clear() and deque::erase() in C++ STL
  • Difficulty Level : Easy
  • Last Updated : 25 Jun, 2020


Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed.

deque::clear()

clear() function is used to remove all the elements of the deque container, thus making its size 0.
Syntax :

dequename.clear()
Parameters :
No parameters are passed.
Result :
All the elements of the deque are
removed ( or destroyed )

Examples:

Input  : mydeque = {1, 2, 3, 4, 5}
         mydeque.clear();
Output : mydeque = {}

Input  : mydeque = {}
         mydeque.clear();
Output : mydeque = {}

Errors and Exceptions

1. It has a no exception throw guarantee.
2. Shows error when a parameter is passed.



filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Implementation of clear() function
#include <deque>
#include <iostream>
using namespace std;
  
int main()
{
    deque<int> mydeque{ 1, 2, 3, 4, 5 };
  
    mydeque.clear();
    // Deque becomes empty
  
    // Printing the deque
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

No Output
deque::erase()

erase() function is used to remove elements from a container from the specified position or range.

Syntax :

1. dequename.erase(position)
2. dequename.erase(startingposition, endingposition)
Parameters :
Position of the element to be removed in the form of iterator.
or the range specified using start and end iterator.
Result :
Elements are removed from the specified
position of the container.

Examples:

Input  : mydeque{1, 2, 3, 4, 5}, iterator= 2
         mydeque.erase(iterator);
Output : 1, 2, 4, 5

Input  : mydeque{1, 2, 3, 4, 5, 6, 7, 8}, iterator1= 3, iterator2= 6
         mydeque.erase(iterator1, iterator2);
Output : 1, 2, 3, 8

Errors and Exceptions

1. It has a no exception throw guarantee, if the position is valid.
2. Shows undefined behaviour otherwise.

Removing element from particular position

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Implementation of erase() function
#include <deque>
#include <iostream>
using namespace std;
  
int main()
{
    deque<int> mydeque{ 1, 2, 3, 4, 5 };
    deque<int>::iterator it;
  
    it = mydeque.begin();
    mydeque.erase(it);
  
    // Printing the deque
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:



2 3 4 5

Removing elements within a range

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Implementation of erase() function
#include <deque>
#include <iostream>
using namespace std;
  
int main()
{
    deque<int> mydeque{ 1, 2, 3, 4, 5 };
    deque<int>::iterator it1, it2;
  
    it1 = mydeque.begin();
    it2 = mydeque.end();
    it2--;
    it2--;
  
    mydeque.erase(it1, it2);
  
    // Printing the deque
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

4 5

Application
Given a list of integers, remove all the even elements from the deque and print the deque.

Input  :1, 2, 3, 4, 5, 6, 7, 8, 9
Output :1 3 5 7 9
Explanation - 2, 4, 6 and 8 which are even are erased from the deque

Algorithm
1. Run a loop till the size of the deque.
2. Check if the element at each position is divisible by 2, if yes, remove the element and increment the iterator, else just increment the iterator to check the next element.
3. Print the final deque.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// Application of erase() function
#include <deque>
#include <iostream>
using namespace std;
  
int main()
{
    deque<int> mydeque{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    deque<int>::iterator i;
    i = mydeque.begin();
    while (i != mydeque.end()) {
        if (*i % 2 == 0) 
            mydeque.erase(i);
        i++;        
    }
  
    // Printing the deque
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

chevron_right


Output:

1 3 5 7 9
clear() VS erase() . When to use what?

clear() removes all the elements from a deque container, thus making its size 0. All the elements of the deque are removed using clear() function.
erase() function on the other hand, is used to remove specific elements from the container or a range of elements from the container, thus reducing its size by the number of elements removed.

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :