Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is std::vector::max_size() non-static? [duplicate]

Tags:

c++

std

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?

like image 738
Baz Avatar asked Nov 28 '12 10:11

Baz


People also ask

What is a static vector?

A dynamically-resizable vector with fixed capacity and embedded storage (revision 2)

What is the difference between std :: string and std :: vector?

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.

What is the maximum size of a vector in C++?

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.

What is std vector in C++?

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.

What is max size of a vector in C++?

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.

What is max_size function in C++ STL?

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.

What is the difference between a static array and a vector?

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.


1 Answers

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.

like image 117
utnapistim Avatar answered Oct 06 '22 18:10

utnapistim