(Python就不需要嵌套)
by WA鸭鸭 @ 2019-03-24 10:15:17
(Python就不需要嵌套)
by 已注销%Jm9VScx @ 2019-03-24 10:25:35
@[guodong](/space/show?uid=81372)
有两种办法:
一、使用va_list
例:
```cpp
#include <cstdarg>
int maxval(unsigned int argc, ...) {
va_list ap;
va_start (ap, argc);
int retval = -0x3f3f3f3f;
for (register unsigned i = 0; i != argc; ++i) {
int tmp = va_arg(ap, int);
if (retval < tmp) retval = tmp;
}
va_end(ap);
return retval;
}
```
使用时第一个参数为传入变量总数,后面跟变量值。
如:
maxval(3, 1, 2, 4) == 4
maxval(3, 1, 1, 1) == 1
二、使用STL
这里给出initializer_list的例子(C++11, 没有的话for循环用迭代器写):
```cpp
int call_max(initializer_list<int> a) {
int retval = -0x3f3f3f3f;
for (int i:a) {
if (retval < i) retval = i;
}
return retval;
}
```
要加花括号
例:
call_max({1,2,3,4}) == 4
by encore @ 2019-05-06 20:08:31
@[guodong](/space/show?uid=81372)
还有一种用不定参数模板的方法:
```cpp
template<typename T>
inline T the_max(T a) {
return a;
}
template<typename T>
inline T the_max(T a, T b) {
return a > b ? a : b;
}
template<typename T, typename... Args>
T the_max(T a, T b, Args ...args) {
return the_max(the_max(a, b), args...);
}
```
直接用。。。
the_max(1,2,3,4,5,6,7,8) == 8
by encore @ 2019-05-06 20:17:40
@[encore](/space/show?uid=113385) define更方便吧。。。
by guodong @ 2019-05-06 21:21:27
@[guodong](/space/show?uid=81372) 你不是说看着写着都难受嘛。。。
by encore @ 2019-05-06 21:30:19
@[encore](/space/show?uid=113385) 而且这样做无论多少个变量都能求,而define要一行行写
by encore @ 2019-05-06 21:31:35
@[encore](/space/show?uid=113385) 那你穿入不也要一个一个写?怎么会有这么毒瘤的出题人呢,最多就5个吧。。
by guodong @ 2019-05-06 21:53:33
@[guodong](/space/show?uid=81372) 那算了,抱歉没能解决你的问题
by encore @ 2019-05-07 16:36:14
@[encore](/space/show?uid=113385) initializer_list好评
by 孤月 @ 2019-08-18 13:51:15