R.3 — vector functions

📘 Appendix R⏱ about 1 min read✍️ Beginner friendly
Read one box at a time. No rush.

All need #include <vector>.

CodeWhat it does
std::vector<int> v { 1, 2, 3 };make a list
v.push_back(x)add to the end
v.pop_back()remove the last item
v.size()number of items
v.empty()true if no items
v[i] / v.at(i)item at index i
v.front() / v.back()first / last item
v.insert(v.begin() + i, x)insert at index i
v.erase(v.begin() + i)remove item at index i
v.clear()remove everything
v.resize(n)change the size to n
C++
for (const auto& item : v)    std::cout << item << ' ';
Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.