How do I initialize a vector later?

The vector is a class in the vector library which is included into the program. From the same vector class, different vector objects can be instantiated. Vector object declaration without memory allocation for the elements is instantiation. Vector object declaration with memory allocation for the elements is still instantiation.

Ideally, initialization is done during instantiation. However, when declaration is done without memory allocation for the elements, then initialization has to be done by assigning or pushing_back (into), initial values.

This information gives two situations for initialization: initialization with vector creation as a definition or initialization after declaration without definition by assignment (or pushing_back into).

Vector creation can be classified in two categories: vector creation with elements and vector creation without elements. The objective of this article has to be interpreted as: how to create a vector with all initial elements being zero. In order to have a vector with all elements zero the type of the elements has to be int or float or their variants. We will use type int in this article.

Initialization at Vector Creation with Elements

Creating with Initializer_list

Initializer_list is a list of values of the same type separated by commas, and delimited by braces. There are two syntaxes for creating a vector with initializer_list. The syntaxes are:

vector& operator=(initializer_list<T>)

and

vector(initializer_list<T>, const Allocator& = Allocator())

To create a vector with all zeroes, all the values in the initializer_list should all simply be zeros. The following program illustrates this using the first syntax where the initializer_list is assigned to the vector object with the assignment operator:

#include <iostream>

    #include <vector>

    using namespace std;
    int main()
    {
        vectorvtr = {0, 0, 0, 0, 0};

        for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ' ';
cout<<endl;
        return 0;
    }

The output is:

0 0 0 0 0

In the program header, the vector library was included. Vector declaration is found in the first statement in the main function. The vector content is displayed at the rest of the code in the main function.

For the second syntax, the second parameter is optional and will not be included in the following program. For this second syntax, the initializer_list is the argument of the vector constructor function. The following program illustrates the use of the second syntax:

    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
        vectorvtr({0, 0, 0, 0, 0});

        for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ' ';
cout<<endl;
        return 0;
    }

The output is:

0 0 0 0 0

The input was five zeros, and the output is the same five zeros.

Number of Vector Elements Known in Advance

The number of vector elements may be known in advance, but the actual elements may not be known in advance. In this case, the vector should still be initialized to zeros. The vector creation syntax for this is:

vector(size_type n, const T& value, const Allocator& = Allocator())

The third parameter is optional and will not be part of the following program. The first argument is the number of elements and the second argument is the default value, which in this case is zero for all the elements. The following program shows how to use this syntax for a vector size of 5:

    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
        vectorvtr(5, 0);

        for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ' ';
cout<<endl;
        return 0;
    }

The output is:

0, 0, 0, 0, 0

Initialization after Vector Creation without Elements

Here, an empty vector is created before initialization. The syntax to create an empty vector is:

vector() noexcept(noexcept(Allocator())) : vector(Allocator()) { }

The syntax to assign initial values to a vector with the initializer_list, is:

void assign(initializer_list<T>)

The following program creates an empty vector and then assigns zeros to the elements:

    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
        vectorvtr;
vtr.assign({0, 0, 0, 0, 0});

        for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ' ';
cout<<endl;
        return 0;
    }

The output is:

0 0 0 0 0

The first statement in the main function creates the empty vector. The second statement is the initialization using the assign() member function. The argument of the assign() member function is the initializer_list with each element in zero.

The syntax to assign default values to a vector, when the number of elements is known in advance, is:

void assign(size_type n, const T& u);

With this syntax, the first argument is the number of elements in the vector, and the second argument is the default value. For initialization with zeros, just make the default value zero. Remember that this is to be done when an empty vector has been created. The following program illustrates this:

    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
        vectorvtr;
vtr.assign(5, 0);

        for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ' ';
cout<<endl;
        return 0;
    }

The output is:

0 0 0 0 0

Pushing Back

After an empty vector has been created, any number of zeros can be pushed_backed into the vector for initialization as illustrated in the following program:

    #include <iostream>

    #include <vector>

    using namespace std;
    int main()
    {
        vectorvtr;
vtr.push_back(0); vtr.push_back(0); vtr.push_back(0);
vtr.push_back(0); vtr.push_back(0);

        for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ' ';
cout<<endl;
        return 0;
    }

The output is:

0 0 0 0 0

Vector of Default Type Values and Initialization

If the number of elements is known in advance, then without indicating the values, the default values of the type will be the initial values. The default value for the type int is zero. The syntax for the creation of such a vector is:

explicit vector(size_type n, const Allocator& = Allocator())

The second argument is optional and can be omitted. The first argument is the size of the vector. If the vector type is int, then the initial values will all be zeros. The following program illustrates this:

    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
        vectorvtr(5);

        for (int i=0; i<vtr.size(); i++)
cout<<vtr[i] << ' ';
cout<<endl;
        return 0;
    }

The output is:

0 0 0 0 0

Conclusion

The size of the vector in all the code samples above is 5. Any other number can be used. A vector can be initialized with all zeros in three principal ways: A) use the initializer_list, B) use the assign() vector member function to an empty vector (or push_back()), or C) use int or float as the template parameter specialization for a vector of initially only default values.

How do you define and initialize a vector?

There are four ways of initializing a vector in C++:.
By entering the values one-by-one..
By using an overloaded constructor of the vector class..
By the help of arrays..
By using another initialized vector..

What function is used to initialize a vector?

How to Initialize a Vector in C++ Using the push_back() Method. push_back() is one out of the many methods you can use to interact with vectors in C++. It takes in the new item to be passed in as a parameter. This allows us to push new items to the last index of a vector .

Do vectors need to be initialized?

You will not be able to create a Vector, which is not initialized. The default constructor constructs an empty container with a default-constructed allocator. Here are some examples on how to initialise a Vector if you already know its content or size.

How do you initialize a vector with a specific size?

To initialize a two-dimensional vector to be of a certain size, you can first initialize a one-dimensional vector and then use this to initialize the two-dimensional one: vector<int> v(5); vector<vector<int> > v2(8,v); or you can do it in one line: vector<vector<int> > v2(8, vector<int>(5));