stl map - the lowest and greatest key

T

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?

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

Thanks for advice,
Tomasz Milewski
 
A

Alf P. Steinbach

* 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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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

Mark P

tomek said:
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?
 
C

Chris Theis

tomek milewski said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,044
Latest member
RonaldNen

Latest Threads

Top