코딩/C++
ellipsis
by sonysame
2024. 5. 28.
//Ellipsis를 사용하는 elements에 access할 때는 va_list를 사용한다!
#include <iostream>
#include <cstdarg>
using namespace std;
double findAverage(int count, ...) {
double sum = 0;
va_list list;
va_start(list, count);
for (int arg = 0; arg < count; ++arg) {
sum += va_arg(list, int);
}
va_end(list);
return sum / count;
}
int main() {
cout << findAverage(1, 1, 2, 3, "Helllo", 'c') << endl; //1
cout << findAverage(3, 1, 2, 3) << endl; //2
cout << findAverage(5, 1, 2, 3, 4, 5) << endl; //3
cout << findAverage(10, 1, 2, 3, 4, 5) << endl; //4.15655e+08
}