Is there a min function that accepts any number of arguments?

This is a discussion on Is there a min function that accepts any number of arguments? within the c++ forums in Programming Languages category; Hi, I'm wondering if there is a min function (in boost, maybe?) that accepts any number of arguments? std::min only accepts two arguments. If I want to get the minimum number out of many, I have use std::min many times, which is not convenient. Thanks, Peng...

Go Back   Application Development Forum > Programming Languages > c++

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-03-2008, 03:33 PM
Peng Yu
Guest
 
Default Is there a min function that accepts any number of arguments?

Hi,

I'm wondering if there is a min function (in boost, maybe?) that
accepts any number of arguments? std::min only accepts two arguments.
If I want to get the minimum number out of many, I have use std::min
many times, which is not convenient.

Thanks,
Peng
Reply With Quote
  #2  
Old 09-03-2008, 03:40 PM
Leandro Melo
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> wrote:
> Hi,
>
> I'm wondering if there is a min function (in boost, maybe?) that
> accepts any number of arguments? std::min only accepts two arguments.
> If I want to get the minimum number out of many, I have use std::min
> many times, which is not convenient.


Hi.

Maybe, you could iterate through the elements with a for (or something
similar) keeping track of the smallest element. This is pretty simple.


--
Leandro T. C. Melo
Reply With Quote
  #3  
Old 09-03-2008, 03:44 PM
Peng Yu
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote:
> On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> wrote:
>
> > Hi,

>
> > I'm wondering if there is a min function (in boost, maybe?) that
> > accepts any number of arguments? std::min only accepts two arguments.
> > If I want to get the minimum number out of many, I have use std::min
> > many times, which is not convenient.

>
> Hi.
>
> Maybe, you could iterate through the elements with a for (or something
> similar) keeping track of the smallest element. This is pretty simple.


Hi,

The numbers are at compile time not at runtime time. It has to some
how use the template to implement such a function.

Thanks,
Peng
Reply With Quote
  #4  
Old 09-03-2008, 03:45 PM
Victor Bazarov
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

Peng Yu wrote:
> I'm wondering if there is a min function (in boost, maybe?) that
> accepts any number of arguments? std::min only accepts two arguments.
> If I want to get the minimum number out of many, I have use std::min
> many times, which is not convenient.


No, as far as I know, there is no such function (since there is no way
to indicate to that function when to stop, or inside the function to
know what the number of arguments is). You can write your own wrappers
of 'min' with up to N arguments, can't you? Beyond that you're better
off with a loop anyway. Consider:

template<class It>
typename iterator_traits<It>::value_type min_of(It from, It to)
{
if (from == to) throw "empty range";
typename iterator_traits<It>::value_type v = *from++;
while (from != to) {
if (*from < v)
v = *from;
++from;
}
return v;
}
...
double temp_array[] = { v1, v2, v3, ... , vN };
size_t temp_array_size = sizeof(temp_array) / sizeof(*temp_array);
double mymin = min_of(temp_array, temp_array + temp_array_size);

(I didn't check the code, provided for illustration only).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Reply With Quote
  #5  
Old 09-03-2008, 03:49 PM
Victor Bazarov
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

Peng Yu wrote:
> On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote:
>> On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> wrote:
>>
>>> Hi,
>>> I'm wondering if there is a min function (in boost, maybe?) that
>>> accepts any number of arguments? std::min only accepts two arguments.
>>> If I want to get the minimum number out of many, I have use std::min
>>> many times, which is not convenient.

>> Hi.
>>
>> Maybe, you could iterate through the elements with a for (or something
>> similar) keeping track of the smallest element. This is pretty simple.

>
> Hi,
>
> The numbers are at compile time not at runtime time. It has to some
> how use the template to implement such a function.


How many numbers are we talking about? You can roll your own 'min_of'
implementation using recursive template definitions in no time, can't you?

template<class T> T min_of(T t1, T t2) {
return std::min(t1, t2);
}
template<class T> T
min_of(T t1, T t2, T t3) {
return std::min(min_of(t1, t2), t3);
}
template<class T> T
min_of(T t1, T t2, T t3, T t4) {
return std::min(min_of(t1, t2, t3), t4);
}
template<class T> T
min_of(T t1, T t2, T t3, T t4, T t5) {
return std::min(min_of(t1, t2, t3, t4), t5);
}
.... and so on

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Reply With Quote
  #6  
Old 09-03-2008, 03:56 PM
Peng Yu
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
> Peng Yu wrote:
> > On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote:
> >> On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> wrote:

>
> >>> Hi,
> >>> I'm wondering if there is a min function (in boost, maybe?) that
> >>> accepts any number of arguments? std::min only accepts two arguments.
> >>> If I want to get the minimum number out of many, I have use std::min
> >>> many times, which is not convenient.
> >> Hi.

>
> >> Maybe, you could iterate through the elements with a for (or something
> >> similar) keeping track of the smallest element. This is pretty simple.

>
> > Hi,

>
> > The numbers are at compile time not at runtime time. It has to some
> > how use the template to implement such a function.

>
> How many numbers are we talking about? You can roll your own 'min_of'
> implementation using recursive template definitions in no time, can't you?
>
> template<class T> T min_of(T t1, T t2) {
> return std::min(t1, t2);}
>
> template<class T> T
> min_of(T t1, T t2, T t3) {
> return std::min(min_of(t1, t2), t3);}
>
> template<class T> T
> min_of(T t1, T t2, T t3, T t4) {
> return std::min(min_of(t1, t2, t3), t4);}
>
> template<class T> T
> min_of(T t1, T t2, T t3, T t4, T t5) {
> return std::min(min_of(t1, t2, t3, t4), t5);}
>
> ... and so on


Hi Victor,

I can define my own version of min_of. But I feel that the min
function that accepts any number of arguments is a reasonable
extension to std::min, and boost in many ways extends the original C++
library. I think that it is worthwhile to add such function in boost
if it is not there. At this point, since the issue is with boost,
maybe I should post the message at boost mailing list.

Thanks,
Peng

Reply With Quote
  #7  
Old 09-03-2008, 04:03 PM
Victor Bazarov
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

Peng Yu wrote:
> On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>> Peng Yu wrote:
>>> On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote:
>>>> On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> wrote:
>>>>> Hi,
>>>>> I'm wondering if there is a min function (in boost, maybe?) that
>>>>> accepts any number of arguments? std::min only accepts two arguments.
>>>>> If I want to get the minimum number out of many, I have use std::min
>>>>> many times, which is not convenient.
>>>> Hi.
>>>> Maybe, you could iterate through the elements with a for (or something
>>>> similar) keeping track of the smallest element. This is pretty simple.
>>> Hi,
>>> The numbers are at compile time not at runtime time. It has to some
>>> how use the template to implement such a function.

>> How many numbers are we talking about? You can roll your own 'min_of'
>> implementation using recursive template definitions in no time, can't you?
>>
>> template<class T> T min_of(T t1, T t2) {
>> return std::min(t1, t2);}
>>
>> template<class T> T
>> min_of(T t1, T t2, T t3) {
>> return std::min(min_of(t1, t2), t3);}
>>
>> template<class T> T
>> min_of(T t1, T t2, T t3, T t4) {
>> return std::min(min_of(t1, t2, t3), t4);}
>>
>> template<class T> T
>> min_of(T t1, T t2, T t3, T t4, T t5) {
>> return std::min(min_of(t1, t2, t3, t4), t5);}
>>
>> ... and so on

>
> Hi Victor,
>
> I can define my own version of min_of. But I feel that the min
> function that accepts any number of arguments is a reasonable
> extension to std::min, and boost in many ways extends the original C++
> library. I think that it is worthwhile to add such function in boost
> if it is not there. At this point, since the issue is with boost,
> maybe I should post the message at boost mailing list.


I don't believe it to be an issue *with* Boost. It's something _you_
need (or, rather, *want*). It's something "nice to have", not a "must
have". Post to Boost forum, but all means. Just don't mention that it
is "an issue". Just friendly advice...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Reply With Quote
  #8  
Old 09-03-2008, 04:04 PM
Juha Nieminen
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

Peng Yu wrote:
> I'm wondering if there is a min function (in boost, maybe?) that
> accepts any number of arguments? std::min only accepts two arguments.
> If I want to get the minimum number out of many, I have use std::min
> many times, which is not convenient.


With the current C++ I think it's rather difficult to implement such a
function which takes a variable number of arguments (while probably
possible, it won't be type-safe, and you would probably have to specify
explicitly the number of variables as a parameter, making it more
cumbersome).

However, AFAIK, with the upcoming standard C++ it will be possible to
create such a function in a completely type-safe way (and without having
to specify the number of arguments explicitly), by using so-called
variadic templates. However, this is not yet standardized.
Reply With Quote
  #9  
Old 09-03-2008, 04:19 PM
Leandro Melo
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On 3 set, 17:04, Juha Nieminen <nos...@thanks.invalid> wrote:
> Peng Yu wrote:
> > I'm wondering if there is a min function (in boost, maybe?) that
> > accepts any number of arguments? std::min only accepts two arguments.
> > If I want to get the minimum number out of many, I have use std::min
> > many times, which is not convenient.

>
> * With the current C++ I think it's rather difficult to implement such a
> function which takes a variable number of arguments (while probably
> possible, it won't be type-safe, and you would probably have to specify
> explicitly the number of variables as a parameter, making it more
> cumbersome).
>
> * However, AFAIK, with the upcoming standard C++ it will be possible to
> create such a function in a completely type-safe way (and without having
> to specify the number of arguments explicitly), by using so-called
> variadic templates. However, this is not yet standardized.



GCC has some implementations already. (Using flag -std=c++0x.)

--
Leandro T. C. Melo.
Reply With Quote
  #10  
Old 09-03-2008, 08:21 PM
Daniel T.
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

Peng Yu <PengYu.UT@gmail.com> wrote:

> I'm wondering if there is a min function (in boost, maybe?) that
> accepts any number of arguments? std::min only accepts two arguments.
> If I want to get the minimum number out of many, I have use std::min
> many times, which is not convenient.


Yes there is.

void fn( const vector<int>& vec )
{
if ( !vec.empty() ) {
int m = *min_element( vec.begin(), vec.end() );
// m now equals the minimum value of the vector
// so use it
cout << "minimum value == " << m << '\n';
}
}
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:30 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.