std::ranges:: for_each, std::ranges:: for_each_result
std::ranges
| Non-modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Modifying sequence operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Partitioning operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sorting operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Binary search operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Set operations (on sorted ranges) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Heap operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Minimum/maximum operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Permutation operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Fold operations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Operations on uninitialized storage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Return types | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
헤더에 정의됨
<algorithm>
|
||
|
호출 서명
|
||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
class
Proj
=
std::
identity
,
std::
indirectly_unary_invocable
<
std
::
projected
<
I, Proj
>>
Fun
>
|
(1) | (C++20부터) |
|
template
<
ranges::
input_range
R,
class
Proj
=
std::
identity
,
std::
indirectly_unary_invocable
<
|
(2) | (C++20부터) |
|
헬퍼 타입
|
||
|
template
<
class
I,
class
F
>
using for_each_result = ranges:: in_fun_result < I, F > ; |
(3) | (C++20부터) |
[
first
,
last
)
내 각 반복자가 투영한 값의 결과에 순서대로 적용합니다.
두 오버로드 모두에서, 반복자 유형이 변경 가능한 경우, f 는 역참조된 반복자를 통해 범위의 요소들을 수정할 수 있습니다. 만약 f 가 결과를 반환하는 경우, 해당 결과는 무시됩니다.
이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:
- 명시적 템플릿 인수 목록은 이들 중 어느 것을 호출할 때도 지정할 수 없습니다.
- 이들 중 어느 것도 인수 의존 이름 검색 에 보이지 않습니다.
- 이들 중 어느 것이 함수 호출 연산자 왼쪽의 이름으로 일반 비한정 이름 검색 에 의해 발견될 때, 인수 의존 이름 검색 이 억제됩니다.
목차 |
매개변수
| first, last | - | 함수를 적용할 요소들의 범위 를 정의하는 반복자-감시자 쌍 |
| r | - | 함수를 적용할 요소들의 범위 |
| f | - | 투영된 범위에 적용할 함수 |
| proj | - | 요소들에 적용할 투영 |
반환값
{ ranges:: next ( std :: move ( first ) , last ) , std :: move ( f ) }
복잡도
정확히 ranges:: distance ( first, last ) 번의 f 와 proj 적용이 수행됩니다.
가능한 구현
struct for_each_fn { template<std::input_iterator I, std::sentinel_for<I> S, class Proj = std::identity, std::indirectly_unary_invocable<std::projected<I, Proj>> Fun> constexpr ranges::for_each_result<I, Fun> operator()(I first, S last, Fun f, Proj proj = {}) const { for (; first != last; ++first) std::invoke(f, std::invoke(proj, *first)); return {std::move(first), std::move(f)}; } template<ranges::input_range R, class Proj = std::identity, std::indirectly_unary_invocable<std::projected<ranges::iterator_t<R>, Proj>> Fun> constexpr ranges::for_each_result<ranges::borrowed_iterator_t<R>, Fun> operator()(R&& r, Fun f, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(f), std::ref(proj)); } }; inline constexpr for_each_fn for_each; |
예제
다음 예제는
람다 표현식
을 사용하여 벡터의 모든 요소를 증가시킨 후, 펑터(functor)에서 오버로드된
operator()
을 사용하여 그 합계를 계산합니다. 합계를 계산할 때는 전용 알고리즘인
std::accumulate
를 사용하는 것이 권장됩니다.
#include <algorithm> #include <cassert> #include <iostream> #include <string> #include <utility> #include <vector> struct Sum { void operator()(int n) { sum += n; } int sum {0}; }; int main() { std::vector<int> nums {3, 4, 2, 8, 15, 267}; auto print = [](const auto& n) { std::cout << ' ' << n; }; namespace ranges = std::ranges; std::cout << "before:"; ranges::for_each(std::as_const(nums), print); print('\n'); ranges::for_each(nums, [](int& n) { ++n; }); // calls Sum::operator() for each number auto [i, s] = ranges::for_each(nums.begin(), nums.end(), Sum()); assert(i == nums.end()); std::cout << "after: "; ranges::for_each(nums.cbegin(), nums.cend(), print); std::cout << "\n" "sum: " << s.sum << '\n'; using pair = std::pair<int, std::string>; std::vector<pair> pairs {{1,"one"}, {2,"two"}, {3,"tree"}}; std::cout << "project the pair::first: "; ranges::for_each(pairs, print, [](const pair& p) { return p.first; }); std::cout << "\n" "project the pair::second:"; ranges::for_each(pairs, print, &pair::second); print('\n'); }
출력:
before: 3 4 2 8 15 267 after: 4 5 3 9 16 268 sum: 305 project the pair::first: 1 2 3 project the pair::second: one two tree
참고 항목
range-
for
loop
(C++11)
|
범위에 대한 루프 실행 |
|
(C++20)
|
요소 범위에 함수 적용
(알고리즘 함수 객체) |
|
(C++20)
|
시퀀스의 처음 N개 요소에 함수 객체 적용
(알고리즘 함수 객체) |
|
함수 객체
를
범위
의 요소에 적용
(함수 템플릿) |