Many methods within the template class vector
take a const reference to value_type
objects, for instance:
void push_back (const value_type& val);
while resize
takes its value_type
parameter by value:
void resize (size_type n, value_type val = value_type());
As a non-expert C++ programmer I can only think of disadvantages with this choice (for instance if size_of(value_type)
is big enough stack overflow may occur). What I would like to ask to people with more insight on the language is thus:
What is the design rationale behind this choice?
The C++ function std::vector::resize() changes the size of vector. If n is smaller than current size then extra elements are destroyed. If n is greater than current container size then new elements are inserted at the end of vector.
std::vector::resizeResizes the container to contain count elements. If the current size is greater than count , the container is reduced to its first count elements as if by repeatedly calling pop_back() . If the current size is less than count , additional elements are appended and initialized with copies of value .
vector::resize() The function alters the container's content in actual by inserting or deleting the elements from it. It happens so, If the given value of n is less than the size at present then extra elements are demolished.
Calling resize() with a smaller size has no effect on the capacity of a vector . It will not free memory.
void resize( size_type count, T value = T() );
This function has been removed from C++11.
C++11 has two overloads of resize()
:
void resize( size_type count );
void resize( size_type count, const value_type& value);
which is pretty much straightforward to understand. The first one uses default constructed objects of type value_type
to fill vector when resizing, the second takes a value from which it makes copies when resizing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With