Difference Between Size And Capacity Of Vector In C++ STL - C++ Programming Tutorial
C++ Course / STL Vector / Difference Between Size And Capacity Of Vector In C++ STL

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

BLUF: Mastering Difference Between Size And Capacity Of Vector In C++ STL is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Difference Between Size And Capacity Of Vector In C++ STL

C++ is renowned for its efficiency. Learn how Difference Between Size And Capacity Of Vector In C++ STL enables low-level control and high-performance computing in the tutorial below.

In this post, we will explore the variance between size and the capacity of vectors in C++. Prior to delving into their distinctions, it is essential to understand the functions size and 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 various significant distinctions between the size and capacity of a vector in C++. Some primary variances include:

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's consider an example to demonstrate the size and capacity of a 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 summary, the dimensions and capabilities of a std::vector are two separate yet interconnected ideas within the C++ STL that play a crucial role in effective memory handling and performance. Although the size changes with element insertions or deletions, the capacity represents the reserved memory for storing items, indicating the maximum number of elements supported without reallocating memory. Through methods such as resize, reserve, and shrinktofit, programmers can enhance memory utilization and speed, particularly when dealing with extensive or frequently modified data sets. The size and capacity collaborate to uphold the dynamic nature of vectors while upholding their operational efficiency.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience