What is the easiest way to initialize a std::vector with hardcoded elements?
Categories:
Easiest Ways to Initialize a std::vector
with Hardcoded Elements in C++

Discover the most straightforward and modern C++ techniques for initializing std::vector
containers with a predefined set of hardcoded values.
Initializing a std::vector
with a specific set of hardcoded elements is a common task in C++ programming. While there are several ways to achieve this, modern C++ (C++11 and later) provides elegant and concise methods that significantly improve readability and reduce boilerplate code. This article explores the easiest and most recommended approaches, focusing on initializer lists and direct construction.
Understanding std::vector
Initialization
std::vector
is a dynamic array that can grow or shrink in size. When you need to populate it with a fixed set of values at compile time, hardcoding these elements directly into the initialization statement is often the most efficient and readable approach. The methods discussed here leverage C++'s powerful initialization features to make this process seamless.
flowchart TD A[Start] --> B{Need hardcoded vector?} B -- Yes --> C[Use Initializer List (C++11+)] C --> D[Example: `std::vector<int> v = {1, 2, 3};`] B -- No --> E[Other Initialization Methods] E --> F[End]
Decision flow for initializing std::vector
with hardcoded elements.
Method 1: Initializer List (C++11 and Later)
The initializer list is by far the most idiomatic and recommended way to initialize a std::vector
with hardcoded elements in modern C++. Introduced in C++11, it allows you to provide a comma-separated list of values enclosed in curly braces {}
directly during declaration. This method is concise, clear, and efficient.
#include <vector>
#include <string>
#include <iostream>
int main() {
// Initialize a vector of integers
std::vector<int> numbers = {10, 20, 30, 40, 50};
// Initialize a vector of strings
std::vector<std::string> fruits = {"apple", "banana", "cherry"};
// Print elements to verify
std::cout << "Numbers: ";
for (int n : numbers) {
std::cout << n << " ";
}
std::cout << std::endl;
std::cout << "Fruits: ";
for (const std::string& f : fruits) {
std::cout << f << " ";
}
std::cout << std::endl;
return 0;
}
Using an initializer list to populate std::vector
with various data types.
={...}
is a form of copy-list-initialization, while {...}
is direct-list-initialization. Both work for std::vector
and are generally equivalent in this context, but direct-list-initialization is often preferred for consistency and to avoid potential implicit conversions.Method 2: Direct Construction with Iterators (Less Common for Hardcoded)
While not as direct for hardcoded values as initializer lists, you can also construct a std::vector
from a range defined by iterators. This is more commonly used when copying from another container or an array. For hardcoded elements, you'd typically use a C-style array and then construct the vector from its beginning and end pointers (which act as iterators).
#include <vector>
#include <iostream>
#include <array>
int main() {
// Using a C-style array
int c_array[] = {100, 200, 300};
std::vector<int> vec_from_c_array(std::begin(c_array), std::end(c_array));
// Using std::array (C++11)
std::array<double, 3> std_arr = {1.1, 2.2, 3.3};
std::vector<double> vec_from_std_array(std_arr.begin(), std_arr.end());
std::cout << "Vector from C-array: ";
for (int n : vec_from_c_array) {
std::cout << n << " ";
}
std::cout << std::endl;
std::cout << "Vector from std::array: ";
for (double d : vec_from_std_array) {
std::cout << d << " ";
}
std::cout << std::endl;
return 0;
}
Initializing std::vector
from C-style arrays and std::array
using iterators.
std::vector
.Legacy Methods (Pre-C++11)
Before C++11, initializing a std::vector
with hardcoded elements was often more cumbersome. Common approaches included adding elements one by one or using a temporary C-style array. While still functional, these methods are generally discouraged in modern C++ due to their verbosity and potential for errors compared to initializer lists.
#include <vector>
#include <iostream>
int main() {
// Method 1: Push_back elements individually
std::vector<int> old_vec_1;
old_vec_1.push_back(1);
old_vec_1.push_back(2);
old_vec_1.push_back(3);
// Method 2: Using a temporary C-style array and then copying
// (This is essentially what the iterator constructor does, but more manual)
int temp_arr[] = {4, 5, 6};
std::vector<int> old_vec_2(temp_arr, temp_arr + sizeof(temp_arr) / sizeof(temp_arr[0]));
std::cout << "Legacy Vector 1: ";
for (int n : old_vec_1) {
std::cout << n << " ";
}
std::cout << std::endl;
std::cout << "Legacy Vector 2: ";
for (int n : old_vec_2) {
std::cout << n << " ";
}
std::cout << std::endl;
return 0;
}
Examples of pre-C++11 std::vector
initialization methods.