DEV Community


C++ Vector Initialization how to initialize a vector in c++ ?

My name is Angular King. I'm a full-stack developer, entrepreneur and owner of Pakainfo. I live in India and I love to write
・2 min read

In initialize vector c++, A vector is a dynamic array class executed in the default C++ library. It vector ​class has the capacity to dynamically expand with reduce. The component are placed in a adjacent rage therefor that they can be traversed by iterators.

The very first step imp in using vectors is to know how to declare as well as initialize them; there are 5+ several ways of initializing a vector in C++ Examples:

initialize vector c++

Also You can learn std::vector in C++, What is std::vector?, Declaration of std::vector, Initialization of std::vector, Length of std::vector, Passing std::vector to function, Multidimensional std::vector, Passing a multidimensional std::vector to a function or many more Member Functions.

There are the following the Initialize a define vector in C++ as shown below:(5 several ways)
Initialization vector can be done in several ways with examples of vectors

  • Initializing by pushing values one by one
  • Specifying size and initializing all values
  • Initializing like arrays
  • Initializing from an array
  • Initializing from another vector

vector c++ & c++ vector initialization

Creating a vector object without any types of the initialization will make an empty vector with no any elements example like.
std::vector vecOfInts;

Initializing by pushing values one by one :

// c++ Example to make an empty vector
// with push values step by step.


using namespace std;

int main()
// Make an empty vector
vector myvct;


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

return 0; 
Enter fullscreen mode Exit fullscreen mode


You can visit my Article with More Examples : click here

Discussion (0)