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

Similar Threads

Java matrix problem 3
min-max template 3
min max from tuples in list 23
Help for a newbie 13
std::min/max vs own functions 10
Why no min / max 14
std::min and 32/64 bits. 21
Min and max of a POD 14

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top