GREEN๋‚˜๋ฌด 2025. 3. 29. 23:50
728x90

๐Ÿ“Œ ๋ฒ”์œ„ ๊ธฐ๋ฐ˜ 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 ๋“ฑ์—์„œ๋„ ํ™œ์šฉ ๊ฐ€๋Šฅ
๐Ÿšจ ํฌ์ธํ„ฐ๋กœ ๋ณ€ํ™˜๋œ ๋ฐฐ์—ด ๋ฐ ๋™์  ๋ฐฐ์—ด์—์„œ๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ

๐Ÿ”— ์ถœ์ฒ˜: ์†Œ๋…„์ฝ”๋”ฉ ๐Ÿš€