So when we need to traverse a container from start to end we write something like
for (i = v->begin(); i != v->end(); i++)
assuming i
is an iterator for container v
.
My question is "what guarantees that end will always point to one past the last element in container?" How does STL ensures this behavior and is there any chance that this case is not true?
The implementation of your standard library containers is determined by its authors, at the time they author it. And they don't decide how to implement it randomly of course. They decide how to implement it according to what they think is generally best based on their experience as software developers.
In C++, STL Unordered Associative Containers provide the unsorted versions of the associative container. Internally, unordered associative containers are implemented as hash table data structures.
In something like an std::vector the ::end() iterator will point to one past the last element.
Associative Containers The STL AssociativeContainer types are can be divided in two ways: containers which require unique keys, and those which allow multiple entries using the same key. The default comparison function for associative containers is std::less . This comparison function is used to sort keys.
STL ensures this behavior by always storing stuff like this:
In the end (pun), it doesn't matter what end()
is, as long as it's always end()
(and, obviously, can't be confused with any other node).
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