/ˈvɛktər/
noun … “Resizable sequential container.”
Vector is a dynamic, sequential container that stores elements in contiguous memory locations, providing indexed access similar to arrays but with automatic resizing. In many programming languages, such as C++ (via the std::vector class), vectors manage memory allocation internally, expanding capacity when elements are added and maintaining order. They combine the efficiency of arrays with flexible, dynamic memory usage on the heap.
Key characteristics of Vector include:
- Contiguous storage: elements are stored sequentially to enable constant-time indexed access.
- Dynamic resizing: automatically grows when capacity is exceeded, often doubling the allocated memory.
- Efficient insertion/removal: appending to the end is fast; inserting or deleting in the middle may require shifting elements.
- Memory management: internally handles allocation, deallocation, and sometimes wear leveling in embedded contexts.
- Integration with pointers: allows direct access to underlying memory for low-level operations.
Workflow example: Using a vector in C++:
std::vector<int> vec
vec.push_back(10)
vec.push_back(20)
vec.push_back(30)
for int i = 0..vec.size()-1:
printf("%d", vec[i])
Here, vec automatically resizes as elements are added, maintaining sequential order and enabling efficient iteration.
Conceptually, Vector is like a stretchable bookshelf: books (elements) are stored in order, and the shelf expands seamlessly as more books are added.
See Array, Heap, Pointer, Dynamic Array, Memory Management.