std:: replace_copy, std:: replace_copy_if
|
헤더 파일에 정의됨
<algorithm>
|
||
|
template
<
class
InputIt,
class
OutputIt,
class
T
>
OutputIt replace_copy
(
InputIt first, InputIt last, OutputIt d_first,
|
(1) | (C++20부터 constexpr) |
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
T
>
|
(2) | (C++17부터) |
| (3) | ||
|
template
<
class
InputIt,
class
OutputIt,
class
UnaryPred,
class
T
>
OutputIt replace_copy_if
|
(C++20부터 constexpr)
(C++26까지) |
|
|
template
<
class
InputIt,
class
OutputIt,
class
UnaryPred,
class
T
=
typename
std::
iterator_traits
|
(C++26부터) | |
| (4) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
UnaryPred,
class
T
>
|
(C++17부터)
(C++26까지) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt1,
class
ForwardIt2,
class
UnaryPred,
class
T
=
typename
std::
iterator_traits
|
(C++26부터) | |
범위
[
first
,
last
)
의 요소들을
d_first
에서 시작하는 다른 범위로 복사하되, 특정 조건을 만족하는 모든 요소는
new_value
로 대체합니다.
|
std:: is_execution_policy_v < std:: decay_t < ExecutionPolicy >> 가 true 인 경우. |
(C++20 이전) |
|
std:: is_execution_policy_v < std:: remove_cvref_t < ExecutionPolicy >> 가 true 인 경우. |
(C++20 이후) |
만약 * first 와 new_value 표현식의 결과 중 어떤 것도 d_first 에 쓰기 가능 하지 않다면, 프로그램의 형식이 올바르지 않습니다.
소스 범위와 대상 범위가 겹치는 경우 동작은 정의되지 않습니다.
목차 |
매개변수
| first, last | - | 복사할 요소들의 소스 범위 를 정의하는 반복자 쌍 |
| d_first | - | 대상 범위의 시작 지점 |
| old_value | - | 대체할 요소의 값 |
| policy | - | 사용할 실행 정책 |
| p | - |
요소 값이 대체되어야 할 경우
true
를 반환하는 단항 predicate.
표현식
p
(
v
)
는
|
| new_value | - | 대체 값으로 사용할 값 |
| 타입 요구사항 | ||
-
InputIt
는
LegacyInputIterator
요구사항을 충족해야 합니다.
|
||
-
OutputIt
는
LegacyOutputIterator
요구사항을 충족해야 합니다.
|
||
-
ForwardIt1, ForwardIt2
는
LegacyForwardIterator
요구사항을 충족해야 합니다.
|
||
반환값
복사된 마지막 요소의 다음 요소를 가리키는 반복자.
복잡도
주어진 N 이 std:: distance ( first, last ) 인 경우:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패하는 경우, std::bad_alloc 이 throw됩니다.
가능한 구현
| replace_copy (1) |
|---|
template<class InputIt, class OutputIt, class T> OutputIt replace_copy(InputIt first, InputIt last, OutputIt d_first, const T& old_value, const T& new_value) { for (; first != last; ++first) *d_first++ = (*first == old_value) ? new_value : *first; return d_first; } |
| replace_copy_if (3) |
template<class InputIt, class OutputIt, class UnaryPred, class T = typename std::iterator_traits<ForwardIt>::value_type> OutputIt replace_copy_if(InputIt first, InputIt last, OutputIt d_first, UnaryPred p, const T& new_value) { for (; first != last; ++first) *d_first++ = p(*first) ? new_value : *first; return d_first; } |
참고 사항
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | 목록 초기화 for algorithms ( 3,4 ) |
예제
#include <algorithm> #include <complex> #include <iostream> #include <vector> void println(const auto& seq) { for (const auto& e : seq) std::cout << e << ' '; std::cout << '\n'; } int main() { std::vector<short> src{3, 1, 4, 1, 5, 9, 2, 6, 5}; println(src); std::vector<int> dst(src.size()); std::replace_copy_if(src.cbegin(), src.cend(), dst.begin(), [](short n){ return n > 5; }, 0); println(dst); std::vector<std::complex<double>> src2{{1, 3}, {2, 4}, {3, 5}}, dst2(src2.size()); println(src2); #ifdef __cpp_lib_algorithm_default_value_type std::replace_copy_if(src2.cbegin(), src2.cend(), dst2.begin(), [](std::complex<double> z){ return std::abs(z) < 5; }, {4, 2}); // 가능함, T가 추론되기 때문 #else std::replace_copy_if(src2.cbegin(), src2.cend(), dst2.begin(), [](std::complex<double> z){ return std::abs(z) < 5; }, std::complex<double>{4, 2}); #endif println(dst2); }
출력:
3 1 4 1 5 9 2 6 5 3 1 4 1 5 0 2 0 5 (1,3) (2,4) (3,5) (4,2) (4,2) (3,5)
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 283 | C++98 |
T
가
CopyAssignable
(그리고
replace_copy
의 경우
EqualityComparable
)를
만족해야 했으나,
InputIt
의 값 타입이 항상
T
는 아님
|
요구사항 제거 |
| LWG 337 | C++98 |
replace_copy_if
는
InputIt
가
LegacyIterator [1] 요구사항만 충족하도록 요구함 |
LegacyInputIterator 로 수정 |
-
↑
C++ 표준의 실제 결함은 템플릿 매개변수
InputIterator가Iterator로 잘못 지정되었다는 점입니다. 이는 타입 요구사항에 영향을 미치는데, 그 이유는 C++ 표준이 알고리즘 라이브러리의 함수 템플릿들에 대해Iterator로 끝나는 이름을 가진 템플릿 타입 매개변수들은 해당 반복자 카테고리의 타입 요구사항을 암시한다고 명시하고 있기 때문입니다.
참고 항목
|
특정 조건을 만족하는 모든 값을 다른 값으로 대체합니다
(function template) |
|
|
특정 조건을 만족하는 요소들을 제거합니다
(function template) |
|
|
(C++20)
(C++20)
|
범위를 복사하면서 특정 조건을 만족하는 요소들을 다른 값으로 대체합니다
(algorithm function object) |