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 inbuilt functions to perform different operations and initialization hence it is always recommended to use vectors instead of arrays in C++.

Vector is part of STL(Standard Template Library) and hence it comes with many features such as dynamic size, etc. Varying-sized arrays are represented by vectors, which are sequence containers that can hold them.

Syntax Of Vector:

// If you want to initialize with Size

vector<type-variable> variable-name(size);

//Else

vector<type-variable> variable-name;

To understand it in more detail you can see what each element is shown in the Syntax represents.

  • type-variable: This is the type of element you want to store in your Vector. It can be anything such as Integer, Float, Chars, or String.
  • variable-name: This represents the name of the vector variable that you want to give for initialization and use it further.
  • size: This represents the initial size of the vector that you want. If you do not want any size then you can ignore and use the second option as shown above.

Let us see in the details how you can initialize vector in C++ in various ways with code examples below.

1. Using push_back function to initialize Vector

Suppose you want to initialize the vector by adding the element in the list or vector using its inbuilt 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 keep it as some vector variable is 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;
}

Output:

1 2 3

As you can see in the above code, using the push_back method I was able to add elements to the vector immediately and was able to get the output as well.

2. Initialize 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 a few initial values of 4,5,6 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{4,5,6};
 
    for (int x : newList)
        cout << x << " ";
 //Output: 4 5 6
    return 0;
}

Output:

4 5 6

If you want to initialize the vector with some values then you can use the curly braces with the elements inside it separated with commas as you can see in the above code.

3. Using Another Array with Help of Range Constructor To Initialize Vector

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[] = {7,8,9};
    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: 7 8 9
    return 0;
}

Output:

7 8 9

In this example, if you want to initialize a vector using an existing array in C++ then you can do that as well. All you will require is to get the size of the array and then use the array variable inside the vector initialization bracket.

4. Using Default Constructor of Different Vector to Initialize a New Vector

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;
}

Output:

1 2 3

In this code example, I am using the elements of the different vectors to create a new copy of the vector. You can use the fill constructor method to achieve it. In this method, all the elements from the different vectors are copied one by one to the new vector.

5. With Specific Size and Value using Default Constructor To Initiate The New Vector

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;
}

Output:

5 5 5 5

In the above program, all the values of the vector are initialized to number 5. If you want to vector to be initialized initially with some size and with specific values for all the elements then you can use the vector default constructor method to achieve it.

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

In this method, it is just a little modification of the above method. If you want to create a vector with a specific size and want to assign all the vector elements to the specific value then you can use the fill constructor method to achieve this.

//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;
}

Output:

20 20 20 20 20

In the above example, you can see that I initialize the vector with 5 elements and then assigned those elements to a specific value of 20 using the fill constructor method for vector initialization in C++.

6 Different Ways to Initialize a Vector in C++

Wrap Up

I hope you learned how to Initialize Vector in C++ in different ways. I have listed down at least 6 different ways that you can use to initialize vectors and assign them with values if required.

Let me know in the comment section if you think you know any better method than the one is discussed here. I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Declare An Empty Array in C++
  2. How To Convert Char to Int in C++

Leave a Comment