6 Different ways to Initialize vector in C++

In this article, we are going to learn 6 different ways to Initialize vector in C++. When we create a vector, we can initialize a vector with default values or we can assign values to vector elements at runtime. Vector size can grow as per the user’s need at runtime. This makes vectors more flexible when compared to arrays in C++. In this article, we are going to learn these ways to initialize vector elements:

6 different ways to Initialize vector in C++


  • By using push_back() method
  • By using Initializer list.
  • By using fill function to assign same value to all elements.
  • By size and initializing same value to all elements.
  • By assigning an array to a vector.
  • By assigning a Vector to Vector.

so let us begin with our first method of vector initialization.

1. Initialize vector in C++ using push_back() method


#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int> vect;
    vector<string> vectstr;

    vect.push_back(1);
    vect.push_back(2);
    vect.push_back(3);

    vectstr.push_back("Sachin");
    vectstr.push_back("Kohli");
    vectstr.push_back("Dhoni");

    // print integer vector
    for (int i : vect)
        std::cout << i << " ";

    cout << endl;

    // print string vector
    for (string str : vectstr)
        std::cout << str << " ";

    return 0;
}

Output

1 2 3
Sachin Kohli Dhoni

2. Initialize vector in C++ By using the Initializer list


In this example, we are using the second method of initializing a vector. In this program, we are just using the initializer list at the time of vector declaration itself. let us understand this with our code.

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int> vect{ 1, 2, 3 };
    vector<string> vectstr{ "Sachin", "Kohli", "Dhoni" };

    // print int vector
    for (int i : vect)
        cout << i << " ";

    cout << endl;

    // print string vector
    for (string str : vectstr)
        std::cout << str << " ";

    return 0;
}



Output:

1 2 3
Sachin Kohli Dhoni

3. Initialize vector in C++ using fill function to assign same value to all elements


In this example, we are using the fill() function to initialize a vector. fill() function initializes a vector with the same value assigned to all the elements of the vector.

Here is a code example to show us how the fill() function works.


#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int> vect(5);
    vector<string> vectstr(3);

    int value = 100;
    string strval = "Sachin";

    fill(vect.begin(), vect.end(), value);
    for (int i : vect)
        cout << i << " ";
    cout << endl;
    fill(vectstr.begin(), vectstr.end(), strval);
    for (string str : vectstr)
        cout << str << " ";
}

Output:

100 100 100 100 100
Sachin Sachin Sachin

4. Initialize vector in C++ By size and initialize same value to all elements


In this example, we are initializing the vector by using its constructor in which we are passing the size of the vector and the value that we want to initialize to all the elements of the vector.

Here is a code example.

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    int len = 5;
    vector<int> vect(len, 100);
    vector<string> vectstr(len, "Sachin");
    
    for (int x : vect)
        cout << x << " ";

    cout << endl;

    for (string str : vectstr)
        cout << str << " ";

    return 0;
}

Output

100 100 100 100 100
Sachin Sachin Sachin Sachin Sachin

5. Initialize vector in C++ By assigning an array to a vector


In this example, we are using an array to assign values to a vector. This method works when we want to create a vector from an array. This is helpful in the array conversion to vector and gain the vector inbuilt functions support in our code.

Let us understand this with the below code example.

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    int intarr[] = { 1, 2, 3 };
    string strarr[] = { "Sachin","Kohli","Dhoni" };

    int len = sizeof(intarr) / sizeof(intarr[0]);
    int len1 = sizeof(strarr) / sizeof(strarr[0]);

    vector<int> vect(intarr, intarr + len);

    for (int x : vect)
        cout << x << " ";

    vector<string> vectstr(strarr, strarr + len1);

    cout << endl;

    for (string str : vectstr)
        cout << str << " ";

    return 0;
}

Output

1 2 3
Sachin Kohli Dhoni

6. Initialize vector in C++ By assigning a Vector to Vector


In this example, we are learning how to initialize a vector with another vector element value. Here we have a vector and then we are assigning its values to another vector. This is kind of creating a duplicate vector from a vector.

Program to assign a vector to another vector in C++.

#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int> vect{ 1, 2, 3 };

    vector<int> intvect(vect.begin(), vect.end());

    for (int x : intvect)
        cout << x << " ";


    vector<string> vectstr{ "Sachin", "Kohli", "Dhoni" };

    vector<string> strvect(vectstr.begin(), vectstr.end());

    cout << endl;

    for (string str : strvect)
        cout << str << " ";

    return 0;
}

Output

1 2 3
Sachin Kohli Dhoni