The STL reference seems to make a conceptual difference between :
Also, it seems like we have :
begin()
method returning an iterator pointing to the first element in the container.front()
method returning a reference to the first element in the container.My understanding is that the front()
method could easily be defined in terms of the begin()
method by just dereferencing its return value.
Thus, my question is : why isn't the front()
method defined for all objects defining the begin()
method ? (which should be every container really)
(I guess that from a semantic point of view, it doesn't make as much sense to get the first element from a map as it does for the first element from a vector but I was wondering if there was a more valid explanation).
The map and the multimap are both containers that manage key/value pairs as single components. The essential difference between the two is that in a map the keys must be unique, while a multimap permits duplicate keys.
In computing, associative containers refer to a group of class templates in the standard library of the C++ programming language that implement ordered associative arrays. Being templates, they can be used to store arbitrary elements, such as integers or custom classes.
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have the same key values.
You really have to ask the standards committee on that one (comp.lang.c++.std) but my guess is that yeah, it just doesn't make as much sense. Further there's not as much clarity as to what it would mean. Do you want the root, the pre-order first, post-order first, first you inserted...? With sequences it's quite clear: front is one side, back the other. Maps are trees.
Front() implies an ordering; "the first in the row".
Begin() implies lets start somewhere, no matter where.
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