for๋ฌธ
๐ ๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ (Range-based for Statement) ์ ๋ฆฌ
๊ธฐ๋ณธ ํ์ | ์ค๋ช | ์์ ์ฝ๋ | ์ฌ์ฉํ๋ ๊ฒฝ์ฐ |
๊ฐ ๋ณต์ฌ (Value) | ๋ฐฐ์ด ์์๋ฅผ ๋ณต์ฌํ์ฌ ๋ณ์์ ์ ์ฅ | for (auto element : array) {} | ๊ฐ์ด ๋ณ๊ฒฝ๋ ํ์ ์๊ณ , ์๋ณธ ๋ฐฐ์ด์ ๋ณดํธํด์ผ ํ ๋ |
์ฐธ์กฐ (Reference) | ๋ฐฐ์ด ์์๋ฅผ ๋ณต์ฌํ์ง ์๊ณ ์ง์ ์ฐธ์กฐ | for (auto& element : array) {} | ์ฑ๋ฅ ์ต์ ํ ๋ฐ ๋ฐฐ์ด ์์ ์์ ์ด ํ์ํ ๊ฒฝ์ฐ |
์์ ์ฐธ์กฐ (Const Reference) | ๋ฐฐ์ด ์์๋ฅผ ๋ณต์ฌํ์ง ์๊ณ ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์ฐธ์กฐ | for (const auto& element : array) {} | ์ฑ๋ฅ ์ต์ ํ ๋ฐ ๊ฐ์ด ๋ณ๊ฒฝ๋๋ฉด ์ ๋๋ ๊ฒฝ์ฐ |
auto ํค์๋ | ๋ฐฐ์ด ์์์ ํ์ ์ ์๋์ผ๋ก ์ถ๋ก | for (auto element : array) {} | ์ฝ๋ ๊ฐ๋ ์ฑ ํฅ์ ๋ฐ ๊ฐ๋ณ ๋ฐ์ดํฐ ํ์ ์ฒ๋ฆฌ |
std::vector | ๋์ ๋ฐฐ์ด์ ์ฌ์ฉํ์ฌ ์์ ํ ๋ฐ๋ณต ์ฒ๋ฆฌ | for (const auto& num : vector) {} | ๊ณ ์ ๋ฐฐ์ด์ด ์๋ ๋์ ๋ฐฐ์ด์ ์ฌ์ฉํ ๋ |
std::map | ํค-๊ฐ ์์ ๋ฐ๋ณตํ๋ฉฐ ์ฒ๋ฆฌ | for (const auto& [key, value] : map) {} | ํค-๊ฐ ์์ ์ํํด์ผ ํ๋ ๊ฒฝ์ฐ |
โ๏ธ ์ฑ๋ฅ์ ๊ณ ๋ คํ๋ฉด const auto& ์ฌ์ฉ์ด ๊ฐ์ฅ ํจ์จ์ ! ๐
๐น ๊ธฐ๋ณธ for ๋ฌธ ์์
#include <iostream>
int main() {
const int numStudents = 5;
int scores[numStudents] = { 84, 92, 76, 81, 56 };
int maxScore = 0; // ์ต๊ณ ์ ์๋ฅผ ์ ์ฅํ ๋ณ์
for (int student = 0; student < numStudents; ++student)
if (scores[student] > maxScore)
maxScore = scores[student];
std::cout << "The best score was " << maxScore << '\n';
return 0;
}
โ๏ธ ๋ฐฐ์ด์ ๋ฐ๋ณตํ ๋ ์ ์ฉํ์ง๋ง ์ธ๋ฑ์ค ๊ด๋ฆฌ๊ฐ ํ์ํ๊ณ ์ค์ํ๊ธฐ ์ฌ์
๐น ๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ (Range-based for Statement)
๐ C++11์์ ๋์
๋ ๊ฐ๊ฒฐํ๊ณ ์์ ํ for ๋ฌธ
๐น ๋ฌธ๋ฒ:
for (element_declaration : array)
statement;
โ๏ธ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์๋์ผ๋ก ์ํํ๋ฉฐ ๊ฐ ์์๋ฅผ ๋ณ์์ ํ ๋น
๐ฅ ์์ : Fibonacci ๋ฐฐ์ด ์ถ๋ ฅ
#include <iostream>
int main() {
int fibonacci[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
for (int number : fibonacci) // ๋ฐฐ์ด์ ๊ฐ ์์๋ฅผ ์ํ
std::cout << number << ' ';
return 0;
}
๐ ๋์ ๋ฐฉ์:
1๏ธโฃ ์ฒซ ๋ฒ์งธ ์์ 0์ด number์ ํ ๋น๋จ
2๏ธโฃ ์ถ๋ ฅ ํ ๋ค์ ์์ 1์ด number์ ํ ๋น๋จ
3๏ธโฃ ๋ฐฐ์ด์ด ๋๋ ๋๊น์ง ๋ฐ๋ณต
๐น auto ํค์๋ ํ์ฉ
๐ ๋ฐฐ์ด์ ์๋ฃํ์ ์๋ ์ถ๋ก
for (auto number : fibonacci)
std::cout << number << ' ';
โ๏ธ ๊ฐ๋ ์ฑ ํฅ์ ๋ฐ ์ค์ ๋ฐฉ์ง
๐น ์ฐธ์กฐ (&)๋ฅผ ํ์ฉํ ๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ
โ ๊ฐ์ ๋ณต์ฌํ๋ ๋์ ์ฐธ์กฐ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋ชจ๋ฆฌ ์ ์ฝ ๊ฐ๋ฅ
๐ซ ๊ฐ ๋ณต์ฌ ์์ (๋นํจ์จ์ )
int array[5] = { 9, 7, 5, 3, 1 };
for (auto element : array) // ์์๋ฅผ ๋ณต์ฌ
std::cout << element << ' ';
โ๏ธ ์ฐธ์กฐ(&)๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด ์์๋ฅผ ์ง์ ์กฐ์ ๊ฐ๋ฅ
int array[5] = { 9, 7, 5, 3, 1 };
for (auto &element : array) // ์ฐธ์กฐ ์ฌ์ฉ (๋ณต์ฌ ๋ฐฉ์ง)
std::cout << element << ' ';
๐ก ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก ์ฌ์ฉํ๋ ค๋ฉด const auto& ์ถ๊ฐ
for (const auto& element : array)
std::cout << element << ' ';
โ๏ธ ๋ถํ์ํ ๋ณ๊ฒฝ ๋ฐฉ์ง ๋ฐ ์ฑ๋ฅ ์ต์ ํ
๐น ๊ธฐ์กด ์์ ์ ๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ ์ ์ฉ
#include <iostream>
int main() {
const int numStudents = 5;
int scores[numStudents] = { 84, 92, 76, 81, 56 };
int maxScore = 0;
for (const auto& score : scores) // ๋ฐฐ์ด ์์ ์ง์ ์ ๊ทผ
if (score > maxScore)
maxScore = score;
std::cout << "The best score was " << maxScore << '\n';
return 0;
}
โ๏ธ ๋ฐฐ์ด ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋ ๊ฐ๊ฒฐํ๊ฒ ์์ฑ ๊ฐ๋ฅ
๐น ๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ๊ณผ ๋น๋ฐฐ์ด ์ปจํ ์ด๋ (std::vector ๋ฑ)
๐ ๋ฐฐ์ด๋ฟ๋ง ์๋๋ผ std::vector, std::list, std::set, std::map ๋ฑ์์๋ ์ฌ์ฉ ๊ฐ๋ฅ
#include <vector>
#include <iostream>
int main() {
std::vector<int> fibonacci = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
for (const auto& number : fibonacci)
std::cout << number << ' ';
return 0;
}
โ๏ธ ์ ์ฐํ๊ฒ ๋ค์ํ ์๋ฃ๊ตฌ์กฐ ํ์ฉ ๊ฐ๋ฅ
๐น ํฌ์ธํฐ ๋ฐฐ์ด์์ ๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ ์ฌ์ฉ ๋ถ๊ฐ ๐ซ
#include <iostream>
int sumArray(int array[]) {
int sum = 0;
for (const auto& number : array) // โ ์ปดํ์ผ ์ค๋ฅ (๋ฐฐ์ด ํฌ๊ธฐ ์ ์ ์์)
sum += number;
return sum;
}
โ๏ธ ๋ฐฐ์ด์ด ํฌ์ธํฐ๋ก ๋ณํ๋๋ฉด ํฌ๊ธฐ๋ฅผ ์ ์ ์๊ธฐ ๋๋ฌธ
๐จ ๋์ ๋ฐฐ์ด์์๋ ๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
๐ ์ ๋ฆฌ
โ
๋ฒ์ ๊ธฐ๋ฐ for ๋ฌธ์ ๋ฐฐ์ด ์์๋ฅผ ์๋ ์ํํ์ฌ ์ฝ๋ ๊ฐ๋
์ฑ๊ณผ ์์ ์ฑ์ ํฅ์
โ
auto ํค์๋๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ฃํ์ ์๋ ์ถ๋ก ๊ฐ๋ฅ
โ
์ฐธ์กฐ(&)๋ฅผ ์ฌ์ฉํ๋ฉด ๋ถํ์ํ ๋ณต์ฌ ๋ฐฉ์ง ๋ฐ ์ฑ๋ฅ ์ต์ ํ
โ
๋ฐฐ์ด๋ฟ๋ง ์๋๋ผ std::vector ๋ฑ์์๋ ํ์ฉ ๊ฐ๋ฅ
๐จ ํฌ์ธํฐ๋ก ๋ณํ๋ ๋ฐฐ์ด ๋ฐ ๋์ ๋ฐฐ์ด์์๋ ์ฌ์ฉ ๋ถ๊ฐ๋ฅ
๐ ์ถ์ฒ: ์๋ ์ฝ๋ฉ ๐