How to use Vector capacity function in C++

In this post, we will learn how to use the vector library and its vector capacity function in C++.

Vector library is just like a regular library except for the part where these houses dynamic data types like vectors, maps, etc. This is a part where we can resize arrays like dynamic arrays and make it far usable to work it. So let us start understanding how this thing works.

So to work with any of the vector functions, we will need to use the <vector> header file which is :

#include<vector>

Vectors are the same as dynamic arrays only that it can be handled or resizes upon will and also the computer resizes it automatically. Vector elements are similar to arrays as they are placed in contiguous locations so we can access them using iterators. Similar to arrays, in vectors, data is inserted not at the beginning but at the end.

In order to work with vectors we will have to know two functions first:

  • begin() – Returns an iterator pointing to the first element in the vector
  • end() – Returns an iterator pointing to the theoretical element that follows the last element in the vector
  • push_back() – Inserts an element to the end of the list.

Also read: push_back() and pop_back() function in C++ STL

Here “iterator” is a type of pointer that is inbuilt in the functions and they return the desired position to the user. It is inbuilt in the vector functions so it is required that we use these functions.

And another important thing will be to know the difference between the size and capacity of a vector. Size is literally what it spells. It returns the size of the vector or the number of elements present in it. While capacity is the size of the allocated storage in terms of elements present.

In order to make it work, we will have to first use the vector so let us see how we are going to do it.

Algorithm to use “capacity” function in <vector> class in C++

  1. declare vector<int> vect.
  2. start for loop
  3. insert items by vect.push_back.
  4. end for loop (How to Terminate a Loop in C++?)
  5. Get size by vect.size
  6. Get capacity by vect.capacity
  7. Print size and capacity
  8. End

Program to demonstrate the use of  “capacity” function in <vector> class in C++

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

int main() 
{
    vector<int> vect;                   // declaring a vector here which takes input as integers
    int i;
    
    for (i = 0; i < 5; i++)
       vect.push_back('y');              //this function is used to insert elements at the back of the vector(the char acter gets stored as integers here)
    cout << "Number of elements in vector = " << vect.size() << endl;           //no. of elements present in the vector
    cout << "Capacity of vector           = " << vect.capacity() << endl;        //capacity it holds in the memory
    cout<<endl<<"--------------------------------------"<<endl<<endl;
    
    for (i = 0; i < 15; i++)
       vect.push_back('y');              //this function is used to insert elements at the back of the vector
    cout << "Number of elements in vector = " << vect.size() << endl;
    cout << "Capacity of vector           = " << vect.capacity() << endl;
    
    

    return 0;
}

The output of the above program:

How to use Vector capacity function in C++

Leave a Reply

Your email address will not be published. Required fields are marked *