deque::clear() and deque::erase() in C++ STL
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.
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 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:
No Output
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
// 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 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 decrement iterator.
3. Print the final deque.
// 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 }; for (auto i = mydeque.begin(); i != mydeque.end(); ++i) { if (*i % 2 == 0) { 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() 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.
Recommended Posts:
- Difference between Private and Protected in C++ with Example
- Difference between Inheritance and Polymorphism
- Difference between Single and Multiple Inheritance in C++
- Difference between Abstraction and Encapsulation in C++
- return statement in C/C++ with Examples
- How to implement our own Vector Class in C++?
- Web Programming in C++
- C++ Program to print an Array using Recursion
- Storage Classes in C++ with Examples
- fill_n() function in C++ STL with examples
- fill() function in C++ STL with examples
- How to erase an element from a vector using erase() and reverse_iterator?
- iomanip setbase() function in C++ with Examples
- iomanip setpricision() function in C++ with Examples
- iomanip setiosflags() function in C++ with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



