Quick C++ std::vector initialization

  • Post category:C++
  • Post comments:0 Comments
  • Reading time:3 mins read

The C++ vector can be initialized in different ways. This post provides basic examples of vector initialization ignoring all scenarios where the content of the new vector is copied from another array or container.

Initialization using vector constructor (identical values)

A std::vector can be initialized using the std::vector constructor if we need a vector containing M elements that are all equal to a given value or object.

Therefore, we can just construct our vector in this way:

#include <vector>

// myCnt will contain M integers equal to 0
std::vector myCnt(M, 0);

// myCnt2 will contain M integers equal to -5
std::vector myCnt2(M, -5);

Initialization using the std::iota() function (increasing values)

The std::iota() function is very useful when we need to initialize the vector with an increasing group of values.

For easy of understanding, we can initialize our vector with M integers equal to 0, 1, 2, … , M – 1 as shown in this example:

#include <vector>       // std::vector
#include <iostream>     // std::cout
#include <numeric>      // std::iota

int main() {
    std::vector<int> myCnt(20,0); // we need to allocate memory

    std::iota(myCnt.begin(), myCnt.end(), -10);

    std::cout << "myCnt: ";
    for (int& i : myCnt) std::cout << ' ' << i;
    std::cout << std::endl;

    return 0;

This example will print the following output:

myCnt:  -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9
Press any key to continue . . .

The std::iota() does not allocate memory. For this reason, we need to construct the vector as shown previously.
This function operates on a user-defined range and it overwrites already existing values with the new ones.

Initialization using std::generate() function (random values)

We can initialize a vector using the function std::generate() that will call a given function for each element in a user-defined range.

For example, we can generate a vector of values randomly chosen between 0 and 99 in the following way:

#include <vector>
#include <iostream>     // std::cout
#include <algorithm>    // std::generate
#include <ctime>        // std::time

// This function returns a random number
int RandomNumber() { return (std::rand() % 100); }

int main() {
    std::vector<int> myCnt2(20);

    std::generate(myCnt2.begin(), myCnt2.end(), RandomNumber);

    std::cout << "myCnt2: ";
    for (int& i : myCnt2) std::cout << ' ' << i;
    std::cout << std::endl;

    return 0;

The code above will print something like:

myCnt2:  78 63 38 71 65 69 83 83 1 16 26 30 59 12 19 60 46 1 60 90
Press any key to continue . . .

In the example above, the values are not guaranteed to be unique.

The function generator must accept no arguments and it should return the desired element type.

How many ways do you know to initialize a std::vector? Share your experience with us!

Pietro L. C

C++ Software Engineer, Ph.D. working in Malta on embedded systems with a vision in mind: design best software to solve complex problems in the real world.
0 0 votes
Rated by members
Notify of
Inline Feedbacks
View all comments