Skip to content
Justice Tech School

Vector In Depth

vector grows automatically. push_back, emplace_back, pop_back, insert, erase, size, capacity, reserve, front, back and at (with bounds checking).

Example
vector<int> v = {5, 2, 9};
v.push_back(1); v.insert(v.begin(), 0);
v.erase(v.begin() + 1);
cout << v.size() << " " << v.front() << " " << v.back() << "\n";
v.reserve(100);
cout << (v.capacity() >= 100) << "\n";
try { v.at(50); } catch (const out_of_range &) { cout << "out of range\n"; }

Result

4 0 1
1
out of range

Exercise

Checked element access.

v.(3)

Quiz Test

Which access checks bounds?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.