9.3.5. Resizing a Container
With the usual exception of arrays, we can use resize, described in Table 9.9, to make a container larger or smaller. If the current size is greater than the requested size, elements are deleted from the back of the container; if the current size is less than the new size, elements are added to the back of the container:
list<int> ilist(10, 42); // ten ints: each has value 42ilist.resize(15); // adds five elements of value 0 to the back of ilistilist.resize(25, -1); // adds ten elements of value -1 to the back of ilistilist.resize(5); // erases 20 elements from the back of ilist
Table 9.9. Sequential Container Size Operations
The resize operation takes an optional element-value argument that it uses to initialize ...
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