473,805 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Sep 3 '08 #1
16 3875
On 3 set, 16:33, Peng Yu <PengYu...@gmai l.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
On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail. comwrote:
On 3 set, 16:33, Peng Yu <PengYu...@gmai l.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
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_typ e min_of(It from, It to)
{
if (from == to) throw "empty range";
typename iterator_traits <It>::value_typ e 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_arr ay) / sizeof(*temp_ar ray);
double mymin = min_of(temp_arr ay, 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
Peng Yu wrote:
On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail. comwrote:
>On 3 set, 16:33, Peng Yu <PengYu...@gmai l.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
On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
Peng Yu wrote:
On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail. comwrote:
On 3 set, 16:33, Peng Yu <PengYu...@gmai l.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
Peng Yu wrote:
On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@com Acast.netwrote:
>Peng Yu wrote:
>>On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail. comwrote:
On 3 set, 16:33, Peng Yu <PengYu...@gmai l.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'
implementati on using recursive template definitions in no time, can't you?

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

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

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

template<cla ss 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
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
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
Peng Yu <Pe*******@gmai l.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2915
by: Stephen Boulet | last post by:
I've run across code like "myfunction(x, *someargs, **someotherargs)", but haven't seen documentation for this. Can someone fill me in on what the leading * and ** do? Thanks. Stephen
3
14960
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
18
2105
by: Razvan | last post by:
Hi! What is the purpose of such a function ? int function(void)
35
2471
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your time in considering this post, and for your comments. I wrote this function to simplify the task of combining strings using mixed sources. I often see the use of sprintf() which results in several lines of code, and more processing than really...
7
1856
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*); int bar(int *, char *, VFPTR, void *);
3
2804
by: Nimmi Srivastav | last post by:
Consider two functions A and B, both of which accept a variable number of arguments (va_start, va-arg, va_end). Is there an easy way for arguments passed to A to be, in turn, passed to B? (For example, if A is a wrapper function around B). Thanks, Nimmi
2
1614
by: MuZZy | last post by:
Hi, Maybe it's a known issue but i couldn't find any answer so far... Consider this c# function: // ---------------------------------- public int SumUp(params int iArgs) { int i = 0;
16
1252
by: globalrev | last post by:
if i want a function that can take any amount of arguments how do i do? lets say i want a function average that accepts any number of integers and returns the average.
6
1503
by: dlists.cad | last post by:
Hi. I am looking for a way to check if some given set of (*args, **kwds) conforms to the argument specification of a given function, without calling that function. For example, given the function foo: def foo(a, b, c): pass and some tuple args and some dict kwds, is there a way to tell if i _could_ call foo(*args, **kwds) without getting an exception for those arguments? I am hoping there is a way to do this without actually
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.