|
list::resizepublic member function
void resize ( size_type sz, T c = T() ); Change size Resizes the container to contain sz elements.If sz is smaller than the current container size, the content is reduced to its first sz elements, the rest being dropped. If sz is greater than the current container size, the content is expanded by inserting at the end as many copies of c as needed to reach a size of sz elements. Notice that this function changes the actual content of the container by inserting or erasing elements from it. Parameters
Return ValuenoneIn case of growth, the storage for the new elements is allocated using Allocator::allocate(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed). Example
The code sets a sequence of 9 numbers as an initial content for mylist. It then uses resize first to set the container size to 5, then to extend its size to 8 with values of 100 for its new elements, and finally it extends its size to 12 with their default values (for int elements this is zero). Output:
ComplexityIf the container grows, linear in the number number of elements inserted (constructor).If the container shrinks, linear on the number of elements erased (destructions)plus up to linear on the new size (iterator advance). See also
|