Back to questionsWhat do the vectors
JuniorTheoryCommon
What do the vectors a and b hold after reserve(5) and resize(5)?
#include <vector>
int main() {
std::vector<int> a;
a.reserve(5);
std::vector<int> b;
b.resize(5);
}a stays empty but has room for at least 5 elements; b holds five zeros
- Expecting
reserveto create elements: it only allocates memory, soa.size()stays0, and evena[0]is out of range. - Expecting
resizeonly to allocate memory: it setsb.size()to5and adds elements with the value0, the same valueint x{}gets.
- Why call
reservebefore a loop that adds a known number of elements withpush_back? - What does a later
b.resize(2)do tob?