472,146 Members | 1,303 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

stl map - the lowest and greatest key

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
4 2799
* 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
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
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
"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.

Similar topics

8 posts views Thread by Mike Nolan | last post: by
namcintosh
54 posts views Thread by namcintosh | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.