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:
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 <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:
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 <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:
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:
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 <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:
Initialization after Vector Creation without Elements
Here, an empty vector is created before initialization. The syntax to create an empty vector is:
The syntax to assign initial values to a vector with the initializer_list, is:
The following program creates an empty vector and then assigns zeros to the elements:
#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:
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:
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 <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:
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 <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:
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:
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 <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:
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.