6 Different ways to Initialize vector in C++

C-plus-plus vector

In this article, we are going to learn 6 different ways to initialize vectors 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:

  • 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 vector.
  • By assigning a Vector to Vector.

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

1. Program to Assign Values in a Vector by 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. By using 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. By 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. By size and initializing 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 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. By assigning an array to 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. 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