[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

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

Peng Yu

9/3/2008 7:34:00 PM

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
17 Answers

Leandro Melo

9/3/2008 7:40:00 PM

0

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

Peng Yu

9/3/2008 7:45:00 PM

0

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

Victor Bazarov

9/3/2008 7:45:00 PM

0

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

Victor Bazarov

9/3/2008 7:50:00 PM

0

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

Peng Yu

9/3/2008 7:57:00 PM

0

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

Victor Bazarov

9/3/2008 8:03:00 PM

0

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

Juha Nieminen

9/3/2008 8:04:00 PM

0

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.

Leandro Melo

9/3/2008 8:19:00 PM

0

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.

Daniel T.

9/4/2008 12:21:00 AM

0

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';
}
}

Peng Yu

9/4/2008 1:22:00 AM

0

On Sep 3, 7:21 pm, "Daniel T." <danie...@earthlink.net> wrote:
> Peng Yu <PengYu...@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';
> }
>
> }

Hi,

It seems that many people misunderstood my original post. What I'm
looking for is of the syntax,

min(x1, x2, x3, x4, ..., x_n); // where n can be any number.

Thanks,
Peng