Understanding Vector Capacity and Size in C++

Vector capacity refers to the maximum number of elements that can be stored with out allocating additional memory, while vector size indicates the actual number of elements currently contained. The capacity() member function returns the allocated storage space, and size() returns the current element count.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
    primes.reserve(20);
    std::cout << "Vector capacity: " << primes.capacity() << std::endl;
    std::cout << "Vector size: " << primes.size() << std::endl;
    return 0;
}

Output: Vector capacity: 20 Vector size: 15

A vector's size cannot exceed its capacity. When adding elements beyond current capacity, the vector reallocates memory and may invalidate references, pointers, and iterators.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
    std::cout << "Initial capacity: " << numbers.capacity() << std::endl;
    std::cout << "Initial size: " << numbers.size() << std::endl;
    std::cout << "Memory address: " << numbers.data() << std::endl;
    
    numbers.push_back(53);
    std::cout << "New capacity: " << numbers.capacity() << std::endl;
    std::cout << "New size: " << numbers.size() << std::endl;
    std::cout << "New memory address: " << numbers.data() << std::endl;
    return 0;
}

Output: Initial capacity: 15 Initial size: 15 Memory adress: 0x7ffee3a8a6a0 New capacity: 22 New size: 16 New memory address: 0x7ffee3a8a6c0

Modifying Capacity and Size

The reserve() function increases capacity without changing size, potentially relocating memory. The resize() function changes size and may increase capacity.

#include <iostream>
#include <vector>

int main() {
    std::vector<int> data = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
    std::cout << "Capacity: " << data.capacity() << std::endl;
    std::cout << "Size: " << data.size() << std::endl;
    std::cout << "Address: " << data.data() << std::endl;
    
    data.reserve(20);
    std::cout << "Address after reserve: " << data.data() << std::endl;
    std::cout << "Capacity after reserve: " << data.capacity() << std::endl;
    std::cout << "Size after reserve: " << data.size() << std::endl;
    
    data.resize(21);
    std::cout << "Address after resize: " << data.data() << std::endl;
    std::cout << "Capacity after resize: " << data.capacity() << std::endl;
    std::cout << "Size after resize: " << data.size() << std::endl;
    
    data.resize(20);
    std::cout << "Capacity after second resize: " << data.capacity() << std::endl;
    std::cout << "Size after second resize: " << data.size() << std::endl;
    return 0;
}

Reducing size with resize() removes excess elements but doesn't affect capacity.

Data Types for Capacity and Size

Vector capacity and size values use the vector<T>::size_type type:

std::vector<int>::size_type current_capacity = data.capacity();
std::vector<int>::size_type current_size = data.size();

The size_type is platform-dependent: unsigned int on 32-bit systems, unsigned long on 64-bit systems. The auto keyword can simplify declarations:

auto current_capacity = data.capacity();
auto current_size = data.size();

Tags: C++ STL vector Memory Management containers

Posted on Fri, 24 Jul 2026 16:49:51 +0000 by theinfamousmielie