Question
How to Initialize std::vector with Hardcoded Values in C++
Question
I can create an array and initialize it like this:
int a[] = {10, 20, 30};
How can I create a std::vector and initialize it in a similarly clean and elegant way?
The best approach I currently know is:
std::vector<int> ints;
ints.push_back(10);
ints.push_back(20);
ints.push_back(30);
Is there a better way to initialize a std::vector with hardcoded elements?
Short Answer
By the end of this page, you will understand the simplest ways to initialize a std::vector with fixed values in C++, when to use each approach, and how modern C++ makes vector initialization much cleaner than repeated push_back calls.
Concept
std::vector is a dynamic array in C++. Unlike a built-in array, it can grow and shrink at runtime. Because of that flexibility, C++ provides several ways to create and fill a vector.
When you already know the values you want to store, the most readable approach in modern C++ is usually list initialization using braces:
std::vector<int> ints = {10, 20, 30};
or simply:
std::vector<int> ints{10, 20, 30};
This works because std::vector supports initializer lists. An initializer list lets you pass a fixed set of values directly when constructing the vector.
This matters in real programming because:
- it makes code shorter and easier to read
- it reduces repetitive
push_backcalls - it clearly shows the vector's initial contents
- it avoids accidental mistakes from forgetting an element or adding them in multiple places
Before C++11, developers often had to use less elegant approaches, such as creating an array first and copying its contents into the vector. In modern C++, brace initialization is the standard beginner-friendly solution.
Mental Model
Think of a std::vector like a resizable shopping basket.
- A built-in array is like a fixed egg carton: its size is decided immediately.
- A
std::vectoris like a basket that can hold more items later.
If you already know the starting items, brace initialization is like filling the basket all at once:
std::vector<int> ints{10, 20, 30};
Using repeated push_back is like putting items in one by one after picking up the basket. That works, but it is more verbose when the items are already known.
Syntax and Examples
The most common ways to initialize a std::vector are below.
1. Initialize with hardcoded values using braces
#include <vector>
std::vector<int> nums{10, 20, 30};
This is the cleanest modern C++ approach.
You may also see:
std::vector<int> nums = {10, 20, 30};
Both forms are valid.
2. Initialize with repeated copies of one value
std::vector<int> nums(3, 100);
This creates:
{100, 100, 100}
Be careful: this is different from brace initialization.
3. Construct from an array
a[] = {, , };
;
Step by Step Execution
Consider this example:
#include <vector>
int main() {
std::vector<int> ints{10, 20, 30};
return 0;
}
Here is what happens step by step:
- The program reaches
std::vector<int> ints{10, 20, 30}; - A vector named
intsis created. - The type inside the vector is
int. - The brace list
{10, 20, 30}is passed to the vector constructor. - The vector stores three elements in order:
ints[0]becomes10ints[1]becomes20ints[2]becomes30
- The vector size becomes
3.
You can verify that with this traceable example:
Real World Use Cases
Hardcoded vector initialization is common when a program starts with a known set of values.
Common examples
-
Menu choices
std::vector<std::string> options{"start", "settings", "quit"}; -
Allowed status codes
std::vector<int> validCodes{200, 201, 204}; -
Default configuration values
std::vector<double> thresholds{0.25, 0.5, 0.75}; -
Test data in unit tests
std::vector<int> input{1, 2, 3, 4, 5}; -
Coordinate or sample data in small programs
std::vector<int> points{10, , , };
Real Codebase Usage
In real projects, developers often use vector initialization in a few common patterns.
1. Default data setup
A vector may be created with default values near the point of declaration:
std::vector<std::string> roles{"admin", "editor", "viewer"};
This keeps the code readable and avoids scattered push_back calls.
2. Test fixtures and sample input
In tests, short inline vectors are very common:
std::vector<int> expected{2, 4, 6};
This makes expected values easy to compare.
3. Guarded setup before processing
Developers may initialize a vector and then validate it:
std::vector<int> ports{80, 443, 8080};
if (ports.empty()) {
return;
}
4. Copying from existing ranges
When values come from arrays or other containers, range constructors are often used:
int raw[] = {, , };
;
Common Mistakes
Here are common mistakes beginners make when initializing std::vector.
1. Confusing parentheses with braces
These two do different things:
std::vector<int> a(3, 10);
std::vector<int> b{3, 10};
a becomes:
{10, 10, 10}
b becomes:
{3, 10}
How to avoid it
- Use braces when listing explicit elements.
- Use parentheses when calling a constructor with size and value.
2. Forgetting to include the header
Broken code:
std::vector<int> nums{1, 2, 3};
This will fail if you forget:
Comparisons
Here is a comparison of common ways to create a std::vector.
| Approach | Example | Best for | Notes |
|---|---|---|---|
| Brace initialization | std::vector<int> v{10, 20, 30}; | Known hardcoded values | Most readable in modern C++ |
| Copy-list initialization | std::vector<int> v = {10, 20, 30}; | Known hardcoded values | Also common and clear |
| Repeated value constructor | std::vector<int> v(3, 10); | Same value repeated | Creates 3 elements, each 10 |
| Range constructor | std::vector<int> v(begin, end); | Copying from arrays or other containers |
Cheat Sheet
#include <vector>
Initialize with hardcoded values
std::vector<int> v{10, 20, 30};
std::vector<int> v = {10, 20, 30};
Initialize with repeated value
std::vector<int> v(3, 5); // {5, 5, 5}
Copy from an array
int a[] = {10, 20, 30};
std::vector<int> v(std::begin(a), std::end(a));
Add items later
std::vector<int> v;
v.push_back(10);
v.push_back(20);
FAQ
Can I initialize a std::vector like an array in C++?
Yes. In modern C++, you can use brace initialization:
std::vector<int> v{10, 20, 30};
Is std::vector<int> v = {1, 2, 3}; valid?
Yes. This is valid C++ and is a common way to initialize a vector with fixed values.
What is the difference between vector<int>(3, 10) and vector<int>{3, 10}?
vector<int>(3, 10) creates three elements, each equal to 10.
vector<int>{3, 10} creates two elements: 3 and 10.
Should I use push_back or brace initialization?
Use brace initialization when the values are already known. Use push_back when values are produced later by logic, loops, input, or function results.
Does this require C++11?
Yes, initializer-list vector syntax is part of modern C++ and is available from C++11 onward.
Can I initialize a vector from an existing array?
Mini Project
Description
Build a small C++ program that stores a list of hardcoded exam scores in a std::vector, then prints them and calculates the total. This demonstrates clean vector initialization with brace syntax and basic iteration over vector elements.
Goal
Create and use a std::vector<int> initialized with hardcoded values, then process its contents.
Requirements
- Create a
std::vector<int>with at least five hardcoded scores using brace initialization. - Print each score to the console.
- Calculate the sum of all scores.
- Print the total and the number of scores.
Keep learning
Related questions
Advantages of Brace Initialization in C++
Learn why C++ brace initialization is often clearer and safer than other object initialization styles, with examples and common pitfalls.
Basic Rules and Idioms for Operator Overloading in C++
Learn the core rules, syntax, and common idioms for operator overloading in C++, including member vs non-member operators.
C++ Aggregates, Trivial Types, Trivially Copyable Types, and PODs Explained
Learn what aggregates, trivial types, trivially copyable types, and PODs mean in C++, how they differ, and why they matter.