Home C++ Deques Functions clear() C++ std::deque clear() Function Definition In C++, std::deque::clear() removes all elements from a std::deque. After the call, size() is 0 and empty() is true.
The deque object itself is still alive, so you can push new elements into it again. Include <deque> to use it.
This is a member of std::deque<T>, a double-ended queue . A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.
Syntax Call clear with empty brackets. It takes no arguments.
Call What it does d.clear()Destroys every element. The deque becomes empty.
To remove just one element use erase(), pop_front(), or pop_back().
Library declarations Parameters Parameter Description None clear() takes no parameters. Call it with empty brackets.
Return Value Return Value Description Nothing clear() returns void. Afterwards the deque holds zero elements.
Important
clear() invalidates all iterators, pointers, and references to the elements (including the old end()). If the elements are raw pointers, clear() does not delete what they point to. It only removes the pointers.
C++ Examples 1. Basic clear 2. Reuse 3. Check empty Example 1 — Basic clear() Look at the size before and after the call.
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {1, 2, 3, 4};
std::cout << "before: " << d.size() << '\n';
d.clear();
std::cout << "after: " << d.size() << '\n';
return 0;
} How It Works The deque begins with four elements, so size() prints 4. clear() destroys all four elements in one call.size() now prints 0. The deque is empty but still usable.Example 2 — Reuse a Deque After clear() Clear the deque and fill it again with new values.
#include <deque>
#include <iostream>
int main() {
std::deque<int> d = {5, 6, 7};
d.clear();
d.push_back(100);
d.push_front(50);
for (int n : d) {
std::cout << n << ' ';
}
std::cout << '\n';
return 0;
} How It Works clear() throws away 5 6 7. Nothing is left.push_back(100) adds the first new element, and push_front(50) places a new one before it.Only the two new values remain. The deque never needs to be created again. Example 3 — Check empty() After clear() Use empty() to confirm there is nothing left.
#include <deque>
#include <iostream>
#include <string>
int main() {
std::deque<std::string> tasks = {"wash", "cook", "read"};
std::cout << std::boolalpha;
std::cout << "empty before: " << tasks.empty() << '\n';
tasks.clear();
std::cout << "empty after: " << tasks.empty() << '\n';
return 0;
} empty before: false
empty after: true How It Works std::boolalpha makes cout print true and false instead of 1 and 0.The deque holds three tasks, so empty() is false. After clear() there are no elements, so empty() becomes true. Frequently Asked Questions What does std::deque::clear() do? It removes all elements from the deque. After the call the size is 0 and empty() is true.
Does clear() delete the deque itself? No. The deque object stays alive and can be used again. Only the elements are removed.
Does clear() free the memory? Not always. The standard does not require it. If you want to give memory back, call shrink_to_fit() afterwards, which is only a request.
Does clear() delete pointers stored in the deque? No. If the deque holds raw pointers, clear() only removes the pointer values. You must delete the objects yourself, or better, store smart pointers.
What is the difference between clear() and erase()? clear() removes every element. erase() removes one element or a chosen range, so the rest of the deque stays.
Next: crbegin() Get a read-only reverse iterator to the last element with std::deque.
crbegin() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion