If I want to set the capacity to a std::vector
I have to call .reserve(...)
, are there any reason why there is not a capacity argument in the constructor for the containers in stl, std::string
, std::vector
?
This article focuses on the C++ STL container set. STL containers are objects that can store multiple elements, manage any storage required for the elements, and offer member functions we can use to access them. A container may allow elements of either the same type or different types to be stored in it.
They provide a limited set of container operations: member types value_type and size_type, container_type. basic constructors, destructors and assignment.
std::deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end.
In C++, there are generally 3 kinds of STL containers: Sequential Containers. Associative Containers. Unordered Associative Containers.
There is one obvious reason: what would such a constructor look like?
All of the sequence containers already have a constructor that can be called with a single integer argument. That constructor resizes the container to have the specified number of elements.
Yes, you could add a second parameter (like bool reserve_instead_of_resize
) to be able to use this constructor for both initial resizes and initial reserves, but then I think the end result would be confusing.
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