Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are there no capacity argument in the constructors for the containers?

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?

like image 895
hidayat Avatar asked Apr 26 '11 14:04

hidayat


People also ask

What is an STL container C++?

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.

What are the property shared by all STL containers?

They provide a limited set of container operations: member types value_type and size_type, container_type. basic constructors, destructors and assignment.

Which of the following STL containers has a feature to expand and contract in both directions front as well as back )?

std::deque (double-ended queue) is an indexed sequence container that allows fast insertion and deletion at both its beginning and its end.

What are the container in C Plus Plus?

In C++, there are generally 3 kinds of STL containers: Sequential Containers. Associative Containers. Unordered Associative Containers.


1 Answers

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.

like image 182
James McNellis Avatar answered Nov 15 '22 00:11

James McNellis