6 Different Ways to Initialize a Vector in C++

Learn how you can initialize a vector in C++ in 6 different ways. The vector in C++ acts as an array or list. Since it comes with great inbuild functions to perform different operations and initialization hence it is always recommended to use vectors instead of arrays in C++.

1. Using push_back function to initialize one by one.

Suppose you want to initialize the vector by adding the element in the list or vector using its inbuild function push_back(). This function adds the element at the back of the vector.

As shown in the below code, we are first initializing a vector and then adding the elements to the vector using the method push_back. This way you are able to initialize the vector with some values and not keeping it as some vector variable being NULL.

//CPP Program to Create Vector
//And initialize by adding elements one by one
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Create an empty vector
    vector<int> newList;
 
    newList.push_back(1);
    newList.push_back(2);
    newList.push_back(3);
 
    for (int x : newList)
        cout << x << " ";
 //Output: 1 2 3
    return 0;
}

2. Initilize Vector as Array in One Go

You can initialize vector as an array in C++. Below is the code example for this type of initialization. In the below code you can see that we have initialized a new vector with few initial values of 1,2,3 using curly braces. This set the vector having three elements at the start of that line.

//CPP Program to Create Vector
//And initialize by adding elements one by one
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    // Create a vector as an array
    vector<int> newList{1,2,3};
 
    for (int x : newList)
        cout << x << " ";
 //Output: 1 2 3
    return 0;
}

3. Using Another Array with Help of Range Constructor

We can use a range constructor to initialize vectors from elements of the already initialized array. In this one, we are already creating the array pointer, and then using that pointer we are letting the vector constructor construct a similar vector.

//CPP Program to Create Vector
//And initialize by adding elements one by one
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int arr[] = {1,2,3};
    int numElements = sizeof(arr)/sizeof(arr[0]); 
    // Create a vector from existing array
    vector<int> newList(arr, arr+numElements);
 
    for (int x : newList)
        cout << x << " ";
 //Output: 1 2 3
    return 0;
}

4. Using another vector with help of Default Constructor

You can use or initialize a vector with help of an already available vector using Default Constructor. In the below code if we already have an existing vector with some elements and we want to copy the same vector to another vector element or list then we can use the below code to do that.

//CPP Program to Initialize Vector
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    vector<int> oldVectorList = {1,2,3};
    // Create a vector from oldVectorList using Fill Constructor
    vector<int> newList(oldVectorList.begin(), oldVectorList.end());
 
    for (int x : newList)
        cout << x << " ";
 //Output: 1 2 3
    return 0;
}

5. With Specific Size and Value using Default Constructor

You can Initialize a vector in c++ with a specific size and value. Below is the code example of how you can do that. As in the below code, we are initializing the vector and telling the constructor that initially it will have 4 elements with default values of all those four elements set to 5.

//CPP Program to Initialize Vector
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n = 4;
    // Create a vector with specific size and value for all the elements
    vector<int> newList(n, 5);
 
    for (int x : newList)
        cout << x << " ";
 //Output: 5 5 5 5
    return 0;
}

In the above program, all the values of the vector are initialized to number 5.

6. Different way to Initialize all the elements to specific value with Fill Constructor

In this method, it is just the modification of the above method a little. In this one, we will be assigning all the elements of the vector after it is initialized with a number of elements.

//CPP Program to Initialize Vector
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    int n = 4;
    // Create a vector with specific size of 5 elements
    vector<int> newList(5);
    int givenValue = 20;
    fill(newList.begin(), newList.end(), givenValue);
 
    for (int x : newList)
        cout << x << " ";
 //Output: 20 20 20 20 20
    return 0;
}

If you liked our post related to how to initialize a vector in c++ then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Wanna read more interview-related questions? Check Other Post Related to Data Structures.

Share your love

2 Comments

Leave a Reply