How To Initialize a Vector in C++? (5 Different Ways)

In C++, a vector is a dynamic array-like data structure provided by the Standard Template Library (STL). It allows the storage of elements of the same type in a contiguous memory block and provides dynamic resizing, allowing elements to be added or removed efficiently.


Vectors also support random access to elements and offer a variety of useful member functions for manipulation and iteration. There are multiple ways to initialize a vector and here we are going to see 5 different ways to do so. Let's learn each of them one by one.


1. Initializing with Initializer List: 

It is very much similar to initializing an array data structure using curly brackets and the size of the vector is decided based on the number of elements present. 

//C++ Program to initialize vector using initializer
#include<iostream>
#include<vector>
using namespace std;

int main(){

    vector<int> nums = {1, 2, 3, 4, 5};

    for(int it : nums){
        cout<< it << " ";
    }

    return 0;
}
Output:
1 2 3 4 5

2. Initializing using push_back() function: 

It is a built-in function present in the vector used to push the element at the end. Read more to know about vector built-in functions.

//C++ Program to initialize vector one by one
#include<iostream>
#include<vector>
using namespace std;

int main(){

    vector<int> nums;

    nums.push_back(1);
    nums.push_back(2);
    nums.push_back(8);
    nums.push_back(0);

    for(int it : nums){
        cout<< it << " ";
    }

    return 0;
}
Output:
1 2 8 0


3. Initializing with Size and Default Value: 

Here we are declaring two values while creating the vector, the first value defines the size of the vector, and the second value is the default value assigned to all the elements.

//C++ Program to initialize vector by default value
#include<iostream>
#include<vector>
using namespace std;

int main(){

    //vector of size 5 with all elements initialized to 0
    vector<int> nums(5, 0);

    for(int it : nums){
        cout<< it << " ";
    }

    return 0;
}
Output:
0 0 0 0 0

4. Initializing with Range of Elements:

Here we are using one vector to initialize a new vector with the same elements.

//C++ Program to initialize one vector using another vector
#include<iostream>
#include<vector>
using namespace std;

int main(){

    vector<int> nums{7, 2, 3, 1, 5};
    // Create a new vector with the same elements as nums
    vector<int> copyNums(nums.begin(), nums.end()); 

    for(int it : copyNums){
        cout<< it << " ";
    }

    return 0;
}
Output:
7 2 3 1 5


5. Initializing with Array:

Here, in this case, we are going to use the array to initialize a vector. For this, we must know the size of the given array. We have calculated the size using sizeof() function. 
 
//C++ Program to initialize one vector using array
#include<iostream>
#include<vector>
using namespace std;

int main(){

    int arr[] = {1, 2, 3, 4, 5};
    //size of given array
    int n = sizeof(arr) / sizeof(arr[0]);

    vector<int> nums(arr, arr + n);

    for(int it : nums){
        cout<< it << " ";
    }

    return 0;
}
Output:
7 2 3 1 5

So these are the few different ways in which we can initialize a vector. All these initialization is performed within O(n) time complexity. 

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS