In C++, the most direct way to initialize a vector is list initialization: vector<int> marks = {90, 85, 78};. The vector constructor also supports a size with a fill value, copying from arrays, and copying from other vectors.
Vectors live in the <vector> header and grow at runtime, which is why they replace plain arrays in most modern C++ code.
How Do You Initialize a Vector in C++?
Write the values in braces after the vector's name. This is list initialization, available since C++11, and it works at the point of declaration:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> marks = {90, 85, 78};
for (int m : marks)
cout << m << " ";
// Output: 90 85 78
return 0;
}
8 Ways to Initialize a Vector in C++

1) An Empty Vector
Declaring a vector with no arguments creates it empty, ready to grow with push_back():
vector<int> scores; // empty, size 0
scores.push_back(90);
scores.push_back(85);
// scores now holds: 90 85
2) An Initializer List
Braces set the exact contents at creation. The = sign is optional:
vector<int> marks = {90, 85, 78};
vector<string> cities {"delhi", "mumbai", "goa"};
// marks holds: 90 85 78
3) A Size and a Fill Value
The two-argument constructor creates n copies of one value, the standard way to fill a vector with zeros or any other starting value:
vector<int> zeros(5, 0); // five elements, all 0
vector<int> sevens(3, 7); // three elements, all 7
// zeros holds: 0 0 0 0 0
4) A Size Only
With just a size, every element is value-initialized, which means 0 for numeric types and an empty string for string:
vector<int> counts(4); // four elements, all 0
// counts holds: 0 0 0 0
5) From an Array
The iterator-pair constructor copies a range, so passing the array's start and end pointers copies the whole array:
int arr[] = {10, 20, 30};
vector<int> v(arr, arr + 3);
// v holds: 10 20 30
6) From Another Vector
Passing an existing vector copies all of its elements. The iterator form copies a slice instead:
vector<int> original = {1, 2, 3, 4, 5};
vector<int> copy(original); // full copy
vector<int> firstThree(original.begin(), original.begin() + 3);
// firstThree holds: 1 2 3
7) With fill() After Creation
fill() from <algorithm> overwrites an existing vector's elements with one value, useful for resetting between test cases:
#include <algorithm>
vector<int> v(5);
fill(v.begin(), v.end(), -1);
// v holds: -1 -1 -1 -1 -1
8) A Vector of Vectors
Combining the size-and-value constructor with an inner vector creates a 2D grid in one line, here 3 rows and 4 columns of zeros:
vector<vector<int>> grid(3, vector<int>(4, 0));
// grid holds 3 rows: 0 0 0 0 / 0 0 0 0 / 0 0 0 0
The 2D vector lesson covers working with these grids in detail.
When to Use Each Way
| Situation | Use |
|---|---|
| Values known at compile time | {90, 85, 78} |
| Fixed size, one starting value | vector<int> v(n, value) |
| Fixed size, zeros | vector<int> v(n) |
| Size unknown, grows over time | Empty + push_back() |
| Copy existing data | Array or vector constructor |
| 2D grid | vector<vector<int>> g(r, vector<int>(c, 0)) |
Learn More About Vector Initialization
Declaring vs Initializing a Vector
A declaration names the vector and its element type: vector<int> scores;. Initialization gives it contents. With vectors the two can happen together, and an uninitialized vector is simply empty rather than full of garbage values, unlike a plain array. Checking is one call:
vector<int> scores;
cout << scores.size() << " " << scores.empty();
// Output: 0 1
A Full Program Testing the Common Forms
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a = {1, 2, 3};
vector<int> b(3, 9);
vector<int> c(a.begin(), a.begin() + 2);
for (int x : a) cout << x << " ";
cout << "| ";
for (int x : b) cout << x << " ";
cout << "| ";
for (int x : c) cout << x << " ";
// Output: 1 2 3 | 9 9 9 | 1 2
return 0;
}
Key Takeaways for Initializing Vectors
- Braces for known values -
vector<int> v = {1, 2, 3};is the modern default. v(n, value)for a filled vector - Andv(n)alone value-initializes numeric elements to 0.- Iterator pairs copy ranges - From arrays, other vectors, or slices of them.
- Empty is valid - A declared vector starts empty with size 0, then grows with
push_back(). - 2D grids nest the constructor -
grid(rows, vector<int>(cols, 0)). fill()resets - It overwrites existing elements rather than creating them.
Initialization is the first step of a vector's life; growing and shrinking it comes next. The push_back() lesson covers adding elements, and the full C++ vectors guide covers everything else.

By Keerthana Buvaneshwaran 