If std::list
is a linked list then why is there a limit on how many elements you can have? Each element is a link to a new node and there's no limit on how many pointers you can have.
Consider using std::list if: You need to store many items but the number is unknown. You need to insert or remove new elements from any position in the sequence. You do not need efficient access to random elements.
The std::list is implemented as a doubly-linked list.
The set::max_size() is a built-in function in C++ STL which returns the maximum number of elements a set container can hold. Syntax: set_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a set container can hold.
If
std::list
is a linked list then why is there a limit on how many elements you can have?
Because the max_size()
function is a requirement for all standard containers.
Each element is a link to a new node and there's no limit on how many pointers you can have.
Yes there is: the size must be representable by size_type
, so a limit is that type's maximum value. There's probably no reason for it to be less than that.
there's no limit on how many pointers you can have
There's a limit on how many distinct pointer values there can possibly be, based on the size of a pointer. For example, if a pointer occupies 64 bits in a particular implementation, then max_size()
could safely return 264-1. In fact it could be rather less than this, since each linked list node will be bigger than 1 byte.
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