Vector STL in C++

Vishnu C SVishnu C S
4 min read

Those who know how to write code in C++ know that C++ offers a wide range of libraries to make programs more efficient. One such library is STL(standard template Library).

A standard template Librabry is often a collection of template class and functions that offers common data structures and algorithm to make programming more efficient and convenient.

Then, what does it mean by a vector in front of STL? Just for now know this definition: a vector is a dynamic array that can grow or shrink in size making it a versatile data structure for storing and manipulating a sequence of elements.

Do not worry we will understand in-depth about vectors in this article just read ahead.

before getting into vector let's have a look at the array that we know

we can declare an array, and initialize it in the program. but it is a static array. Even if we initialize it in runtime it is still a static array. and guess what you just can't add or remove an element from an array without more lines of code. if there comes a necessity to add elements to the declared array it's not possible either we should redeclare the array or should should consider increasing the indices.

But if there is a dynamic array that supports the addition and deletion of elements without even bothering about the number of indices, it is better. And vector provides that.

let's see what the above lines trying to tell us:

#include<iostream>
using namespace std;
int main(){
int array[]={12,23,34,5,67,78,94}// a static array
int array1[4]={32,51,64,37}// static array
int n=4
for(int i=0;i<n;i++){
cin>>array1[n];//error as we cant store more elements than decleration
}
}

output:
error: array subscript is not an integer
cin>>array1[n];        ^

suppose we take input from users and try to initialize elements during runtime :

#include<iostream>
using namespace std;
int main(){
int number;
cin>>number;
int array[n];
for(int i=0;i<n;i++){
cin>>arra[n];
}
}
//we will get the same error in output as above program

here comes Vector. what we saw till now is a drawback of static arrays. let get dive into the dynamic vector it means with the help of some of its keywords we can add or remove elements from the array whenever we want.

#include<iostream>
#include<vector> // include this header file to create vector array
using namespace std;
int main(){
//creating a vector
vector<int>array; //this creates a vector called array
//adding elements to the array
array.push_back(1);
array.push_back(2);
array.push_back(3);
int size=array.size();//inbuilt function to calculate the length
  for(int i=0;i<size;i++){
    cout<<array[i]<<" ";
  }
return 0;
}

to understand it in a more precise way let's declare the vector and then try to push the value

#include<vector> // include this header file to create vector array
using namespace std;
int main(){
vector<int>array={12,23,45,56,49}; //this creates a vector called array
//adding elements to the array
array.push_back(23);
array.push_back(65);
int size=array.size();
  for(int i=0;i<size;i++){
    cout<<array[i]<<" ";
  }
return 0;
}

output:
12 23 45 56 49 23 65

observe that the array if not dynamic should have printed 12,23,45,56,49 but 23 and 65 are printed along with the declared array

how does this happen in the memory?

let us understand this when the vector is created you are creating an instance dynamic array. However, at the point of declaration, no memory is allocated for the elements. The vector is initialized in an empty state.

when we push the first element, a block of memory is created, and then when the second element is pushed, as there is no space, the compiler just doubles the memory that is created till now. so memory capacity is now increased to 2. Then when we push the 3rd element compiler again doubles the present memory to add the element. now memory size becomes 4 as we have only inserted 3 elements there is still one block left and now we push the next element.so again array is filled thus compiler doubles the size. now size becomes 4*2=8 blocks and we can push elements till memory is filled up i.e.,8 then if we push memory increases to 16.

just like pushing an element to the vector we can even remove/pop an element from the vector to do so, we use

 array.pop_back();

this is to remove the last element of the vector.

To copy a vector, knowing size and capacity:

int main(){
vector<int>array;
array.push_back(2)
array.push_back(24)
array.push_back(52)
array.push_back(98);
vector<int>array2(array);//array copied
int size=array2.size()
  for(int i=0;i<size;i++){
    cout<<array[i]<<" ";
  }
cout<<"capacity"<<arra2.capacity();
cout<<"size"<<array2.size();
return 0;
}

in vector capacity and size are two different terms. Size is the number of elements currently stored in the vector and capacity, is the number of empty blocks that the vector can hold without reallocation

to print the first and last element of a vector:

#include<iostream>
using namespace std;
int main(){
vector<int>array={23,34,45,56,67};
cout<<vector.front();//prints the first element of the vector
cout<<vector.back();//prints the last element of the vector
return 0;
}

printing the vector using forEach loop

#include<iostream>
#include<vector>
using namespace std;

int main(){
vector<int>array;
array.push_back(2)
array.push_back(24)
array.push_back(52)
array.push_back(98);
  for(auto it:array){
    cout<<it<<" ";
  }
return 0;
}

this is about some basics of vectors. There is still a lot there to learn about vectors but it would be too lengthy if I continued to write. keep learning.

0
Subscribe to my newsletter

Read articles from Vishnu C S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishnu C S
Vishnu C S

I am a student from Visvesvaraya Technological University