C++ std::vector

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

Definition and Usage

In C++, std::vector<T> is a container that holds a row of elements of type T. It is a dynamic contiguous array: all elements sit in one memory block, you can grow it at run time, and v[i] is fast.

Two numbers matter: size (how many elements you use) and capacity (how many fit before the next reallocation). Call reserve() when you know the final count so push_back() does not keep reallocating. Include <vector> to use it.

This page teaches what a std::vector is. The Vector Functions index lists every function guide in this series.

Why choose a vector?

Choose a vector for most sequences: random access, fast loops, and fast growth at the back. Prefer std::array when the size is fixed at compile time, std::deque when you need fast inserts at both ends, and std::list when you insert in the middle often with stable iterators.

C++ Syntax

Common ways to create and use a std::vector:

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

std::vector<int> a;                  // empty
std::vector<int> b = {1, 2, 3};      // three elements
std::vector<int> c(5, 0);            // five zeros

b.reserve(10);                       // prepare capacity
b.push_back(4);                      // 1 2 3 4
int first = b.front();               // 1
int last  = b.back();                // 4
int x     = b[2];                    // 3, no check
int y     = b.at(2);                 // 3, checked
b.pop_back();                        // 1 2 3
std::size_t n = b.size();
std::size_t cap = b.capacity();

std::vector vs std::array vs std::deque

These three look alike but behave differently. Here is a quick comparison:

Featurestd::vectorstd::arraystd::deque
Size can growYesNo (fixed)Yes
Access by index x[i]FastFastFast
Add at the backFastN/AFast
Add at the frontSlow (no push_front())N/AFast
Memory layoutOne contiguous blockOne contiguous blockSeveral blocks
capacity() / reserve()YesNoNo
data() pointerYesYesNo

C++ Examples

Three short programs that show how std::vector works.

Example 1 — Grow a Vector with push_back()

A first program that adds values at the back, then reads both ends and the size.

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

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

    v.push_back(40);
    v.push_back(50);

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

How It Works

  1. std::vector<int> v = {20, 30} starts with two elements in one contiguous block.
  2. Each push_back() adds a value at the end. The vector is now 20 30 40 50.
  3. front() reads the first element, back() reads the last one, and size() counts all four.

Example 2 — Use reserve() Before Many push_back() Calls

When you know the final count, reserve capacity once so the vector does not reallocate in the loop.

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

int main() {
    std::vector<int> v;
    v.reserve(5);

    for (int i = 1; i <= 5; ++i) {
        v.push_back(i * 10);
    }

    std::cout << "size=" << v.size() << '\\n';
    std::cout << "capacity=" << v.capacity() << '\\n';
    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\\n';
    return 0;
}

How It Works

  1. reserve(5) prepares storage for five elements without creating them yet.
  2. The loop adds five values. Because capacity was ready, no reallocation was needed.
  3. Size is 5 and capacity is at least 5. Exact capacity can be larger on some libraries, but the idea is one reservation.

Example 3 — Access by Index and with data()

A vector supports fast [] / at(), and data() gives a pointer to the contiguous storage.

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

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

    std::cout << "v[1] = " << v[1] << '\\n';
    std::cout << "v.at(2) = " << v.at(2) << '\\n';
    std::cout << "data[0] = " << v.data()[0] << '\\n';
    return 0;
}

How It Works

  1. v[1] reads the second element without a bounds check.
  2. v.at(2) reads the third element and throws if the index is out of range.
  3. v.data() is a pointer to the first element. Contiguous storage makes data()[0] the same as v[0].

Next: C++ std::vector Functions Index

After you know what a std::vector is, open the function index for a full tutorial on each member function.

Start withWhy
push_back()Add an element at the end
reserve()Prepare capacity before a growth loop
at()Read an element with a bounds check
size()Count how many elements you store

Full list: C++ std::vector Functions index (/cpp/vector/function).

Frequently Asked Questions

std::vector<T> is a container from <vector>. It is a dynamic contiguous array: elements sit in one block, you can grow it with push_back, and you can read elements by index.
size() is how many elements you have. capacity() is how many fit in the current block without reallocating. reserve() raises capacity without changing size.
std::array has a fixed size known at compile time and never grows. std::vector can grow and shrink at run time and tracks size vs capacity.
Both allow index access. A vector uses one contiguous block and is fastest at the back. A deque is fast at both ends but is not one contiguous block and has no capacity() or reserve().
Open the C++ std::vector Functions index. It lists every member function, from push_back() and reserve() to at() and swap(), each with its own tutorial.

Browse every vector function

Open the functions index for categorized guides with examples and FAQs.

Vector Functions Index →

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.

8 people found this page helpful