std:: replace, std:: replace_if
|
헤더에 정의됨
<algorithm>
|
||
| (1) | ||
|
template
<
class
ForwardIt,
class
T
>
void
replace
(
ForwardIt first, ForwardIt last,
|
(C++20부터 constexpr)
(C++26까지) |
|
|
template
<
class
ForwardIt,
class
T
=
typename
std::
iterator_traits
<
ForwardIt
>
::
value_type
>
|
(C++26부터) | |
| (2) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
T
>
void
replace
(
ExecutionPolicy
&&
policy,
|
(C++17부터)
(C++26까지) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
T
=
typename
std::
iterator_traits
|
(C++26부터) | |
| (3) | ||
|
template
<
class
ForwardIt,
class
UnaryPred,
class
T
>
void
replace_if
(
ForwardIt first, ForwardIt last,
|
(C++20부터 constexpr)
(C++26까지) |
|
|
template
<
class
ForwardIt,
class
UnaryPred,
class
T
=
typename
std::
iterator_traits
|
(C++26부터) | |
| (4) | ||
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred,
class
T
>
|
(C++17부터)
(C++26까지) |
|
|
template
<
class
ExecutionPolicy,
class
ForwardIt,
class
UnaryPred,
|
(C++26부터) | |
범위
[
first
,
last
)
내의 모든 요소들이 특정 조건을 만족할 경우
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 가 유효하지 않다면 (C++20 이전) new_value 가 first 에 쓰기 가능 하지 않다면 (C++20 이후) 프로그램의 형식이 잘못되었습니다.
목차 |
매개변수
| first, last | - | 처리할 요소들의 범위 를 정의하는 반복자 쌍 |
| old_value | - | 대체할 요소의 값 |
| policy | - | 사용할 실행 정책 |
| p | - |
요소 값을 대체해야 하는 경우
true
를 반환하는 단항 predicate.
표현식
p
(
v
)
는
|
| new_value | - | 대체 값으로 사용할 값 |
| 타입 요구사항 | ||
-
ForwardIt
는
LegacyForwardIterator
요구사항을 충족해야 합니다.
|
||
-
UnaryPred
는
Predicate
요구사항을 충족해야 합니다.
|
||
복잡도
주어진 N 이 std:: distance ( first, last ) 인 경우:
예외
ExecutionPolicy
라는 템플릿 매개변수를 사용하는 오버로드는 다음과 같이 오류를 보고합니다:
-
알고리즘의 일부로 호출된 함수 실행 중 예외가 발생하고
ExecutionPolicy가 표준 정책 중 하나인 경우, std::terminate 가 호출됩니다. 다른ExecutionPolicy의 경우 동작은 구현에 따라 정의됩니다. - 알고리즘이 메모리 할당에 실패할 경우, std::bad_alloc 이 throw됩니다.
참고 사항
알고리즘이
old_value
와
new_value
를 참조로 받기 때문에, 둘 중 하나가 범위
[
first
,
last
)
의 요소에 대한 참조인 경우 예기치 않은 동작이 발생할 수 있습니다.
| Feature-test 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__cpp_lib_algorithm_default_value_type
|
202403
|
(C++26) | 목록 초기화 for algorithms ( 1-4 ) |
가능한 구현
| replace (1) |
|---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> void replace(ForwardIt first, ForwardIt last, const T& old_value, const T& new_value) { for (; first != last; ++first) if (*first == old_value) *first = new_value; } |
| replace_if (3) |
template<class ForwardIt, class UnaryPred, class T = typename std::iterator_traits<ForwardIt>::value_type> void replace_if(ForwardIt first, ForwardIt last, UnaryPred p, const T& new_value) { for (; first != last; ++first) if (p(*first)) *first = new_value; } |
예제
#include <algorithm> #include <array> #include <complex> #include <functional> #include <iostream> void println(const auto& seq) { for (const auto& e : seq) std::cout << e << ' '; std::cout << '\n'; } int main() { std::array<int, 10> s{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; // 모든 8을 88로 교체합니다. std::replace(s.begin(), s.end(), 8, 88); println(s); // 5보다 작은 모든 값을 55로 교체합니다. std::replace_if(s.begin(), s.end(), std::bind(std::less<int>(), std::placeholders::_1, 5), 55); println(s); std::array<std::complex<double>, 2> nums{{{1, 3}, {1, 3}}}; #ifdef __cpp_lib_algorithm_default_value_type std::replace(nums.begin(), nums.end(), {1, 3}, {4, 2}); #else std::replace(nums.begin(), nums.end(), std::complex<double>{1, 3}, std::complex<double>{4, 2}); #endif println(nums); }
출력:
5 7 4 2 88 6 1 9 0 3 5 7 55 55 88 6 55 9 55 55 (4,2), (4,2)
결함 보고서
다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 283 | C++98 |
T
가
CopyAssignable
요구사항을 충족해야 했으며
(
replace
의 경우
EqualityComparable
도)
하지만
ForwardIt
의 값 타입이 항상
T
가 아니며
T
가 항상
ForwardIt
에 쓸 수 있는 것은 아님
|
대신
*
first
=
new_value
가 유효하도록 요구 |
참고 항목
|
범위를 복사하며 특정 조건을 만족하는 요소를 다른 값으로 대체
(함수 템플릿) |
|
|
(C++20)
(C++20)
|
특정 조건을 만족하는 모든 값을 다른 값으로 대체
(알고리즘 함수 객체) |