본문 바로가기
로봇/C++

C++ Range-based for Loop

by 33곰탱 2025. 1. 9.

신기하기도 하고 혁명인 것 같다

vector<double> temps {87.2, 77.1, 80.0, 72.5};

double average_temp {};
double running_sum {};

for (auto temp : temps)
	running_sum += temp;

average_temp = running_sum / temps.size();

auto

for (auto c : "Frank")
	cout << c << endl;

result

F
r
a
n
k

example

vector<double> temperatures{ 87.9, 77.9, 80.0, 72.5 };
double average_temp{};
double total{};
for (auto t : temperatures)
	total += t;
average_temp = total / temperatures.size();

cout << "total : " << total << endl;
cout << "average_temp is : " << average_temp << endl;

'로봇 > C++' 카테고리의 다른 글

C++ Scope rules 스코프 룰  (1) 2025.01.09
C++ 오버로딩 Overloading Functions  (1) 2025.01.09
C++ string 문자열  (1) 2025.01.09
C++ 벡터  (0) 2025.01.09
C, C++ 차이 배열, 입출력  (0) 2024.12.30