ranges::replace_copy

ranges::replace_copy (교체하고 복사 — ranges)

범위에서 특정 값/조건을 만족하는 원소를 새 값으로 교체해 복사하는 ranges 버전 알고리즘이에요. <algorithm> 헤더에 있어요.

출처: cppreference

본문

std::ranges::replace_copy[first, last)에서 value와 같은 원소를 new_value로 바꿔 result에 복사해요. 원본은 그대로 두고요.

namespace std::ranges {
template< std::input_iterator I, std::sentinel_for<I> S,
          std::weakly_incrementable O, class T1, class T2,
          class Proj = std::identity >
requires std::indirectly_copyable<I, O>
constexpr replace_copy_result<I, O>
    replace_copy( I first, S last, O result, const T1& old_value,
                  const T2& new_value, Proj proj = {} );
}
  • 반환 타입 replace_copy_result{in, out}.
  • replace_copy_if는 술어 기반 버전.
std::vector<int> v{1, 2, 3, 2, 4};
std::vector<int> out;
std::ranges::replace_copy(v, std::back_inserter(out), 2, 9);
// out == {1,9,3,9,4}

replace가 제자리라면, replace_copy는 교체된 결과를 새 컨테이너에 담아 원본을 보존해요.

더 알아보기 (Learn more)

cppreference