finding max and min integers - newbie question

K

Kamil Burzynski

[SNIP]
int FindMin( vector<int>::iterator Begin, vector<int>::iterator End ) {
return (Begin != End) ? min( *Begin, FindMin( Begin + 1, End) ) :
*(Begin-1);
}

However, there might be more efficient solutions :)

And more stable too ;)

That *(Begin-1) seems to be little dangerous, especially when there is
no data (Begin == End) ;)
 
C

Chris Theis

Kamil Burzynski said:
[SNIP]
int FindMin( vector<int>::iterator Begin, vector<int>::iterator End ) {
return (Begin != End) ? min( *Begin, FindMin( Begin + 1, End) ) :
*(Begin-1);
}

However, there might be more efficient solutions :)

And more stable too ;)

That *(Begin-1) seems to be little dangerous, especially when there is
no data (Begin == End) ;)

This is certainly true. IMHO I wouldn't use the recursive solution anyway
'cause there is simply no advantage over using min_element() or
max_element(). Christoph's solution however is certainly faster.

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
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top