Home C++ Vectors Functions clear() C++ std::vector clear() Function Definition In C++, std::vector::clear() removes all elements from a std::vector. After the call, size() is 0 and empty() is true.
The vector object itself is still alive, so you can push new elements into it again. Include <vector> to use it.
This is a member of std::vector<T>, a dynamic contiguous array . Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.
Syntax Call clear with empty brackets. It takes no arguments.
Call What it does v.clear()Destroys every element. The vector becomes empty.
To remove just one element use erase() 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 vector 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 <vector>
#include <iostream>
int main() {
std::vector<int> v = {1, 2, 3, 4};
std::cout << "before: " << v.size() << '\n';
v.clear();
std::cout << "after: " << v.size() << '\n';
return 0;
} How It Works The vector begins with four elements, so size() prints 4. clear() destroys all four elements in one call.size() now prints 0. The vector is empty but still usable.Example 2 — Reuse a Vector After clear() Clear the vector and fill it again with new values.
#include <vector>
#include <iostream>
int main() {
std::vector<int> v = {5, 6, 7};
v.clear();
v.push_back(100);
v.push_back(50);
for (int n : v) {
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_back(50) adds another at the end.Only the two new values remain. The vector never needs to be created again. Example 3 — Check empty() After clear() Use empty() to confirm there is nothing left.
#include <vector>
#include <iostream>
#include <string>
int main() {
std::vector<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 vector holds three tasks, so empty() is false. After clear() there are no elements, so empty() becomes true. Frequently Asked Questions What does std::vector::clear() do? It removes all elements from the vector. After the call the size is 0 and empty() is true.
Does clear() delete the vector itself? No. The vector 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 vector? No. If the vector 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 vector stays.
Next: crbegin() Get a read-only reverse iterator to the last element with std::vector.
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.
7 people found this page helpful
Helpful Share Copy link Suggestion