capacity and size
It is important to understand the difference between capacity and size. The size of a container is the number of elements it already holds; its capacity is how many elements it can hold before more space must be allocated.
The following code illustrates the interaction between size and capacity:
vector<int> ivec;// size should be zero; capacity is implementation definedcout << "ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;// give ivec 24 elementsfor (vector<int>::size_type ix = 0; ix != 24; ++ix) ivec.push_back(ix);// size should be 24; capacity will be >= 24 and is implementation definedcout << "ivec: size: " << ivec.size() << " capacity: " << ivec.capacity() << endl;
When run on our ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access