C++ std::vector push_back() Function

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

Definition

In C++, std::vector::push_back() adds a new element at the back of a std::vector. The size becomes one larger, and the new element becomes the last one.

It is the most common way to grow a vector. Prefer reserve() when you already know the final size. 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

Pass the value to add. There are two forms: copy and move.

C++
v.push_back(30);
names.push_back(std::string("Mia"));
CallWhat it does
v.push_back(30)Adds a copy of 30 after the current last element.
names.push_back(std::string("Mia"))Adds a temporary object. It is moved into the vector instead of being copied.

If the element needs several constructor arguments, emplace_back() can build it in place.

Library declarations
C++
void push_back(const T& value);
void push_back(T&& value);   // C++11

Parameters

ParameterDescription
valueRequired. The value to add. A normal value is copied, and a temporary is moved into the vector.

Return Value

Return ValueDescription
Nothingpush_back() returns void. Use back() if you want to see the new element.

C++ Examples

Example 1 — Basic push_back()

Add two numbers to the back of a small vector.

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

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

    v.push_back(30);
    v.push_back(40);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The vector starts as 10 20.
  2. Each push_back() adds its value after the current last element.
  3. The values appear in the order they were added: 10 20 30 40.

Example 2 — Build a Sequence with push_back()

Each new number is the sum of the last two numbers.

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

int main() {
    std::vector<int> v = {0, 1};

    for (int i = 0; i < 6; ++i) {
        v.push_back(d[v.size() - 1] + d[v.size() - 2]);
    }

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The vector starts with the first two Fibonacci numbers, 0 and 1.
  2. In each round, d[d.size() - 1] is the last number and d[d.size() - 2] is the one before it. Their sum is added with push_back().
  3. After six rounds, the vector holds eight numbers. The loop is able to use [] because a vector supports fast access by index.

Example 3 — Use reserve() with push_back()

Reserve room first so a reference to an early element stays valid while you grow the vector.

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

int main() {
    std::vector<int> v = {10, 20, 30};
    v.reserve(200);
    int& first = v[0];

    for (int i = 0; i < 100; ++i) {
        v.push_back(i);
    }

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

How It Works

  1. reserve(200) makes sure the next 100 push_back calls will not reallocate.
  2. int& first = v[0] keeps a reference to the first element.
  3. Because capacity was large enough, the reference still prints 10 after the loop.

Frequently Asked Questions

It adds a new element at the back of the vector. The size grows by one.
No. It returns void. Use back() to see the element you added, or use emplace_back() (C++17+) which can return a reference.
Yes, amortized constant time. Occasionally it reallocates when capacity is full, which copies or moves all elements.
Only if no reallocation happened. If size equals capacity before the call, everything can become invalid. Prefer reserve() when you keep pointers.
push_back() takes an existing object. emplace_back() takes constructor arguments and builds the object in place (C++11).

Next: rbegin()

Get a reverse iterator to the last element with std::vector.

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

13 people found this page helpful