By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,750 Members | 1,471 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,750 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Sep 3 '08 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On 3 set, 16:33, Peng Yu <PengYu...@gmail.comwrote:
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
Sep 3 '08 #2

P: n/a
On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.comwrote:
On 3 set, 16:33, Peng Yu <PengYu...@gmail.comwrote:
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
Sep 3 '08 #3

P: n/a
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
Sep 3 '08 #4

P: n/a
Peng Yu wrote:
On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.comwrote:
>On 3 set, 16:33, Peng Yu <PengYu...@gmail.comwrote:
>>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 TT min_of(T t1, T t2) {
return std::min(t1, t2);
}
template<class TT
min_of(T t1, T t2, T t3) {
return std::min(min_of(t1, t2), t3);
}
template<class TT
min_of(T t1, T t2, T t3, T t4) {
return std::min(min_of(t1, t2, t3), t4);
}
template<class TT
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
Sep 3 '08 #5

P: n/a
On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Peng Yu wrote:
On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.comwrote:
On 3 set, 16:33, Peng Yu <PengYu...@gmail.comwrote:
>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 TT min_of(T t1, T t2) {
return std::min(t1, t2);}

template<class TT
min_of(T t1, T t2, T t3) {
return std::min(min_of(t1, t2), t3);}

template<class TT
min_of(T t1, T t2, T t3, T t4) {
return std::min(min_of(t1, t2, t3), t4);}

template<class TT
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

Sep 3 '08 #6

P: n/a
Peng Yu wrote:
On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
>Peng Yu wrote:
>>On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.comwrote:
On 3 set, 16:33, Peng Yu <PengYu...@gmail.comwrote:
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 TT min_of(T t1, T t2) {
return std::min(t1, t2);}

template<class TT
min_of(T t1, T t2, T t3) {
return std::min(min_of(t1, t2), t3);}

template<class TT
min_of(T t1, T t2, T t3, T t4) {
return std::min(min_of(t1, t2, t3), t4);}

template<class TT
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
Sep 3 '08 #7

P: n/a
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.
Sep 3 '08 #8

P: n/a
On 3 set, 17:04, Juha Nieminen <nos...@thanks.invalidwrote:
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.
Sep 3 '08 #9

P: n/a
Peng Yu <Pe*******@gmail.comwrote:
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';
}
}
Sep 4 '08 #10

P: n/a
On Sep 3, 7:21 pm, "Daniel T." <danie...@earthlink.netwrote:
Peng Yu <PengYu...@gmail.comwrote:
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
Sep 4 '08 #11

P: n/a
On 3 Sep, 20:33, Peng Yu <PengYu...@gmail.comwrote:
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.
put them in a container and use min_element()?
You really want to use Lisp don't you? :-)

--
Nick Keighley
Sep 4 '08 #12

P: n/a
Peng Yu wrote:
It seems that many people misunderstood my original post.
I don't think they misunderstood. They are just saying "there's no
easy way of doing that with the current C++, but here is an alternative
approach". For some reason they want to leave out the first part rather
then say it explicitly.
Sep 4 '08 #13

P: n/a
On Sep 3, 9:22*pm, Peng Yu <PengYu...@gmail.comwrote:
On Sep 3, 7:21 pm, "Daniel T." <danie...@earthlink.netwrote:


Peng Yu <PengYu...@gmail.comwrote:
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.
You have an arbitrary number of variables that are all related, but
you don't have them in an array? That's silly.

Put your variables in a vector and call min_element.
Sep 4 '08 #14

P: n/a
Peng Yu 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.
Well, if you really want it:
----------------
namespace peng_yu
{
template<typename T>
struct tmp_min_t
{
const T &item;
tmp_min_t(const T &n) : item(n) {}
operator const T& () const { return item; }
};

struct minimum_t {} minimum;

template<typename T>
inline tmp_min_t<Toperator , (minimum_t, const T &x)
{
return tmp_min_t<T>(x);
}
template<class T>
inline tmp_min_t<Toperator , (tmp_min_t<Ta, const T &x)
{
if (a.item<=x) return a;
else return tmp_min_t<T>(x);
}
}

#define MIN(...) (peng_yu::minimum,__VA_ARGS__)

#include <iostream>

int main()
{
std::cout
<< MIN(6,5,4,5,1,5,3,5,7,4,3,4,6,8,5,3,2,-23,5,2,2,4,6,4,4,3,4)
<< std::endl;
}
--------------
You'll need a compiler with C99-style variadic macro support though
(which is supported by most compilers I am aware of), or preprocess
your source accordingly.

Sep 4 '08 #15

P: n/a
On 2008-09-03 15:33:32 -0400, Peng Yu <Pe*******@gmail.comsaid:
>
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.
There will be, in the next C++ standard. But not yet.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Sep 5 '08 #16

P: n/a
On Sep 5, 8:37 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2008-09-03 15:33:32 -0400, Peng Yu <PengYu...@gmail.comsaid:
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.

There will be, in the next C++ standard. But not yet.
Well. If it returns by const ref like the current one then I'll
certainly be rolling my own again.. as I do now ;-)
regards
Andy Little

Sep 7 '08 #17

This discussion thread is closed

Replies have been disabled for this discussion.