By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,409 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

stl map - the lowest and greatest key

P: n/a
Hello,
I have a map with keys that can be compared each other. I need a
method that returns the lowest and the greatest key from that map.

Now I'm using begin() and rbegin() which gives iterators to first and
last element of the map.

My question is:
Does begin() and rbegin() guarantee that returned iterators point on
the pair with lowest and gratest value?

Simple test (using gcc) shows it is true, but I've found comments it
is not.

Thanks for advice,
Tomasz Milewski

Feb 20 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* tomek milewski:
Hello,
I have a map with keys that can be compared each other. I need a
method that returns the lowest and the greatest key from that map.

Now I'm using begin() and rbegin() which gives iterators to first and
last element of the map.

My question is:
Does begin() and rbegin() guarantee that returned iterators point on
the pair with lowest and gratest value?
Read your own second paragraph.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Feb 20 '07 #2

P: n/a
On Feb 20, 11:00 am, "tomek milewski" <milewski.tom...@gmail.com>
wrote:
Hello,
I have a map with keys that can be compared each other. I need a
method that returns the lowest and the greatest key from that map.

Now I'm using begin() and rbegin() which gives iterators to first and
last element of the map.

My question is:
Does begin() and rbegin() guarantee that returned iterators point on
the pair with lowest and gratest value?
Unless you created your map with your own comparator as parameter then
std::less will be used, this means that the <-operator will be used to
compare objects. This means that the smallest (according to the
operator) element will be first and the largest last.

--
Erik Wikström

Feb 20 '07 #3

P: n/a
tomek milewski wrote:
Hello,
I have a map with keys that can be compared each other. I need a
method that returns the lowest and the greatest key from that map.

Now I'm using begin() and rbegin() which gives iterators to first and
last element of the map.

My question is:
Does begin() and rbegin() guarantee that returned iterators point on
the pair with lowest and gratest value?
Assuming the iterators are valid, yes.
Simple test (using gcc) shows it is true, but I've found comments it
is not.
What comments said otherwise?
Feb 20 '07 #4

P: n/a
"tomek milewski" <mi*************@gmail.comwrote in message
news:11**********************@v45g2000cwv.googlegr oups.com...
Hello,
I have a map with keys that can be compared each other. I need a
method that returns the lowest and the greatest key from that map.

Now I'm using begin() and rbegin() which gives iterators to first and
last element of the map.

My question is:
Does begin() and rbegin() guarantee that returned iterators point on
the pair with lowest and gratest value?
begin() and rbegin() are specified to give you the first and the last
element in the container. Whether these are the ones with the lowest and the
greatest value depends on your sorting predicate. The standard map uses the
less<operator and thus, all elements in the container will be sorted
ascending if you do not specify otherwise.
Simple test (using gcc) shows it is true, but I've found comments it
is not.
Are you sure that those comments did not refer to user specified sorting
like the following:

map<T, T, std::greater<T MyMap;

which would give you a map with descending order.

HTH
Chris
Feb 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.