Resizable Arrays with std::vector Terminology Reference
This reference provides an overview of container and std::vector terminology you'll encounter in this chapter. Use it as a quick lookup guide.
Container Basics
| Term |
Definition |
Example |
| Container |
Object that stores collection of other objects |
std::vector, std::array, std::string |
| Element |
Individual item stored in container |
Each value in vector |
| Sequence Container |
Container with elements in specific order |
std::vector, std::deque, std::list |
| Dynamic Array |
Array that can grow and shrink at runtime |
std::vector is dynamic array |
std::vector Fundamentals
| Term |
Definition |
Example |
| std::vector |
Resizable array container from standard library |
std::vector<int> numbers; |
| Template Type |
Type of elements stored in vector |
int in std::vector<int> |
| Empty Vector |
Vector with no elements |
std::vector<int> v; starts empty |
| Vector Size |
Number of elements currently in vector |
v.size() returns element count |
Vector Construction
| Term |
Definition |
Example |
| Default Constructor |
Creates empty vector |
std::vector<int> v; |
| List Constructor |
Creates vector from initializer list |
std::vector<int> v{1, 2, 3}; |
| Fill Constructor |
Creates vector with N copies of value |
std::vector<int> v(5, 10); five 10s |
| Copy Constructor |
Creates vector as copy of another |
std::vector<int> v2{v1}; |
Size and Index Types
| Term |
Definition |
Example |
| size_t |
Unsigned type for sizes and counts |
Return type of size() |
| size_type |
Container's unsigned size type |
std::vector<int>::size_type |
| Signed Index |
Using signed type for indices |
int or std::ptrdiff_t |
| Unsigned Wraparound |
Underflow of unsigned subtraction |
size_t(0) - 1 becomes very large |
Vector Access
| Term |
Definition |
Example |
| Subscript Operator |
operator[] for element access |
v[0] accesses first element |
| at() Member Function |
Bounds-checked element access |
v.at(0) throws if out of range |
| front() |
Access first element |
v.front() same as v[0] |
| back() |
Access last element |
v.back() same as v[v.size()-1] |
| Bounds Checking |
Verifying index is valid |
at() checks, [] does not |
Passing Vectors
| Term |
Definition |
Example |
| Pass by const Reference |
Efficient read-only parameter passing |
void func(const std::vector<int>& v) |
| Pass by Reference |
Passing for modification |
void func(std::vector<int>& v) |
| Pass by Value |
Copying entire vector (expensive) |
void func(std::vector<int> v) avoid this |
| Reference Parameter |
Parameter that avoids copying |
Preferred for vectors |
Returning Vectors
| Term |
Definition |
Example |
| Return by Value |
Returning vector from function |
std::vector<int> makeVec() |
| Move Semantics |
Efficient transfer of resources |
Compiler moves instead of copies |
| RVO |
Return Value Optimization |
Compiler eliminates copy/move |
| NRVO |
Named Return Value Optimization |
RVO with named local variable |
Loops and Iteration
| Term |
Definition |
Example |
| Index-Based Loop |
Loop using index variable |
for (int i{0}; i < v.size(); ++i) |
| Range-Based For Loop |
Loop iterating over all elements |
for (int x : v) |
| Iterator |
Object for traversing container |
v.begin() to v.end() |
| for-each Loop |
Another name for range-based for loop |
Simpler syntax for iteration |
Range-Based For Details
| Term |
Definition |
Example |
| Element Copy |
Range-for copies elements by default |
for (int x : v) x is copy |
| Reference Iteration |
Range-for with references |
for (int& x : v) x is reference |
| const Reference Iteration |
Read-only range-for |
for (const auto& x : v) |
| Range Expression |
Container being iterated |
v in for (auto x : v) |
Vector Capacity
| Term |
Definition |
Example |
| Size |
Number of elements in vector |
v.size() current element count |
| Capacity |
Memory allocated for elements |
v.capacity() may be larger than size |
| Reserve |
Pre-allocating memory |
v.reserve(100); allocates for 100 elements |
| Reallocation |
Allocating new memory when growing |
May invalidate pointers/references |
Vector Modification
| Term |
Definition |
Example |
| push_back() |
Add element to end |
v.push_back(5); |
| pop_back() |
Remove last element |
v.pop_back(); |
| resize() |
Change number of elements |
v.resize(10); |
| clear() |
Remove all elements |
v.clear(); size becomes 0 |
| emplace_back() |
Construct element in place |
v.emplace_back(args); |
Stack Operations
| Term |
Definition |
Example |
| Stack Behavior |
Last-in, first-out access pattern |
Using push_back/pop_back only |
| Push |
Adding element to top of stack |
v.push_back(x); |
| Pop |
Removing element from top |
v.pop_back(); |
| Top |
Accessing top element |
v.back(); |
Vector Efficiency
| Term |
Definition |
Example |
| Amortized Constant Time |
Operation is O(1) on average |
push_back() usually very fast |
| Geometric Growth |
Capacity increases by factor (e.g., 2x) |
Minimizes reallocations |
| Memory Contiguity |
Elements stored in continuous memory |
Fast access, cache-friendly |
| Random Access |
O(1) access to any element |
Direct indexing is very fast |
std::vector Specialization
| Term |
Definition |
Example |
| Vector Bool Specialization |
Special implementation for bool |
std::vector<bool> packs bits |
| Bit Packing |
Storing multiple bools per byte |
Space-efficient but different behavior |
| Proxy Reference |
Fake reference returned by operator[] |
Not a true bool& |
| Reference Proxy |
Object that acts like reference |
Used because can't reference individual bits |
Enumeration with Vectors
| Term |
Definition |
Example |
| Enum Index |
Using enum as array index |
v[Color::Red] |
| Underlying Value |
Integer value of enum |
Used as index into vector |
| Type-Safe Indexing |
Using enum instead of magic numbers |
More readable and maintainable |
| Enum Array Size |
Enum count used for array size |
Last enum value + 1 |