What is a Vector and How to initialize a Vector

 Vector and its Initialization

Vector is a Dynamic Array that changes its size on run time, and provides flexibility to use it without initializing the size, but needs to declare the data type like int, string, etc 

basic initialization of vector class format

vector<data type> vector name ;

Ex: vector <int> v1;

here in the above example, the vector is initialized with data type int and the name of the vector is v1 

let us take another example to initialize the vector 

Ex: vector<data type>vector name (initial size, every indexing address value);

vector <bool>v2(3,0);

here the vector gets initialized from 3 instead of initialized from 0 and here the vector is bool type and all 3 means if  arr1 is an array then 

vector <bool>v2(3,0);

bool arr[3]={0,0,0}

both have the same value but only v2 can be mutable and have a size of 4 in this situation because vectors always have the size in terms of 2**n where n is an integer likewise if the vector v2 gets filled to the 4th index new automatically when we need more index it doubles its size and makes size to store 8 bool value and on 9th indexing value the size gets changed to 16 and so on


Comments

Pageviews