dimanche 30 janvier 2022

the return type of lower_bound and upper_bound if key value not found

I have a confusion about the returned value from the lower_bound and upp_bound member function from C++ STL containers. According to chapter 11.3.5 of "C++ Primer 5th Edition":

"if the element is not in the multimap, then lower_bound and upper_bound will rerturn equal iterators; both will refer to the point at which the key can be inserted without disrupting the order...If the element we're looking for has the largest key in the container, then upper_bound on that key returns the off-the-end iterator."

So if we have a multimap<int, int> with keys {1, 2, 3}, and we want to look for the element with key value 4, then the returned value of both lower_bound and upper_bound are iterators pointing to the position after 3, because 4 is larger than any existing key values in the multimap.

For the same reason, if we're looking for the element with the key value 0, then both the first element with the key value 1, because we can insert this element there in front of the one with the key value 1 "without disrupting the book" according to the book.

However, I found another file about the lower_bound:https://en.cppreference.com/w/cpp/algorithm/lower_bound, in which the returned value of it will point to the "last if no such element is found", same as for the upper_bound. From here, it seems like that no matter what the key value is, the returned iterator will always point to the last (off-the-end) in the container.

So my problem is, which one is correct, or did I make it wrong somewhere?

Aucun commentaire:

Enregistrer un commentaire