Level Up Your C++ Programming with Vectors: A Step-by-Step Tutorial

In this tutorial, I will cover all the essential things that are related to vectors and give you a clear understanding of vectors.

Vector is very helpful in solving array-related problems in data structures. After reading this article you will able to use vectors in programming and solving Data Structures problems.

Why do we use Vectors if we already have an Array Data structure?

  • In array, we have to declare the size of the array before compile time.

  • If we do not declare its size before compile time then our program will generate the error.

#include<bits/stdc++.h>
using namespace std;
int main()
{
//taking size of array
   int n;
   cout<<"enter the size of array:"<<endl;
   cin>>n;
//declare the array with the size
   int array[n];
}
  • After declaring we can't add extra elements to the array because the array contains only a number of elements equal to the size of the array i.e. n.

  • We can say the size of the array should be fixed and we can't resize it after initialized. That's why such type of array is called a Fixed size array.

  • For example: suppose we define an array of 5 size and after adding 5 elements in the array now we want to add 6th element in the array.

  •       #include<bits/stdc++.h>
          using namespace std;
          int main()
          {
          //declare the array with the 5
             int array[5]={1,2,3,4,5}; 
          //add 6th element in the array
             arry[6] = 7;
          //it will not include the 7 because 
          //array already filled and can't take extra elements
          }
    
  • But the array would not take the sixth element(7) because its size is 5 and the array already contains elements equal to its size.

  • We will implement a vector and our problem will be solved easily.

    Note: If you enjoyed this content, please consider giving it a thumbs-up or liking it to show your support!

Getting Started With Vector

  • Vector Container is a part of the Standard Template Library (STL).

  • In Simple terms, a vector container is nothing but an array with a dynamic nature.

  • It exactly works as an array data structure with extra fast functionality.

  • In vector, we don't need to define the array size. It has a resizing property and can automatically resize its size as per requirements. That's why we use a vector in place of the array because of its resizing property.

  • With vectors, we have multiple sub-functions which is very helpful for manipulations of elements.

  • functions like push_back(),pop_back(), size() ,capacity(),resize(),erase(),emplace_back(). We will cover all functions with the code.

    Syntax of vector

  • We define the vector with the vector keyword<data_type> vector_name;

  • We can define the vector of any datatype like int, double, long, string etc.

      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
      //define vector of integer type whose name is vect
        vector<int> vect;
      }
    

    Vectors Inbuit Functions

  • push_back(): This function helps to insert the element in the vector from the back side. The most widely used function to insert elements in containers.

  • pop_back(): This function helps to remove the element from the back side.

  • size(): This function helps to compute the size of the vector.

  • capacity(): This function helps to define the capacity of the vector or we can say it defines how many elements the vector can store.

  • resize(): This function helps to resize the vector. With the help of this function, we can optimize our space

  • erase(): This Function helps to remove the element from the particular index of a vector.

      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
      //vector of integer type with the vec name
        vector<int> vec;
        //now insert element in the vector with the help of vectorname.push_back()
        vec.push_back(1)
        vec.push_back(2)
        vec.push_back(3)
        vec.push_back(4)
       //now four element insert in vec successfully
       //now  for remove element from back side in vector is done 
      //with the help of pop_back()
        vec.pop_back()
        vec.pop_back()
         //4,3 are remove from the vec
        //for get the size of vec we use size()
        cout<<vec.size()<<endl;
        //for get the capacity of vec we use size()
        cout<<vec.capacity()<<endl;
    
      }
    
  • We can also declare the size of the vector if we want to. Let's understand with the simple index.

  • When we want to define the size we have to write the size of the vector under () braces immediately after the vector name.

      #include<bits/stdc++.h>
      using namespace std;
      int main()
      {
      //vector of integer type with the vec name with the size 4
        vector<int> vec(4);
        //now insert element in the vector with the help of push_back()
        vec.push_back(1)
        vec.push_back(2)
        vec.push_back(3)
        vec.push_back(4)
    
      //we have clearly see that there are only 4 elements in vec
      //and we want to add more element in existing vector but the size is 4
      //no problem since vector is dynamic in nature we can resize its size with
      //the help of resize function
      vec.resize(7)
      //now our vec is of 7 size and we can insert the elements
      vec.push_back(5)
      vec.push_back(6)
      vec.push_back(7)
    

In the image, we can clearly understand the resize property before resizing our vec is of 4 sizes as shown in the image. But after resizing our vector is of 7 size.

NOTE: We can also insert elements into the vector without the resize() function.

Time Complexity and Space Complexity

FunctionsTime ComplexitySpace Complexity
vector<datatype>O(N) if having more than half an element of its size N.o(N)
push_back()O(1)O(1)
pop_back()O(1)O(1)
erase()O(1)O(1)
resize()O(1)O(1)
size()O(1)O(1)

In the next tutorials, we will cover 2D vector, Linked List, Hashing and so on...