LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

How do you declare and use a vector in C++?

#include <vector>, declare vector<int> v;, add with v.push_back(x), and index with v[i] — it's a resizable array.

#include <vector>
using namespace std;

vector<int> v;       // empty vector of ints
v.push_back(10);     // append elements
v.push_back(20);
cout << v[0];        // 10
cout << v.size();    // 2

Common operations:

Operation Code
Create empty vector<int> v;
Create with size vector<int> v(10);
Create with values vector<int> v = {1, 2, 3};
Add element v.push_back(x);
Access element v[i] or v.at(i)
Size v.size()
Iterate for (int x : v) { ... }

Tip: vector grows automatically as you push_back, so it replaces raw dynamic arrays — no manual new[]/delete[]. Use v.at(i) when you want bounds-checking (it throws on out-of-range); v[i] does not.

Go deeper:

From Quiz: REVE1 / C++ Programming | Updated: Jul 14, 2026