std::vector Quick Reference
`std::vector` is the default sequence container in C++: a contiguous, dynamically resizing array. This snippet shows the core operations (`push_back`, `emplace_back`, indexed access, range iteration) plus reservation patterns to avoid reallocation churn. Reach for it whenever you would reach for an `ArrayList` in Java or a list literal in Python.
1,026 views
5
#include <iostream>
#include <vector>
#include <string>
int main() {
std::vector<int> nums;
nums.push_back(1);
nums.push_back(2);
nums.push_back(3);
// Indexed access (no bounds check; .at(i) throws std::out_of_range).
std::cout << "first=" << nums[0] << " size=" << nums.size() << "\n";
// Range-for is the idiomatic loop.
for (int n : nums) std::cout << n << " ";
std::cout << "\n";
// Initialise from a brace list.
std::vector<std::string> words = {"alpha", "beta", "gamma"};
for (const auto& w : words) std::cout << w << "\n";
return 0;
}push_back appends one element; the vector's amortised cost is O(1) per insertion because capacity grows geometrically (typically doubling). nums[i] is unchecked: passing an out-of-range index is undefined behaviour, while nums.at(i) throws. Range-for (for (auto x : v)) calls begin()/end() under the hood and is the idiomatic loop in modern C++. Always take by const auto& for non-trivial element types so you avoid an unwanted copy each iteration.
#include <iostream>
#include <vector>
#include <utility>
struct Point { int x; int y; Point(int x, int y) : x(x), y(y) {} };
void demoEmplace() {
std::vector<Point> pts;
// push_back(T): constructs a temporary, then moves it in.
pts.push_back(Point{1, 2});
// emplace_back(args...): forwards args to T's ctor in-place. No temporary.
pts.emplace_back(3, 4);
pts.emplace_back(5, 6);
for (const auto& p : pts) {
std::cout << "(" << p.x << "," << p.y << ") ";
}
std::cout << "\n";
}
int main() {
demoEmplace();
return 0;
}emplace_back constructs the element directly inside the vector's storage from the forwarded arguments, skipping the temporary that push_back(T(args...)) would otherwise create. For a trivial type like Point, the difference is negligible because the compiler can elide the move; for types with non-trivial copy or move constructors (a std::string field, a unique_ptr member), it matters. The mental model: emplace_back is an in-place factory call; push_back is an append of an already-built object. Default to emplace_back for new code unless you specifically need a copy of an existing instance.
#include <iostream>
#include <vector>
void demoReserve() {
std::vector<int> v;
v.reserve(1000); // single allocation up front
for (int i = 0; i < 1000; i++) v.push_back(i);
std::cout << "final size=" << v.size() << " cap=" << v.capacity() << "\n";
}
int main() {
demoReserve();
return 0;
}Without reserve, the vector reallocates every time it doubles past its current capacity, copying or moving every element each time. reserve(n) allocates capacity for n elements up front when you know (or can estimate) the final size. The size stays at zero until you actually push elements; capacity() reports the allocation. This is one of the cheapest performance wins in C++: a tight loop building a known-size vector goes from O(n log n) total work to O(n) just by adding a reserve.
