A std::vector (or vector if you using namespace std;) is a C++ data structure. vector is a dynamic array that provides fast lookups. vector can be included by #include <vector>, is part of the Standard Template Library (STL), and is in “standard” scope std::. A vector can be initialized in many different ways. This is not an exhaustive list but what is useful and more common.

What is initialization

Initialization refers to the process of setting the value of a variable for the first time at the time of construction. Regarding, vector we can set an initial values (elements).

vector Empty Initialization

Let’s begin with the default constructor. Here the vector vec1 is initialized empty with size zero (i.e., filled with zero elements).

vector<int> vec1;

// or
vector<int> vec2{};

What are the curly braces? Glad you asked…

List-Initialization

Starting with C++11, list-initialization can be used anywhere a variable can be created. Curly braces {...} use the list-initialization.

// vec2 has no values
vector<int> vec2 { };

// vec3 has [3,4,5]
vector<int> vec3 { 3, 4, 5 };

ℹ️ Advanced technical info: There is direct-list-initialization var {...} and copy-list-initialization var = {...} which I haven’t seen a good argument for one or the other. However, for other types direct-list-initialization is preferred most of the time as it prevents narrowing (as compile time error). For vector it doesn’t seem to matter, from what I’ve seen.

We can list-initialize a vector of objects. For example, we have a struct (or can be a class) of Foo type.

struct Foo
{
    int x = -1;

    Foo()
    {
        println("constructing foo, x = {}", x);
    }
    Foo(int xIn)
    {
        x = xIn;
        println("constructing foo, x= {}", x);
    }
};

vector<Foo> vec8{ {}, {100} };

Which initializes vec8 with a Foo() default constructor and Foo(int) constructor.

Output will be:

Constructing Foo, x = -1
Constructing Foo, x= 100

Initializing with vector constructor

Initial Size and Initial Default Value

With vector constructor we can specify the initial size and the initial default value. In this example the initial size is 3 and the initial default value is -1.

int initializeSize = 3;
int initializeDefaultValue = -1;
vector<int> vec3(initializeSize, initializeDefaultValue);

The vector will now contain three elements of -1: {-1, -1, -1}.

Initalize vector with all zeros

We can also only specify the size. The vector is zero initializes 5 elements.

// zero initialized
vector<int> vec4(5);

vector will contain: [0,0,0,0,0].

⚠️ : We can’t do vector<int> v(3) = {1,2,3}.

Another example, with objects we call the default constructor. If we used our Foo class from above with:

vector<Foo> vec9(5);

We get output:

Constructing Foo, x = -1
Constructing Foo, x = -1
Constructing Foo, x = -1
Constructing Foo, x = -1
Constructing Foo, x = -1

Intialize From C-Array

We can initialize a vector from a C-style array:

int cArr[] {1,2,3,4};

vector<int> vec11(begin(cArr), end(cArr));

vec11 will now contain: [1,2,3,4].

Initialize 2D Vector

We can also

int R = 3, C = 5;
int initVal = 0;
vector<vector<int>> vec2D(R, vector<int>(C, initVal));

which will create a vector that has 3 rows and 5 columns of initial value of 0:

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Thanks for stopping by!