Using C++ Fill Constructor to Initialize Vectors

In this C++ Tutorial, we will discuss the Fill Constructor.

The C++ Fill Constructor is a handy way to both create and initialize vectors in a variety of ways. The Fill Constructor is pretty flexible, and can be used to create 2D Vectors as well, and also initialize them with some initial values.


Fill Constructor Syntax

As mentioned earlier, the Fill Constructor can be used to in two different situations, to initialize 1D Vectors as well as 2D vectors.

Fill Constructor for Vectors

There are two ways to use the Fill Constructor, either using curly brackets, or the = operator. (COLUMN_COUNT is an integer which determines the starting size of each vector )

vector<int> vec1 = vector<int>(COLUMN_COUNT);    // Method 1

vector<int> vec2{ vector<int>(COLUMN_COUNT) };   // Method 2

If you to have the vector initialized with a value besides 0, which is the default, use the below format. (DEFAULT is a placeholder for an integer)

vector<int> vec1 = vector<int>(COLUMN_COUNT, DEFAULT);    // Method 1

vector<int> vec2{ vector<int>(COLUMN_COUNT, DEFAULT) };   // Method 2

A popular alternative to initializer vectors is the initializer list method.

Fill Constructor for 2D Vectors

Here is the syntax required to initialize a 2D Vector using the fill array. The ROW_COUNT is an integer which decides how many vectors will be in the array, and COLUMN_COUNT decides the starting size of each vector.

vector<vector<int> > vec{ ROW_COUNT, vector<int>(COLUMN_COUNT) }; 

If you want to initialize the vector with some values (other than the default 0), then all you need to do is add an extra integer, which decides the initial value as shown below.

vector<vector<int> > vec{ ROW_COUNT, vector<int>(COLUMN_COUNT, 1) };

For more on 2D Vectors, and other ways to initialize them, refer to this tutorial on initializing 2D Vectors.


Example# 1

Here is a simple example using the Fill constructor to initialize a vector with the value 9.

#include <iostream>
#include <vector>
using namespace std;


int main() {

    vector<int> vec = vector<int>(10, 9);

    for (int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
    cout << endl;

}
9 9 9 9 9 9 9 9 9 9

Example# 2

Here is a short example showing how to initialize a 2D Vector of dimensions (3, 5) with all values being 1.

#include <iostream>
#include <vector>
using namespace std;


int main() {

    vector<vector<int> > vec{ 3, vector<int>(5, 1) };


    for (int i = 0; i < vec.size(); i++) {
        for (int j = 0; j < vec[i].size(); j++) {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }
}
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Alternate Method

Here is a slightly different way of using the C++ Fill Constructor. It’s the same thing, but just broken down into two separate steps instead of one. The comments explain everything pretty well.

int main()
{
    // specify the default value to fill vector
    int default_value = 1;
 
    // first, initialize a vector of ints with a given default value
    vector<int> v(N, default_value);
 
    // Use the fill constructor
    vector<vector<int> > matrix(M, v);
 
    return 0;
}

This marks the end of the C++ Fill Constructor Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments