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

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. 
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;}
 
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;}
 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;}
 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, 9Output:1 3 5 7 9Explanation: 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;}
 1 3 5 7 9
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. 
timelessvaibhav
keerthiniab
dheerajvarmabudde
cpp-deque
CPP-Library
deque
STL
C++
Misc
Misc
Misc
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
C++ Classes and Objects
Virtual Function in C++
Templates in C++ with Examples
Constructors in C++
vector::push_back() and vector::pop_back() in C++ STL
Top 10 algorithms in Interview Questions
Overview of Data Structures | Set 1 (Linear Data Structures)
How to write Regular Expressions?