Why isn't max_size
a static member of std::string
?
This compiles but I think its strange that a property common to all strings can only be accessed via an instance of a string:
std::size_t max_size = std::string().max_size();
Why is it implemented like this?
A dynamically-resizable vector with fixed capacity and embedded storage (revision 2)
std::string offers a very different and much expanded interface compared to std::vector<> . While the latter is just a boring old sequence of elements, the former is actually designed to represent a string and therefore offers an assortment of string-related convenience functions.
max_size() is the theoretical maximum number of items that could be put in your vector. On a 32-bit system, you could in theory allocate 4Gb == 2^32 which is 2^32 char values, 2^30 int values or 2^29 double values.
std::vector (for T other than bool) meets the requirements of Container, AllocatorAwareContainer, SequenceContainer, ContiguousContainer (since C++17) and ReversibleContainer. Member functions of std::vector are constexpr : it is possible to create and use std::vector objects in the evaluation of a constant expression.
The vector::max_size() is a built-in function in C++ STL which returns the maximum number of elements that can be held by the vector container. Syntax: vector_name.max_size() Parameters: The function does not accept any parameters.
vector max_size() function in C++ STL. The vector::max_size() is a built-in function in C++ STL which returns the maximum number of elements that can be held by the vector container. Syntax: Parameters: The function does not accept any parameters. Return value: The function returns the maximum numbers that can fit into the vector container.
Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted.
Why isn't max_size a static member of std::string?
Because max_size return value depends on the allocator instance that the string instance uses internally.
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