Skip to content
Related Articles

Related Articles

deque::clear() and deque::erase() in C++ STL

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 22 Apr, 2022

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

deque::clear()

The 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.
 

CPP




// 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;
}

Output

 
deque::erase()

The 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 i.e., [start iterator, 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, 7, 8

Errors and Exceptions:
1. It has a no exception throw guarantee if the position is valid. 
2. Shows undefined behavior otherwise.
Removing elements from a particular position 
 

CPP




// 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;
}

Output

 2 3 4 5

Removing elements within a range 
 

CPP




// 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;
}

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.
 

CPP




// 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)     
 /* Not a good idea to erase inside loop, if you delete last element,
 mydeque.end() cannot be found resulting in infinite loop */
            mydeque.erase(i);
        i++;       
    }
 
    // Printing the deque
    for (auto it = mydeque.begin(); it != mydeque.end(); ++it)
        cout << ' ' << *it;
    return 0;
}

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.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!