Skip to main content
C++ Output Prediction Medium +20 XP

Vector Size vs Capacity

After push_back, what is v.size()?

C++Read-only
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
cout << v.size();

What is the output?

Show answer and explanation

Answer: 3

size() returns the number of elements currently in the vector. push_back added 3 elements, so size() = 3.

Hint: size() counts actual elements, not capacity.

What this challenge tests

Output Prediction in C++: vector, stl.

Related challenges