What is the difference between size() and capacity() on a C++ std::vector?
Correct answer
C. size is elements stored; capacity is allocated slots
16 views
C. size is elements stored; capacity is allocated slots
16 views
size() reports how many elements are actually stored, while capacity() reports how many could be stored before the next reallocation. Growth strategies usually double or multiply capacity by 1.5, so capacity is often larger than size and reserve() can set it in advance. Accessing an index between size and capacity is still out of bounds, since those slots hold no constructed objects.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.