![]() |
VOOZH | about |
In C++, deque also known as double-ended queue is a container provided by the Standard Template Library (STL) that allows efficient insertion and deletion at both ends. In this article, we will learn how to clear all elements from a deque in C++.
Example:
Input: myDeque = {10, 20, 30, 40}; Output: After clearing, the deque is empty
To clear all elements from a std::deque in C++, we can use the std::deque::clear() function that is used to remove all the elements from the deque container, thus making the deque empty.
deque_Name.clear();The below program demonstrates how we can use clear() function to remove all elements from a deque in C++.
Deque Elements:10 20 30 40 Before clearing, the deque has 4 elements After clearing, the deque has 0 elements
Time Complexity: O(1), where N is the size of the deque.
Auxiliary Space: O(1)