C++ std::vector size() Function

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

Definition

In C++, std::vector::size() returns the number of elements that a std::vector holds right now. A vector with three elements has the size 3.

The size changes whenever you add or remove elements, for example with push_back() or pop_back(). 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 size with empty brackets. It takes no arguments.

C++
std::size_t n = v.size();
for (std::size_t i = 0; i < v.size(); ++i) { }
CallWhat it does
v.size()Returns the number of elements.
v.size() - 1The index of the last element. Careful: it is wrong when the vector is empty.

To only check for no elements, empty() is clearer than size() == 0.

Library declarations
C++
size_type size() const noexcept;

Parameters

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

Return Value

Return ValueDescription
size_typeAn unsigned number: how many elements the vector holds. It is 0 for an empty vector.

C++ Examples

Example 1 — Basic size()

Count the elements of a small vector.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {10, 20, 30, 40};

    std::cout << "size: " << v.size() << '\n';
    return 0;
}

How It Works

  1. The vector holds four numbers.
  2. size() counts them and returns 4.
  3. The last valid index is size() - 1, which is 3.

Example 2 — Loop with size()

Use size() as the limit of an index loop.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v = {3, 6, 9};

    for (std::size_t i = 0; i < v.size(); ++i) {
        std::cout << "d[" << i << "] = " << v[i] << '\n';
    }
    return 0;
}

How It Works

  1. The index i starts at 0 and goes up while it is smaller than v.size().
  2. The type std::size_t matches the return type of size(), so the compiler has nothing to warn about.
  3. The loop never reaches index 3, so d[i] always stays inside the vector.

Example 3 — Watch the Size Change

The size grows and shrinks as you add and remove elements at both ends.

C++
#include <vector>
#include <iostream>

int main() {
    std::vector<int> v;
    std::cout << "start: " << v.size() << '\n';

    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    std::cout << "after 3 pushes: " << v.size() << '\n';

    v.pop_back();
    std::cout << "after 1 pop: " << v.size() << '\n';
    return 0;
}

How It Works

  1. A new vector is empty, so size() is 0.
  2. Each of the three pushes adds one element. It does not matter whether the push is at the front or the back, so the size is 3.
  3. pop_back() removes the last element, and the size goes down by one.

Frequently Asked Questions

It returns the number of elements that the vector holds right now, as an unsigned number.
size() is how many elements the vector has now. max_size() is the largest number it could ever have in theory.
size() is unsigned. If the vector is empty, d.size() - 1 wraps around to a huge number instead of -1. Check empty() first.
No. It takes constant time. The vector keeps the count, so it does not count the elements one by one.
A vector has size() only. capacity() exists for std::vector and std::string, but not for std::vector.

Next: swap()

Exchange the content of two vectors with std::vector.

swap() 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.

10 people found this page helpful