Nothing Special   »   [go: up one dir, main page]

Open In App

8 Ways to Initialize Vector in C++

Last Updated : 06 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Initializing a vector means assigning some initial values to the std::vector elements. In this article, we will learn 8 different ways to initialize a vector in C++.

Using Initializer List

We can initialize a vector with the list of values enclosed in curly braces {} known as initializer list. The values of the list will be assigned sequentially i.e. 1st value will be assigned to the 1st element of vector, 2nd value to 2nd element and so on.

Syntax

vector<type> v = {val1, val2, val3,…};

where, val1, val2, val3,… are the initial values.

Example

C++
// C++ Program to initializ std::vector with
// initializer list
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Initializing std::vector with list of
    // multiple values
    vector<int> v = {11, 23, 45, 89};

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
11 23 45 89 

One by One Initialization

Vector can be initialized by pushing value one by one. In this method, an empty vector is created, and elements are added to it one by one using the vector::push_back() method. This method is mostly used to initialize vector after declaration.

Syntax

v.push_back(val);

where, val is the value which we have to insert.

Example

C++
// C++ Program to initialize std::vector by
// pushing values one by one
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v;

    // Pushing Value one by one
    v.push_back(11);
    v.push_back(23);
    v.push_back(45);
    v.push_back(89);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
11 23 45 89 

With a Single Value

We can initialize all the elements of the vector to a single value. We create a vector of a specified size and initialize all elements to the same value using vector constructor.

Syntax

vector<type> v(n, val);

where, n is the size and val is the initial value.

Example

C++
// C++ Program to initializ the std::vector
// with specific value
#include <bits/stdc++.h>
using namespace std;

int main() {

    // Initializing all the elements of a
      // vector using a single value
    vector<int> v(5, 11);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
11 11 11 11 11 

From an Array

We can also initialize a vector using plain old static arrays using vector constructor. This works by copying all the elements of the array to the newly created vector.

Syntax

vector<type> v(arr, arr + n);

where arr is the array name and n is the size of the array.

Example

C++
// C++ Program to initializ the std::vector
// from another array
#include <bits/stdc++.h>
using namespace std;

int main() {
    int arr[] = {11, 23, 45, 89};
    int n = sizeof(arr) / sizeof(arr[0]);

    // Initialize the std::vector v by arr
    vector<int> v = {arr, arr + n};

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
11 23 45 89 

From Another Vector

We can also initialize a newly created vector from an already created vector if they are of same type.

Syntax

vector<type> v2(v1.begin(), v1.end());

where v1 is the already existing vector.

Example

C++
// C++ Program to initializ the std::vector
// from another vector
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {11, 23, 45, 89};

    // Initialize the vector v2 from vector v1
    vector<int> v2(v1.begin(), v1.end());

    for (auto i : v2)
        cout << i << " ";
    return 0;
}

Output
11 23 45 89 

From Any STL Container

Vectors are flexible containers that can be initialized by any other already existing containers such as set, multiset, map, etc. if they are of same type.

Syntax

vector<type> v(first, last);

where first and last are the iterator to the first element and the element just after the last element in the range of STL container.

Using std::fill() Function

We can also use the std::fill function to initialize the whole or a part of a vector to the same value.

Syntax

fill(first, last, val);

where first and last are the iterator to the first element and the element just after the last element in the range of STL container and val is the value to be initialized with.

Example

C++
// C++ Program to initialize the std::vector
// using std::fill() method
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v(5);

    // Initialize vector v with 11
    fill(v.begin(), v.end(), 11);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
11 11 11 11 11 

Using std::iota() Function

The std::iota() function from the <numeric> library allows us to initialize a vector with consecutive values starting from the given value.

Syntax

std::iota(first, last, val);

where first and last are the iterator to the first element and the element just after the last element in the range of the vector and val refers to the starting value.

Example

C++
// C++ Program to initializ the std::vector
// using std::iota()
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v(5);

    // Using std::iota() to initialize vector v
      // with 11
    iota(v.begin(), v.end(), 11);

    for (auto i : v)
        cout << i << " ";
    return 0;
}

Output
11 12 13 14 15 




Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg