Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is end() implemented in STL containers?

Tags:

c++

stl

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?

like image 669
user283403 Avatar asked Sep 28 '10 06:09

user283403


People also ask

How are STL containers implemented?

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.

How are STL containers implemented in C++?

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.

Where does end iterator point to C++?

In something like an std::vector the ::end() iterator will point to one past the last element.

What two types of containers does the STL provide?

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.


1 Answers

STL ensures this behavior by always storing stuff like this:

vector

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).

like image 135
David Titarenco Avatar answered Sep 23 '22 17:09

David Titarenco