What is the easiest way to initialize a std::vector with hardcoded elements in C++?



In C++, a std::vector is a container that is a part of the STL (Standard Template Library). It enables dynamic arrays which can adjust their size automatically. Depending on the requirements and version of C++ being used, there are many ways to initialize a std::vector with hardcoded elements.

Initializing a std::vector with Hardcoded Elements

Some of the approaches to initialize a std::vector with hardcoded elements in C++:

Initializing Vector Using Initializer List

An initializer list is a way to give initial values to a container like std::vector using curly braces {}. It was introduced in C++11, and it is used to create and fill a vector in one line.

Following is the syntax to std::vector using initializer list:

std::vector vec = {};

Example

This program creates a vector with fixed values and prints each value using a loop:

#include<iostream>
#include<vector>
using namespace std;
int main() {
   // Initializing vector with hardcoded values
   vector<int>v = {1, 3, 7, 9};
   for (auto i : v)
      cout<<i<<" ";
   return 0;
}

Following is the output to the above program:

1 3 7 9

Initializing Vector Using assign() Function

In C++, the assign() function is used to fill or replace the contents of a vector with new values. You can give the list of values inside curly braces {}, and it will store those values in the vector.

So, that any old values in the vector will be removed, and the new values will take their place when you want to set or reset a vector with specific elements.

Following is the syntax to std::vector using assign() Function

vector<int> v;
v.assign({});

Example

This program stores fixed values in a vector using assign() function and print all elements one by one:

#include<iostream>
#include<vector>
using namespace std;
int main() {
   vector<int>v;
   // Using assign() to set hardcoded values
   v.assign({1, 3, 7, 9});
   for (auto i : v)
      cout<<i<<" ";
   return 0;
}

Following is the output to the above program:

1 3 7 9

Initializing Vector Using accumulate() Function

In C++, the accumulate() function is used to add up all the elements in a container like a vector. It is defined in the <numeric> header. The function takes three arguments like the starting position, the ending position, and the initial value to start adding from (usually 0) and then it goes through each element in the vector and adds them all together.

Following is the syntax to std::vector using accumulate() function

std::vector<int>vec = {1,2,3};
accumulate(v.begin(), v.end(), 0)

Algorithm

Begin
   Initialize the vector with hardcoded elements.
   Use accumulate() to add all elements.
   Print the sum.
End

Example

This program adds all the elements in the vector and prints the total sum of the elements using accumulate() function:

#include<iostream>
#include<vector>
#include<numeric>
using namespace std;
int main() {
   // Initialize vector with hardcoded values
   vector<int> v = {2, 7, 6, 10};
   // Calculate and print the sum
   cout<<"Sum of all the elements is: ";
   cout<<accumulate(v.begin(), v.end(), 0);
   return 0;
}

Following is the output to the above program:

Sum of all the elements is: 25
Revathi Satya Kondra
Revathi Satya Kondra

Technical Content Writer, Tutorialspoint

Updated on: 2025-06-12T13:45:58+05:30

642 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements