std::ranges:: remove_copy, std::ranges:: remove_copy_if, std::ranges:: remove_copy_result, std::ranges:: remove_copy_if_result
|
헤더 파일에 정의됨
<algorithm>
|
||
|
호출 시그니처
|
||
| (1) | ||
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
std::
weakly_incrementable
O,
class
T,
class
Proj
=
std::
identity
>
|
(C++20부터)
(C++26까지) |
|
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
std::
weakly_incrementable
O,
class
Proj
=
std::
identity
,
|
(C++26부터) | |
| (2) | ||
|
template
<
ranges::
input_range
R,
std::
weakly_incrementable
O,
class
T,
class
Proj
=
std::
identity
>
|
(C++20부터)
(C++26까지) |
|
|
template
<
ranges::
input_range
R,
std::
weakly_incrementable
O,
class
Proj
=
std::
identity
,
|
(C++26부터) | |
|
template
<
std::
input_iterator
I,
std::
sentinel_for
<
I
>
S,
std::
weakly_incrementable
O,
class
Proj
=
std::
identity
,
|
(3) | (C++20 이후) |
|
template
<
ranges::
input_range
R,
std::
weakly_incrementable
O,
class
Proj
=
std::
identity
,
|
(4) | (C++20 이후) |
|
도우미 타입
|
||
|
template
<
class
I,
class
O
>
using remove_copy_result = ranges:: in_out_result < I, O > ; |
(5) | (C++20 이후) |
|
template
<
class
I,
class
O
>
using remove_copy_if_result = ranges:: in_out_result < I, O > ; |
(6) | (C++20 이후) |
소스 범위
[
first
,
last
)
에서 요소들을 대상 범위의
result
에서 시작하는 위치로 복사하되, (
proj
로 투영된 후) 특정 조건을 만족하는 요소들은 생략합니다. 소스와 대상 범위가 겹치는 경우 동작은 정의되지 않습니다.
이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진)입니다. 즉:
- 명시적 템플릿 인수 목록은 이들 중 어느 것을 호출할 때도 지정할 수 없습니다.
- 이들 중 어느 것도 인수 의존 이름 검색 에 보이지 않습니다.
- 이들 중 어느 것이 함수 호출 연산자 왼쪽의 이름으로 일반 비한정 이름 검색 에 의해 발견될 때, 인수 의존 이름 검색 이 억제됩니다.
목차 |
매개변수
| first, last | - | 처리할 요소들의 소스 범위 를 정의하는 반복자-감시자 쌍 |
| r | - | 요소들의 소스 범위 |
| result | - | 대상 범위의 시작점 |
| value | - | 복사 하지 않을 요소들의 값 |
| comp | - | 투영된 요소들을 비교하기 위한 이항 조건자 |
| proj | - | 요소들에 적용할 투영 |
반환값
{ last, result + N } , 여기서 N 은 복사된 원소의 개수입니다.
복잡도
정확히 ranges:: distance ( first, last ) 번의 해당 predicate comp 및 projection proj 적용이 수행됩니다.
참고 사항
알고리즘은 안정적입니다. 즉, 복사된 요소들의 상대적 순서를 유지합니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | 알고리즘을 위한 목록 초기화 ( 1,2 ) |
가능한 구현
| remove_copy (1,2) |
|---|
struct remove_copy_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, class Proj = std::identity, class T = std::projected_value_t<I, Proj>> requires std::indirectly_copyable<I, O> && std::indirect_binary_predicate<ranges::equal_to, std::projected<I, Proj>, const T*> constexpr ranges::remove_copy_result<I, O> operator()(I first, S last, O result, const T& value, Proj proj = {}) const { for (; !(first == last); ++first) if (value != std::invoke(proj, *first)) { *result = *first; ++result; } return {std::move(first), std::move(result)}; } template<ranges::input_range R, std::weakly_incrementable O, class Proj = std::identity, class T = std::projected_value_t<ranges::iterator_t<R>, Proj>> requires std::indirectly_copyable<ranges::iterator_t<R>, O> && std::indirect_binary_predicate<ranges::equal_to, std::projected<ranges::iterator_t<R>, Proj>, const T*> constexpr ranges::remove_copy_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result, const T& value, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(result), value, std::move(proj)); } }; inline constexpr remove_copy_fn remove_copy {}; |
| remove_copy_if (3,4) |
struct remove_copy_if_fn { template<std::input_iterator I, std::sentinel_for<I> S, std::weakly_incrementable O, class Proj = std::identity, std::indirect_unary_predicate<std::projected<I, Proj>> Pred> requires std::indirectly_copyable<I, O> constexpr ranges::remove_copy_if_result<I, O> operator()(I first, S last, O result, Pred pred, Proj proj = {}) const { for (; first != last; ++first) if (false == std::invoke(pred, std::invoke(proj, *first))) { *result = *first; ++result; } return {std::move(first), std::move(result)}; } template<ranges::input_range R, std::weakly_incrementable O, class Proj = std::identity, std::indirect_unary_predicate< std::projected<ranges::iterator_t<R>, Proj>> Pred> requires std::indirectly_copyable<ranges::iterator_t<R>, O> constexpr ranges::remove_copy_if_result<ranges::borrowed_iterator_t<R>, O> operator()(R&& r, O result, Pred pred, Proj proj = {}) const { return (*this)(ranges::begin(r), ranges::end(r), std::move(result), std::move(pred), std::move(proj)); } }; inline constexpr remove_copy_if_fn remove_copy_if {}; |
예제
#include <algorithm> #include <array> #include <complex> #include <iomanip> #include <iostream> #include <iterator> #include <string_view> #include <vector> void println(const auto rem, const auto& v) { std::cout << rem << ' '; for (const auto& e : v) std::cout << e << ' '; std::cout << '\n'; } int main() { // 주어진 문자열에서 해시 기호를 필터링합니다. const std::string_view str{"#Small #Buffer #Optimization"}; std::cout << "before: " << std::quoted(str) << '\n'; std::cout << "after: \""; std::ranges::remove_copy(str.begin(), str.end(), std::ostream_iterator<char>(std::cout), '#'); std::cout << "\"\n"; // 허수부가 양수인 복소수만 복사합니다. using Ci = std::complex<int>; constexpr std::array<Ci, 5> source { Ci{1, 0}, Ci{0, 1}, Ci{2, -1}, Ci{3, 2}, Ci{4, -3} }; std::vector<std::complex<int>> target; std::ranges::remove_copy_if ( source, std::back_inserter(target), [](int imag) { return imag <= 0; }, [](Ci z) { return z.imag(); } ); println("source:", source); println("target:", target); std::vector<std::complex<float>> nums{{2, 2}, {1, 3}, {4, 8}, {1, 3}}; std::vector<std::complex<double>> outs; #ifdef __cpp_lib_algorithm_default_value_type std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs), {1, 3}); // T는 std::complex<float>로 추론됩니다 #else std::remove_copy(nums.cbegin(), nums.cend(), std::back_inserter(outs), std::complex<float>{1, 3}); #endif println("nums: ", nums); println("outs: ", outs); }
출력:
before: "#Small #Buffer #Optimization" after: "Small Buffer Optimization" source: (1,0) (0,1) (2,-1) (3,2) (4,-3) target: (0,1) (3,2) nums: (2,2) (1,3) (4,8) (1,3) outs: (2,2) (4,8)
참고 항목
|
(C++20)
(C++20)
|
특정 조건을 만족하는 요소들을 제거함
(알고리즘 함수 객체) |
|
(C++20)
(C++20)
|
요소들의 범위를 새로운 위치로 복사함
(알고리즘 함수 객체) |
|
(C++20)
|
지정된 개수의 요소들을 새로운 위치로 복사함
(알고리즘 함수 객체) |
|
(C++20)
|
요소들의 범위를 역순으로 복사함
(알고리즘 함수 객체) |
|
(C++20)
(C++20)
|
범위를 복사하면서 특정 조건을 만족하는 요소들을 다른 값으로 대체함
(알고리즘 함수 객체) |
|
(C++20)
|
역순으로 된 범위의 복사본을 생성함
(알고리즘 함수 객체) |
|
(C++20)
|
요소들의 범위를 복사하고 회전시킴
(알고리즘 함수 객체) |
|
(C++20)
|
연속된 중복 요소가 없는 일부 범위의 복사본을 생성함
(알고리즘 함수 객체) |
|
특정 조건을 만족하는 요소들을 생략하고 범위를 복사함
(함수 템플릿) |