본문 바로가기
코딩/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
}

'코딩 > C++' 카테고리의 다른 글

class  (0) 2024.05.28
argc, argv  (0) 2024.05.28
Constructor  (0) 2024.05.28
Encapsulation  (0) 2024.05.28
vector를 스택처럼 사용  (0) 2024.03.21