Home C++ Vectors Functions push_back() C++ std::vector push_back() Function 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.
v.push_back(30);
names.push_back(std::string("Mia")); Call What 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 void push_back(const T& value);
void push_back(T&& value); // C++11 Parameters Parameter Description valueRequired. The value to add. A normal value is copied, and a temporary is moved into the vector.
Return Value Return Value Description Nothing push_back() returns void. Use back() if you want to see the new element.
Important
If size() equals capacity(), push_back() reallocates a bigger block and invalidates all iterators, references, and pointers . Call reserve() when you know how many elements you will add.
C++ Examples 1. Basic push_back 2. Build a sequence 3. With reserve Example 1 — Basic push_back() Add two numbers to the back of a small vector.
#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 The vector starts as 10 20. Each push_back() adds its value after the current last element. 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.
#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 The vector starts with the first two Fibonacci numbers, 0 and 1. 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(). 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.
#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 reserve(200) makes sure the next 100 push_back calls will not reallocate.int& first = v[0] keeps a reference to the first element.Because capacity was large enough, the reference still prints 10 after the loop. Frequently Asked Questions What does std::vector::push_back() do? It adds a new element at the back of the vector. The size grows by one.
Does push_back() return anything? No. It returns void. Use back() to see the element you added, or use emplace_back() (C++17+) which can return a reference.
Is push_back() fast on a vector? Yes, amortized constant time. Occasionally it reallocates when capacity is full, which copies or moves all elements.
Are iterators and references still valid after push_back()? Only if no reallocation happened. If size equals capacity before the call, everything can become invalid. Prefer reserve() when you keep pointers.
What is the difference between push_back() and emplace_back()? push_back() takes an existing object. emplace_back() takes constructor arguments and builds the object in place (C++11).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion