DEV Community

Lalit Kumar
Lalit Kumar

Posted on

How to initialize a vector in C++

Vector is a standard library class template, which is similar to array, but also supports dynamic resizing. Vector provides a data structure with contiguous memory location. This enables efficient, direct access to any element of a vector via the subscript operator [ ], exactly as with a built-in array. Like class template arrays, template vector is most commonly used when the data in the container must be easily accessible via subscript or will be sorted, and when the number of elements may need to grow.

All of the above was a quick review over vectors, but the original topic of today’s article is how to initialize a vector in C++?


title: "originally posted here πŸ‘‡"

canonical_url: https://kodlogs.com/blog/519/how-to-initialize-a-vector-in-c

Methods to initialize a vector in C++

We can initialize vectors in many ways but in this article we will discuss only easy and familiar routes to initialize vectors. All the methods are given ahead.

  • Specifying size and initializing all values
  • Initializing like arrays
  • Initializing from array
  • Initializing from another vector

Specifying size and initializing all values

// CPP program to create an empty vector 
// and push values one by one. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
    int n = 3; 

    // Create a vector of size n with 
    // all values as 10. 
    vector<int> vect(n, 10); 

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

    return 0; 
}
Enter fullscreen mode Exit fullscreen mode

Initializing like arrays

// CPP program to initialize a vector like 
// an array. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
    vector<int> vect{ 10, 20, 30 }; 

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

    return 0; 
}
Enter fullscreen mode Exit fullscreen mode

Initializing from array

// CPP program to initialize a vector from 
// an array. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
    int arr[] = { 10, 20, 30 }; 
    int n = sizeof(arr) / sizeof(arr[0]); 

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

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

    return 0; 
}
Enter fullscreen mode Exit fullscreen mode

Initializing from another vector

As the name suggests we will be initializing vectors from another vector. And the reason I put this method in the last is because before following this method we must know how to initialize a vector by another method.

// CPP program to initialize a vector from 
// another vector. 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
    vector<int> vect1{ 10, 20, 30 }; 

    vector<int> vect2(vect1.begin(), vect1.end()); 

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

    return 0; 
} 
Enter fullscreen mode Exit fullscreen mode

If you have any doubt or queries related to this article feel free to ask in the comment section. I would like to answer you.

Top comments (0)