Home [C++] for_each
Post
Cancel

[C++] for_each

c++ for_each?

c++에서는 for를 통해 반복동작 수행이 가능 하며 for loop와 c++11 부터 제공되는 Range-based for loop가 존재합니다.
for_each는 c++17부터 반복문을 좀 더 간결하고 유연하게 사용 할 수 있도록 제공됩니다.

기존 for loop

for loop

기본 for loop는 우리가 일반적으로 사용하는 반복문 형태이며
for (int i=0; i < 10; ++i) std::cout << i << std::endl; 이처럼 사용 가능합니다.

for loop
formal syntax:
attr(optional) for ( init-statement condition(optional) ; iteration-expression(optional) ) statement
informal syntax:
attr(optional) for ( declaration-or-expression(optional) ; declaration-or-expression(optional) ; expression(optional) ) statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>

int main() {
    int arr[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (int i = 0; i <10; ++i)
        std::cout << arr[i] << " ";
    std::cout << std::endl;

    std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (auto& it = std::begin(arr); it < std::end(arr); ++it)
        std::cout << *it << " ";
    std::cout << std::endl;
    return 0;
}
1
2
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

ranged-baes for loop

ranged-base for loop는 범위에 대해 for loop를 실행하며
for (int i: {1 ,2, 3, 4, 5}) std::cout << i << std::endl; 이처럼 사용 가능합니다.

Range-based for loop
attr(optional) for ( init-statement(optional) range-declaration : range-expression ) loop-statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>

int main() {
    int arr[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (int value: arr)
        std::cout << value << " ";
    std::cout << std::endl;

    std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    for (int value: vec)
        std::cout << value << " ";
    std::cout << std::endl;

    return 0;
}
1
2
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10

for_each

범위 내에 원소들에 대해 함수를 실행시킵니다.
first 부터 last 전 까지 원소들 각각에 대해 함수 f 을 실행합니다. f 리턴값은 무시되며 함수 포인터가 리턴됩니다.

std::for_each  
Defined in header <algorithm>  
template< class InputIt, class UnaryFunction > UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f ); until C++20
template< class InputIt, class UnaryFunction > constexpr UnaryFunction for_each( InputIt first, InputIt last, UnaryFunction f ); since C++20
template< class ExecutionPolicy, class ForwardIt, class UnaryFunction2 > void for_each( ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, UnaryFunction2 f ); since C++17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <algorithm>
#include <iostream>
#include <vector>

void int_print(int value) { std::cout << value << " "; }

int main() {
    int arr[10]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::for_each (std::begin(arr), std::end(arr), int_print);
    std::cout << std::endl;
    std::for_each (std::begin(arr), std::end(arr), [](int value){ std::cout << value << " ";});
    std::cout << std::endl;
    std::for_each(std::begin(arr), std::begin(arr) + 5, int_print);
    std::cout << std::endl;

    std::vector<int> vec{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::for_each (vec.begin(), vec.end(), int_print);
    std::cout << std::endl;
    std::for_each(vec.begin(), vec.begin() + 5, int_print);
    std::cout << std::endl;
    std::for_each (std::begin(vec), std::end(vec), int_print);
    std::cout << std::endl;
    std::for_each (vec.begin(), vec.end(), [](int value){ std::cout << value << " ";});
    std::cout << std::endl;

    return 0;
}
1
2
3
4
5
6
7
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 
1 2 3 4 5 6 7 8 9 10 
1 2 3 4 5 6 7 8 9 10 

std::for_each는 동작하면 first 부터 last 전까지 반드시 실행이 됩니다. 기존 for loop 혹은 ragned-base for loop 처럼 break;를 통해 동작을 벗어날수 없습니다.


참고

https://en.cppreference.com/w/cpp/algorithm/for_each

This post is licensed under CC BY 4.0 by the author.