是否有接受任意数量参数的min函数? [英] Is there a min function that accepts any number of arguments?

查看:74
本文介绍了是否有接受任意数量参数的min函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想知道是否有一个min函数(在boost中,可能?)

接受任意数量的参数? std :: min只接受两个参数。

如果我想得到许多的最小数字,我多次使用std :: min

,这不是方便。


谢谢,


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

推荐答案

在3集上, 16:33,彭宇< PengYu ... @ gmail.com写道:
On 3 set, 16:33, Peng Yu <PengYu...@gmail.comwrote:




我''我想知道是否有一个min函数(在boost中,可能?)

接受任意数量的参数? std :: min只接受两个参数。

如果我想得到许多的最小数字,我多次使用std :: min

,这不是方便。
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.



嗨。


也许,你可以用for(或者什么东西)来迭代元素

类似)跟踪最小元素。这很简单。

-

Leandro TC Melo

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


9月3日下午2:40, Leandro Melo< ltcm ... @ gmail.comwrote:
On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.comwrote:

3集,16:33,彭宇< PengYu ... @ gmail.comwrote:
On 3 set, 16:33, Peng Yu <PengYu...@gmail.comwrote:


Hi,


我想知道是否有min函数(在boost中,也许?)

接受任意数量的参数? std :: min只接受两个参数。

如果我想得到许多的最小数字,我多次使用std :: min

,这不是方便。
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.



嗨。


也许,你可以用for(或者什么东西)来迭代元素

类似)跟踪最小元素。这很简单。


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


Peng Yu写道:
Peng Yu wrote:

我想知道是否有min函数(在boost中,可能?)

接受任意数量的参数? std :: min只接受两个参数。

如果我想得到许多的最小数字,我多次使用std :: min

,这不是方便。
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.



不,据我所知,没有这样的功能(因为没有办法

来指示该功能何时到停止,或者在函数内部

知道参数的数量是多少)。您可以用最多N个参数编写自己的包装

''min'',不是吗?除此之外,你还是会更好地使用循环来获得
。考虑:


模板< 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 ;

}

返回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);


(我没有查看代码,仅供参考)。


V

-

请在通过电子邮件回复时删除资金''A'

我不回复热门帖子回复,请不要问

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


这篇关于是否有接受任意数量参数的min函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆