Growable Arrays with std::vector in C++
std::vector<T> provides a growable array with automatic storage management. Include <vector> to use it; std::vector<int> values{10, 20, 30}; creates three integer elements.
Summary
std::vector<T> provides a growable array with automatic storage management. Include <vector> to use it; std::vector<int> values{10, 20, 30}; creates three integer elements. Ordinary vector specializations store elements contiguously, supporting cache-friendly traversal and indexed access in time.
| Member | Meaning or effect |
|---|---|
| size() | Number of existing elements |
| capacity() | Number of elements storage can hold before reallocation |
| reserve(count) | Requests capacity for at least count elements without changing size |
| resize(count) | Changes size by constructing or destroying elements |
push_back(value) takes amortized time, but an append requiring reallocation takes time for existing elements. Insertion or erasure at an arbitrary position takes time in the worst case because subsequent elements must shift. Reserving an expected element count can reduce repeated allocations.
🧠 Key Concepts
- Size versus capacity
- Automatic storage management
- Amortized append complexity
- Contiguous storage
- Bounds checking
- Capacity reservation
- Iterator invalidation
- Resizing and clearing
🧠 Quick Check
See what you remember from the summary.
A vector contains three elements. After reserve(100) completes successfully, what is guaranteed?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Growable Arrays with std::vector in C++
📘 Overview std::vector<T> manages a growable array and automatically releases its storage when destroyed. Its size is the number of elements currently stored, while its capacity is the number it can hold before reallocating.
🧠 Key Idea A vector grows by acquiring larger storage when its capacity is exhausted. This makes appending efficient on average, but reallocation invalidates existing pointers, references, and iterators to its elements.
⚔️ Core Details: - Declaration: include <vector> and write std::vector<int> values{10, 20, 30}; to create a vector containing three integers. - Appending with push_back(value) takes amortized time; an append requiring reallocation takes time for
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.