C++ std::vector clear() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::vector

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.

C++
v.clear();
CallWhat it does
v.clear()Destroys every element. The vector becomes empty.

To remove just one element use erase() or pop_back().

Library declarations
C++
void clear() noexcept;

Parameters

ParameterDescription
Noneclear() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Nothingclear() returns void. Afterwards the vector holds zero elements.

C++ Examples

Example 1 — Basic clear()

Look at the size before and after the call.

C++
#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

  1. The vector begins with four elements, so size() prints 4.
  2. clear() destroys all four elements in one call.
  3. 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.

C++
#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

  1. clear() throws away 5 6 7. Nothing is left.
  2. push_back(100) adds the first new element, and push_back(50) adds another at the end.
  3. 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.

C++
#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;
}

How It Works

  1. std::boolalpha makes cout print true and false instead of 1 and 0.
  2. The vector holds three tasks, so empty() is false.
  3. After clear() there are no elements, so empty() becomes true.

Frequently Asked Questions

It removes all elements from the vector. After the call the size is 0 and empty() is true.
No. The vector object stays alive and can be used again. Only the elements are removed.
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.
No. If the vector holds raw pointers, clear() only removes the pointer values. You must delete the objects yourself, or better, store smart pointers.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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