| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> 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 |
|
#3
| |||
| |||
| On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote: > On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> 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 |
|
#4
| |||
| |||
| 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 |
|
#5
| |||
| |||
| Peng Yu wrote: > On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote: >> On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> 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 V -- Please remove capital 'A's when replying by e-mail I do not respond to top-posted replies, please don't ask |
|
#6
| |||
| |||
| On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote: > Peng Yu wrote: > > On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote: > >> On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> 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 |
|
#7
| |||
| |||
| Peng Yu wrote: > On Sep 3, 2:49 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote: >> Peng Yu wrote: >>> On Sep 3, 2:40 pm, Leandro Melo <ltcm...@gmail.com> wrote: >>>> On 3 set, 16:33, Peng Yu <PengYu...@gmail.com> 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. 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 |
|
#8
| |||
| |||
| 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. |
|
#9
| |||
| |||
| On 3 set, 17:04, Juha Nieminen <nos...@thanks.invalid> 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. |
|
#10
| |||
| |||
| Peng Yu <PengYu.UT@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'; } } |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.