Initialize a vector in C++ in different ways

C++ STL | Initializing a vector: Here, we are going to learn about the various methods to initialize a vector in C++ with examples.
Submitted by Radib Kar, on July 07, 2020

Vector in C++ STL in one of the most convenient data structure which can be used as a dynamic array. There are several ways to define a vector in C++ and here we discuss all of them in detail.

1D vector in brief

A 1D vector is a linear data structure having exactly similar features like a 1D array. Vector has added advantage of dynamic features which helps it to grow at runtime, which means extending dynamically. Now to initialize a vector there have been several methods like below,

1) Define empty vector and then push_back() as required

This is the most naïve approach to initialize a vector. Firstly, we just define an empty vector. At that point, it has no idea about how many elements it’s going to have. Then by using push_back() function we can simply keep adding elements at the back as per requirement.

Example

Below is an example to add elements as per user wants.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //empty vector initialized
    vector<int> arr;

    cout << "Enter 0 stop appending element\n";
    cout << "Append any other number\n";
    int a;
    cin >> a;

    while (a) {
        arr.push_back(a);
        cout << "press 0 to stop appending\n";
        cout << "append any other number\n";
        cin >> a;
    }

    cout << "printing the vector...\n";
    //print the vector
    for (auto i : arr)
        cout << i << " ";
    cout << endl;

    return 0;
}

Output:

Enter 0 stop appending element
Append any other number 
4 
press 0 to stop appending 
append any other number 
2 
press 0 to stop appending 
append any other number 
-3
press 0 to stop appending 
append any other number 
6 
press 0 to stop appending 
append any other number 
0 
printing the vector...
4 2 -3 6 

2) Initialize the vector with user defined size

We can initialize the vector with user-defined size also. It's quite similar like creating a dynamic array using malloc() or new operator. This initializes the vector with element 0 by default.

Example

Below is the example:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
 
    cout << "Enter vector size:\n";
    cin >> n;
 
    //by default it initializes with 0
    vector<int> arr(n); 
 
    cout << "Printing the vector...\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;
 
    return 0;
}

Output:

Enter vector size:
5
Printing the vector...
0 0 0 0 0 

3) Initialize with user defined size and user defined element

Here instead of initializing with default 0, we initialize with user-defined value

The syntax is,

vector<int> arr(size_t, value);

Example

Creates a vector of size size_t all initialized with value.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n, a;
 
    cout << "Enter vector size:\n";
    cin >> n;
 
    cout << "Enter your element that you want to initialize\n";
    cin >> a;
 
    //by default it initializes with 0
    vector<int> arr(n, a); 
 
    cout << "Printing the vector...\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;
 
    return 0;
}

Output:

Enter vector size:
5
Enter your element that you want to initialize
2
Printing the vector...
2 2 2 2 2

4) Initialize with user defined elements

We can also initialize the vector with user-defined elements.

The syntax would be:

vector<int> arr{comma separated elements};

Example

The example is below:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //initialize with user-defined elements
    vector<int> arr{ 1, 2, 3, 4, 5, -1, -2, 6 };
 
    cout << "Printing the vector...\n";
    for (auto i : arr)
        cout << i << " ";
    cout << endl;
 
    return 0;
}

Output:

Printing the vector...
1 2 3 4 5 -1 -2 6

5) Initializing a vector with elements of other vector

We can also initialize a vector using elements of another vector. The vector is passed as constructor to initialize the new vector. This is a deep copy indeed.

Example

The example is like below:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //initialize with user-defined elements
    vector<int> arr1{ 1, 2, 3, 4, 5, -1, -2, 6 };
 
    cout << "Printing the vector arr1...\n";
    for (auto i : arr1)
        cout << i << " ";
    cout << endl;

    //initialize another vector with this vector
    vector<int> arr2(arr1);

    cout << "Printing the vector arr2...\n";
    for (auto i : arr2)
        cout << i << " ";
    cout << endl;

    return 0;
}

Output:

Printing the vector arr1...
1 2 3 4 5 -1 -2 6
Printing the vector arr2...
1 2 3 4 5 -1 -2 6

6) Initialize by an array

We can also initialize the vector using an existing array.

Example

The example is like below:

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //static array
    int arr[8] = { 1, 2, 3, 4, 5, -1, -2, 6 };

    //initialize with array elements

    vector<int> vect(arr, arr + 8);

    cout << "Printing the vector...\n";
    for (auto i : vect)
        cout << i << " ";
    cout << endl;

    return 0;
}

Output:

Printing the vector...
1 2 3 4 5 -1 -2 6

This is all the way you can initialize a vector as per your choice. Before finishing a few more things to note:

The first thing is we can assign elements to vector similarly as array (dynamically allocated), but to do that we cant initialize vector empty, need to initialize along with its size. For an empty vector, memory is not allocated for vector elements. Thus we need to initialize with user-defined size first.

Vector without defining the size

The below code gives segmentation fault whereas the second code works fine.

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //define vector
    vector<int> arr;
 
    //trying to assign like array
    arr[0] = 1;
 
    //segmentation fault if 
    //we try to assign like this
    return 0;
}

Output:

Segmentation fault (core dumped)

Vector with size

#include <bits/stdc++.h>
using namespace std;

int main()
{
    //define vector with size which allocates 
    //memory and initializes with 0
    vector<int> arr(2);
    
    //trying to assign like array
    arr[0] = 1;
    
    //works fine
    cout << "works fine\n";
    
    return 0;
}

Output:

works fine

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.