|
vector::resizepublic member function
void resize ( size_type sz, T c = T() ); Change size Resizes the vector to contain sz elements.If sz is smaller than the current vector size, the content is reduced to its first sz elements, the rest being dropped. If sz is greater than the current vector size, the content is expanded by inserting at the end as many copies of c as needed to reach a size of sz elements. This may cause a reallocation. Notice that this function changes the actual content of the vector by inserting or erasing elements from the vector; It does not only change its storage capacity. To direct a change only in storage capacity, use vector::reserve instead. Parameters
Return ValuenoneIf a reallocation happens, it is performed using Allocator::allocate(), which may throw exceptions (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 myvector. It then uses resize first to trim the vector to a size of 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:
ComplexityLinear on the number of elements inserted/erased (constructions/destructions).See also
|