In this C++ tutorial, you will learn how to add elements to a vector using vector::push_back() function, with examples.

Add an Element to a C++ Vector with push_back()

Use the push_back() member function to add an element at the end of a C++ std::vector. Each successful call adds one element and increases the size of vector by one.

For example, if a vector contains 10 and 20, calling push_back(30) appends 30, producing 10, 20, 30.

C++ vector::push_back() Syntax

</>
Copy
vectorName.push_back(value);

Here, vectorName is the vector and value is the element to append. The value must be compatible with the vector’s element type.

For a vector<int>, for example, you can append an integer as follows.

</>
Copy
nums.push_back(42);

C++ vector push_back() Examples

1. Add an Integer to a C++ Vector

In this example, we will define a Vector of Integers, and add an integer to this vector using push_back() function.

C++ Program

</>
Copy
#include <iostream>
#include <vector>
using namespace std;

int main() {
   vector<int> nums;
   nums.push_back(24);
   nums.push_back(81);
   nums.push_back(57);

   for(int num: nums)
      cout << num << " ";
}

Initially, when we declared the vector, there are no elements in it. The size of vector is zero.

When we added first element 24, the element is added as the first element in the vector.

When we added second element 81, the element is added as the second element in the vector. In other words, after the current last element 24 which is first element. And so on for other elements.

Output

24 81 57

The three calls to push_back() append the values in the same order in which they are called. After the third call, the vector contains three elements.

2. Add Multiple Elements with C++ vector push_back()

push_back() adds one element per call. To append several individual values, call it once for each value.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers;

    numbers.push_back(10);
    numbers.push_back(20);
    numbers.push_back(30);
    numbers.push_back(40);

    for (int number : numbers) {
        std::cout << number << " ";
    }
}

Output

10 20 30 40

If you need to append an entire range of elements from another container, vector::insert() is generally more direct than writing a separate push_back() call for every element.

3. Add Strings to a C++ Vector with push_back()

push_back() is not limited to integers. It can append values of the element type stored by the vector. The following example appends strings to a vector<string>.

</>
Copy
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> names;

    names.push_back("Amit");
    names.push_back("Neha");
    names.push_back("Ravi");

    for (const std::string& name : names) {
        std::cout << name << "\n";
    }
}

Output

Amit
Neha
Ravi

4. Check Vector Size After push_back()

Each push_back() call adds one element, so you can observe the vector’s size increasing after each append.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<int> nums;

    std::cout << nums.size() << "\n";

    nums.push_back(24);
    std::cout << nums.size() << "\n";

    nums.push_back(81);
    std::cout << nums.size();
}

Output

0
1
2

5. Use push_back() with a 2D C++ Vector

A two-dimensional vector is a vector whose elements are themselves vectors. You can therefore use push_back() to append a complete row.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<std::vector<int>> matrix;

    matrix.push_back({1, 2, 3});
    matrix.push_back({4, 5, 6});

    for (const auto& row : matrix) {
        for (int value : row) {
            std::cout << value << " ";
        }
        std::cout << "\n";
    }
}

Output

1 2 3
4 5 6

How vector push_back() Affects Size and Capacity

The elements of a std::vector are stored contiguously. A vector also maintains storage capacity that can be larger than its current number of elements.

If there is unused capacity, push_back() can place the new element into the existing allocation. If there is not enough capacity, the vector may allocate a larger block of storage and move or copy its existing elements into that new storage before adding the new element.

This means size() increases by exactly one after push_back(), while capacity() does not necessarily increase on every call.

Storage of Vector in Memory

Elements of a Vector are stored in continuous memory location. So, when you try to add an element to the vector, and if next memory location is not available, whole vector is copied into a new location with more capacity and the element is added to the existing elements of vector at the end.

More precisely, reallocation occurs when the vector’s current capacity is insufficient for the new element. Because reallocation changes the underlying storage location, pointers, references, and iterators referring to vector elements may be invalidated.

C++ vector push_back() Time Complexity

Appending with push_back() has constant amortized time complexity. Most calls can append directly into already allocated storage. Occasionally, a call requires reallocation and moving or copying the existing elements, making that individual operation linear in the vector’s current size.

If you know approximately how many elements will be appended, reserve() can be used beforehand to request sufficient capacity and potentially reduce the number of reallocations. Calling reserve() changes capacity, not the vector’s size.

C++ vector push_back() vs emplace_back()

Both push_back() and emplace_back() add an element at the end of a vector. With push_back(), you provide an object or a value that can be converted to the vector’s element type. With emplace_back(), constructor arguments can be passed so that the element is constructed directly at the end of the vector.

For simple types such as int, push_back() is straightforward and usually the clearest choice. For user-defined objects, emplace_back() can be convenient when you want to construct the element from its constructor arguments.

Why C++ Vector Has push_back() but Not push_front()

std::vector provides push_back(), but it does not provide push_front(). Inserting an element at the beginning of a vector generally requires shifting the existing elements to make room.

If you need to insert at a particular vector position, use insert(). If frequent insertion at both the front and back is a core requirement, another standard container such as std::deque may better match that access pattern.

Common C++ vector push_back() Mistakes

  • Remember that one push_back() call appends one element, not an entire unrelated sequence of elements.
  • Append a value that is compatible with the vector’s declared element type.
  • Do not assume that push_back() changes only the size; a capacity reallocation may also occur.
  • Do not keep using an iterator, pointer, or reference after a reallocation without verifying that it is still valid.
  • Do not use capacity() to determine how many elements are currently stored; use size().
  • Do not look for vector::push_front(); std::vector has no such member function.

C++ vector push_back() Editorial QA Checklist

  • Verify that every push_back() example adds the new value at the end of the vector.
  • Confirm that the vector size increases by one for each successful push_back() call.
  • Check that examples append values compatible with the vector’s element type.
  • When capacity is discussed, distinguish the current element count from allocated storage capacity.
  • When reallocation is mentioned, note its effect on affected iterators, pointers, and references.
  • Describe push_back() complexity as amortized constant time rather than claiming every individual call takes constant time.
  • Keep push_back(), insert(), reserve(), and emplace_back() roles distinct.

Summary of Adding Elements with C++ vector push_back()

In this C++ Tutorial, we learned how to add an element to the existing vector at the end, using push_back() function.

Use vector::push_back() when you want to append one element to the end of a C++ vector. Each call increases the vector’s size by one. If additional storage is required, the vector can reallocate its underlying storage automatically.