Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>:: replace_with_range

From cppreference.net
std::basic_string
template < container-compatible-range < CharT > R >

constexpr std:: basic_string & replace_with_range ( const_iterator first,
const_iterator last,

R && rg ) ;
(C++23 이후)

[ first , last ) 범위의 문자들을 rg 범위의 문자들로 대체합니다.

다음과 동일함

return replace(first,
               last,
               std::basic_string(
                   std::from_range,
                   std::forward<R>(rg),
                   get_allocator())
);

목차

매개변수

first, last - 대체될 문자들의 범위
rg - container compatible range

반환값

* this

복잡도

크기에 선형적으로 비례하는 rg .

예외

만약 연산으로 인해 size() max_size() 를 초과하게 되면, std::length_error 를 발생시킵니다.

어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).

참고 사항

기능 테스트 매크로 표준 기능
__cpp_lib_containers_ranges 202202L (C++23) 컨테이너 호환 범위 를 허용하는 멤버 함수

예제

#include <algorithm>
#include <cassert>
#include <forward_list>
#include <iterator>
#include <string>
int main()
{
    using namespace std::literals;
    auto s{"Today is today!"s};
    constexpr auto today{"today"sv};
    constexpr auto tomorrow{"tomorrow's yesterday"sv};
    std::forward_list<char> rg;
    std::ranges::reverse_copy(tomorrow, std::front_inserter(rg));
    const auto pos{s.rfind(today)};
    assert(pos != s.npos);
    const auto first{std::next(s.begin(), pos)};
    const auto last{std::next(first, today.length())};
#ifdef __cpp_lib_containers_ranges
    s.replace_range(first, last, rg);
#else
    s.replace(first, last, rg.cbegin(), rg.cend());
#endif
    assert("Today is tomorrow's yesterday!" == s);
}

참고 항목

문자열의 지정된 부분을 대체합니다
(public member function)