C++ Programming 9. C++ Standard Template Library (STL) 8 mins read 15,404 views

std::vector in C++: Dynamic Arrays, Capacity, and Common Operations

std::vector is a dynamic contiguous array that grows automatically when elements are added. size() is the number of elements currently stored, whereas capacity(...
Published: Nov 1, 2026Last Updated: Nov 21, 2026Curriculum: Class 12 / AP Physics / JEE-NEETReviewed by: OperateTools Editorial Board
Quick Summary & Direct Answer (AEO / GEO Extraction)

What is std::vector in C++: Dynamic Arrays, Capacity, and Common Operations? std::vector is a dynamic contiguous array that grows automatically when elements are added. size() is the number of elements currently stored, whereas capacity(...

Audience & Scope: Students, educators, and candidates preparing for academic examinations and technical assessments in C++ Programming.
How does std::vector manage memory, and what is the difference between size and capacity?
std::vector is a dynamic contiguous array that grows automatically when elements are added. size() is the number of elements currently stored, whereas capacity() is the total memory allocated before a reallocation is triggered. When capacity is exceeded, vector allocates a larger chunk (typically 1.5x or 2x) and moves elements.

Explanation & In-Depth Details

1. Internal Structure (3 Pointers):
```text
std::vector memory management:
┌──────────────┬───────────────────────────────┬────────────────────────┐
│ [0] [1] [2] │ (Unused Allocated Storage) │ │
└──────────────┴───────────────────────────────┴────────────────────────┘
▲ ▲ ▲
begin() size() / end() capacity()
```

2. Key Operations:
- `push_back(v)` / `emplace_back(args...)`: Amortized $O(1)$ append.
- `reserve(n)`: Allocates capacity in advance without creating elements.
- `shrink_to_fit()`: Frees unused extra capacity.

Code Example

#include <iostream>
#include <vector>

int main() {
    std::vector<int> vec;
    vec.reserve(5); // Pre-allocate memory for 5 elements

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

    for (int i = 1; i <= 6; ++i) {
        vec.push_back(i * 10);
        std::cout << "Added " << i * 10 << " -> Size: " << vec.size() 
                  << ", Capacity: " << vec.capacity() << "\n";
    }

    std::cout << "Elements: ";
    for (int val : vec) std::cout << val << " ";
    std::cout << "\n";
    return 0;
}

Interview Question & Tips

Q: What is iterator invalidation in `std::vector`?
A: When a vector reallocates memory due to exceeding its capacity during push_back or insert, all existing pointers, references, and iterators to its elements become dangling and invalid.

Q: What is the benefit of `emplace_back` over `push_back`?
A: `emplace_back` constructs the object in-place directly in the vector's allocated storage using forwarded constructor arguments, avoiding temporary object construction and copy/move operations.

Best Practices

• Call `reserve()` whenever the approximate number of items is known to eliminate multiple reallocations.
• Use `emplace_back()` instead of `push_back()` for non-primitive objects.
• Use `.at(index)` when bounds-checking is required, or `operator[]` for maximum speed in verified loops.
Academic Standards & Verified Citations

This study reference for std::vector in C++: Dynamic Arrays, Capacity, and Common Operations is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation:

C++ Programming