Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is there a min function that accepts any number of arguments?

Reply
Thread Tools

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

 
 
Peng Yu
Guest
Posts: n/a
 
      09-03-2008
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
 
 
 
 
Leandro Melo
Guest
Posts: n/a
 
      09-03-2008
On 3 set, 16:33, Peng Yu <(E-Mail Removed)> 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
 
 
 
 
Peng Yu
Guest
Posts: n/a
 
      09-03-2008
On Sep 3, 2:40 pm, Leandro Melo <(E-Mail Removed)> wrote:
> On 3 set, 16:33, Peng Yu <(E-Mail Removed)> 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
 
Peng Yu
Guest
Posts: n/a
 
      09-03-2008
On Sep 3, 2:49 pm, Victor Bazarov <(E-Mail Removed)> wrote:
> Peng Yu wrote:
> > On Sep 3, 2:40 pm, Leandro Melo <(E-Mail Removed)> wrote:
> >> On 3 set, 16:33, Peng Yu <(E-Mail Removed)> 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
 
Juha Nieminen
Guest
Posts: n/a
 
      09-03-2008
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
 
Leandro Melo
Guest
Posts: n/a
 
      09-03-2008
On 3 set, 17:04, Juha Nieminen <(E-Mail Removed)> 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
 
Peng Yu
Guest
Posts: n/a
 
      09-04-2008
On Sep 3, 7:21 pm, "Daniel T." <(E-Mail Removed)> wrote:
> Peng Yu <(E-Mail Removed)> 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
 
Reply With Quote
 
Michael DOUBEZ
Guest
Posts: n/a
 
      09-04-2008
Peng Yu a crit :
> On Sep 3, 7:21 pm, "Daniel T." <(E-Mail Removed)> wrote:
>> Peng Yu <(E-Mail Removed)> 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';
>> }
>>
>> }


>
> 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.


This is hideous but you could do something like:

template<typename T, class Compare = std::less<T> >
struct min_of
{
const T& value;

min_of(const T& v):value(v){}

min_of operator()(const T& v) const
{
return (Compare()(value,v))?*this:min_of(v);
}
};


int main()
{
int a=-10;
const int b=20;
int min_val=min_of<int>(1)(a)(2)(b)(-1)(4).value;
std::cout<<min_val<<std::endl;

return 0;
}

--
Michael
 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      09-04-2008
On 3 Sep, 20:33, Peng Yu <(E-Mail Removed)> 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.


put them in a container and use min_element()?
You really want to use Lisp don't you?

--
Nick Keighley
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-04-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
function that accepts any amount of arguments? globalrev Python 14 04-28-2008 01:26 PM
When is min(a, b) != min(b, a)? Albert Hopkins Python 31 02-04-2008 07:37 PM
How to write min(a,b) instead of Math.min(a,b) juergen Java 3 09-20-2006 04:20 AM
CSS min-width, max-width, and min-height with display:inline Lois HTML 1 12-27-2004 03:03 AM



Advertisments