Difference Between Size And Capacity Of Vector In C++ STL

In this article, we will discuss the difference between size and the capacity of vectors in C++. Before discussing their differences, we must know about program size and the capacity of vectors in C++.

What is the Size in C++?

The term "size" describes how many elements are currently kept in a C++ STL std::vector. It may be accessed via the .size method and displays the actual number of vector entries. The size dynamically adjusts when things are added or removed. For example, when we call vec.push_back(10), the size is increased by 1. The size can't exceed the size of the vector. The usage of.resize modifies the vector's size, although it may initialize other items by default.

  • The size of a vector is the number of components that are now present in it.
  • It is possible to determine how many elements the vector has by using the.size method.
  • We can adjust the size dynamically by adding or removing things.
  • Example code:

    Example
    
    std::vector<int> vec = {1, 2, 3};
    std::cout << "Size: " << vec.size();  // Output: 3
    

    What is the Capacity of vector in C++?

The maximum number of elements that a std::vector can hold without needing further memory is known as its maximum capacity. It is usually larger than or equal to the size of the vector and is retrievable with the .capacity method. Memory is reallocated, typically by doubling the capacity, when a vector's size surpasses its existing capacity. Copying old items to the new memory address is part of this procedure, which might be costly. The .reserve function can be used to pre-allocate capacity to maximize efficiency when the amount of elements is known in advance. The size of the memory buffer allotted for the vector is indicated by capacity.

  • The capacity is the maximum number of elements that a vector can contain without requiring memory reallocation.
  • It is larger than the vector or equal to it.
  • When insertions surpass the current capacity of the vector, the capacity increases in chunks to minimize the overhead of frequent memory reallocations.
  • A vector's capacity can be found using the .capacity
  • Example:

    Example
    
    std::vector<int> vec = {1, 2, 3};
    std::cout << "Capacity: " << vec.capacity();  // Output: Implementation dependent, but >= 3
    

    Key differences between Size and Capacity of vector in C++:

There are several key differences between size and capacity of vector in C++. Some main differences are as follows:

Aspects Size Capacity
Defination The vector's current element count. The most elements that can be stored before they need to be reallocated.
Relation Never greater than or equal to the capacity. Never less than or equal to the size.
Adjustment Method The .resize() function can be used to explicitly modify it. Using the .reserve() function, it is possible to make specific adjustments.
Dynamic Behaviour It varies when items are added or removed. It alters automatically through .reserve() or during reallocation.
Memory usage It only displays the RAM that is being consumed by active components. It reflects the amount of memory allotted, regardless of usage.
Access methods The .size() function retrieves the current size. The .capacity() function retrieves the current capacity.

Example code:

Let us take an example to illustrate the size and capacity of vector in C++.

Example

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec;

    std::cout << "Initially: Size = " << vec.size() 
              << ", Capacity = " << vec.capacity() << "\n";

    vec.push_back(10);  // Add an element
    std::cout << "After push_back: Size = " << vec.size() 
              << ", Capacity = " << vec.capacity() << "\n";

    vec.reserve(20);  // Reserve capacity for 20 elements
    std::cout << "After reserve(20): Size = " << vec.size() 
              << ", Capacity = " << vec.capacity() << "\n";

    return 0;
}

Output:

Output

Initially: Size = 0, Capacity = 0
After push_back: Size = 1, Capacity = 1
After reserve(20): Size = 1, Capacity = 20

Explanation:

  • In this example, the size of a vector indicates how many elements it includes.
  • The capacity of a vector indicates the amount of storage it has set aside to store items.
  • Conclusion:

In conclusion, the size and capacity of a std::vector are two distinct but related concepts in C++ STL that are essential for efficient memory management and speed. While the size fluctuates in response to the addition or removal of elements, the capacity displays the memory allocated for item storage, which is the number of elements that can be supported without reallocation. Using functions like resize, reserve, and shrinktofit, developers can optimize memory usage and speed, especially when working with large or often changing datasets. The size and capacity cooperate to maintain the dynamic nature of vectors while maintaining their efficiency.

Input Required

This code uses input(). Please provide values below: