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

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

Object Mix

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

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
Reply With Quote
  #12  
Old 09-04-2008, 04:33 AM
Michael DOUBEZ
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

Peng Yu a écrit :
> 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';
>> }
>>
>> }


>
> 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
  #13  
Old 09-04-2008, 05:31 AM
Nick Keighley
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On 3 Sep, 20:33, 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.


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

--
Nick Keighley
Reply With Quote
  #14  
Old 09-04-2008, 09:46 AM
Juha Nieminen
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

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
  #15  
Old 09-04-2008, 12:21 PM
Daniel T.
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On Sep 3, 9:22*pm, Peng Yu <PengYu...@gmail.com> wrote:
> 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.


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.
Reply With Quote
  #16  
Old 09-04-2008, 01:27 PM
Marco Manfredini
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

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<T> operator , (minimum_t, const T &x)
{
return tmp_min_t<T>(x);
}
template<class T>
inline tmp_min_t<T> operator , (tmp_min_t<T> a, 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.

Reply With Quote
  #17  
Old 09-05-2008, 03:37 PM
Pete Becker
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On 2008-09-03 15:33:32 -0400, Peng Yu <PengYu.UT@gmail.com> said:

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

Reply With Quote
  #18  
Old 09-07-2008, 01:22 AM
kwikius
Guest
 
Default Re: Is there a min function that accepts any number of arguments?

On Sep 5, 8:37 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-09-03 15:33:32 -0400, Peng Yu <PengYu...@gmail.com> said:
>
>
>
> > 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

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 02:35 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.