std::vector is the default, go-to sequence container in modern C++. It encapsulates a dynamic array that automatically resizes itself on the heap when elements are added.
#include <iostream>
#include <vector>
int main() {
std::vector<int> vec;
// PERFORMANCE TIP: Reserve space to avoid reallocations!
vec.reserve(100);
vec.push_back(10);
vec.push_back(20);
vec.emplace_back(30); // In-place construct
std::cout << "Size: " << vec.size() << "\n"; // 3 elements
std::cout << "Capacity: " << vec.capacity() << "\n"; // 100 slots reserved!
return 0;
}
Boost your C++ proficiency by studying these closely connected core modules:
Understanding how vector fits into STL sequence containers.
Traversing vectors using iterators.
How vector moves elements during growth.