How do vectors work in c++ – How does std::Vector works Internally ?

Vector:

How do vectors work in c++: Vectors are similar to dynamic arrays in that they can resize themselves when an element is added or removed, and the container takes care of their storage. Iterators can access and traverse vector elements since they’re stored in contiguous storage. Data is inserted at the top of vectors. Inserting at the end takes longer since the array may need to be extended at times. There is no resizing, and removing the final variable still takes the same amount of time. Inserting and erasing at the start or within the middle is linear in terms of your time .

Working of Vector in C++

std::vector example: std::vector allocates heap memory and stores all of its elements in a single contiguous memory location.

But what if the memory that was initially allocated is completely consumed?
Create a vector of ints, for example, using std::vector. Assume its initial capacity is 5 elements, but we want to store 10 elements in it for our application. So, what happens when we add the 6th element?

When std::vector’s internal memory is completely depleted, it expands its memory. To accomplish this, it follows the steps outlined below.

  • It will allocate a larger chunk of memory on the heap, nearly doubling the size of the previous allocation.
  •  It then copies all of the elements from the old memory location to the new one. Yes, it copies them; therefore, if our elements are user-defined objects, their copy constructor will be invoked. As a result, the speed of this step is quite slow.
  •  Following a successful copy, it deletes the old memory.

The capacity() member function can be used to determine the current capacity of a vector, or how many elements it can store in the currently allocated memory.
The size() member function can be used to determine the number of currently stored elements in a std::vector.

Below is the implementation:

#include <bits/stdc++.h>
using namespace std;
// taking a struct named VectorSample
struct vectorSample {
    vectorSample() {}
    vectorSample(const vectorSample& obj)
    {
        cout << "This is a vector sample constructor"
             << endl;
    }
};
int main()
{ // creating a vector of integers
    vector<vectorSample> intvector;
    // printing the capacity and size of the vector
    cout << "capacity of vector :: " << intvector.capacity()
         << endl;
    cout << "size of the vector:: " << intvector.size()
         << endl;
    // taking a integer variable which stores capacity
    int vec_capacity = intvector.capacity();
    // loop till vector capacity
    for (int i = 0; i < vec_capacity + 1; i++)
        intvector.push_back(vectorSample());
    // printing the capacity and size of the vector
    cout << "capacity of vector :: " << intvector.capacity()
         << endl;
    cout << "size of the vector:: " << intvector.size()
         << endl;

    for (int i = 0; i < vec_capacity + 1; i++)
        intvector.push_back(vectorSample());

    // printing the capacity and size of the vector
    cout << "capacity of vector :: " << intvector.capacity()
         << endl;
    cout << "size of the vector:: " << intvector.size()
         << endl;
    return 0;
}

Output:

capacity of vector :: 0
size of the vector:: 0
This is a vector sample constructor
capacity of vector :: 1
size of the vector:: 1
This is a vector sample constructor
This is a vector sample constructor
capacity of vector :: 2
size of the vector:: 2

Related Programs: