std::basic_string<CharT,Traits,Allocator>::replace
cppreference.net에서
basic_string& replace( size_type pos, size_type count,
const basic_string& str );
|
(1) | (C++20부터 constexpr) |
basic_string& replace( const_iterator first, const_iterator last,
const basic_string& str );
|
(2) | (C++20부터 constexpr) |
basic_string& replace( size_type pos, size_type count,
const basic_string& str,
size_type pos2, size_type count2 );
|
(3) | (C++14까지) |
basic_string& replace( size_type pos, size_type count,
const basic_string& str,
size_type pos2, size_type count2 = npos );
|
(C++14부터) (C++20부터 constexpr) |
|
basic_string& replace( size_type pos, size_type count,
const CharT* cstr, size_type count2 );
|
(4) | (C++20부터 constexpr) |
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr, size_type count2 );
|
(5) | (C++20부터 constexpr) |
basic_string& replace( size_type pos, size_type count,
const CharT* cstr );
|
(6) | (C++20부터 constexpr) |
basic_string& replace( const_iterator first, const_iterator last,
const CharT* cstr );
|
(7) | (C++20부터 constexpr) |
basic_string& replace( size_type pos, size_type count,
size_type count2, CharT ch );
|
(8) | (C++20부터 constexpr) |
basic_string& replace( const_iterator first, const_iterator last,
size_type count2, CharT ch );
|
(9) | (C++20부터 constexpr) |
template< class InputIt >
basic_string& replace( const_iterator first, const_iterator last,
InputIt first2, InputIt last2 );
|
(10) | (C++20부터 constexpr) |
basic_string& replace( const_iterator first, const_iterator last,
std::initializer_list<CharT> ilist );
|
(11) | (C++11부터) (C++20부터 constexpr) |
template< class StringViewLike >
basic_string& replace( size_type pos, size_type count,
const StringViewLike& t );
|
(12) | (C++17부터) (C++20부터 constexpr) |
template< class StringViewLike >
basic_string& replace( const_iterator first, const_iterator last,
const StringViewLike& t );
|
(13) | (C++17부터) (C++20부터 constexpr) |
template< class StringViewLike >
basic_string& replace( size_type pos, size_type count,
const StringViewLike& t,
size_type pos2, size_type count2 = npos );
|
(14) | (C++17부터) (C++20부터 constexpr) |
범위 [begin() + pos, begin() + std::min(pos + count, size())) 또는 [first, last) 내의 문자들을 주어진 문자들로 대체합니다.
1,2) 해당 문자들이
str로 대체됩니다.3) 해당 문자들이
[pos2, std::min(pos2 + count2, str.size()))의 부분 문자열 str로 대체됩니다.4,5) 해당 문자들이 범위
[cstr, cstr + count2) 내의 문자들로 대체됩니다.[cstr, cstr + count2)이 유효 범위가 아닌 경우, 동작은 정의되지 않습니다.6,7) 해당 문자들이 범위
[cstr, cstr + Traits::length(cstr)) 내의 문자들로 대체됩니다.8,9) 해당 문자들이
count2개의 ch 복사본으로 대체됩니다.10) 해당 문자들이
[first2, last2)에 의해 범위 replace(first, last, basic_string(first2, last2, get_allocator())) 내의 문자들로 대체됩니다.11) 해당 문자들이
ilist 내의 문자들로 대체됩니다.12,13)
t을 sv에 의해 문자열 뷰 std::basic_string_view<CharT, Traits> sv = t;로 암시적으로 변환한 후, 해당 문자들이 sv의 문자들로 대체됩니다. 이러한 오버로드는
std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>>이 true이고 std::is_convertible_v<const StringViewLike&, const CharT*>이 false인 경우에만 오버로드 해결에 참여합니다.14)
t을 sv에 의해 문자열 뷰 std::basic_string_view<CharT, Traits> sv = t;로 암시적으로 변환한 후, 해당 문자들이 하위 뷰 sv.substr(pos2, count2)의 문자들로 대체됩니다. 이 오버로드는
std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>>이 true이고 std::is_convertible_v<const StringViewLike&, const CharT*>이 false인 경우에만 오버로드 해결에 참여합니다.[begin(), first) 또는 [first, last)이 유효 범위가 아닌 경우, 동작은 정의되지 않습니다.
매개변수
| pos | - | 교체될 부분 문자열의 시작 위치 |
| count | - | 교체될 부분 문자열의 길이 |
| first, last | - | 교체될 문자 범위 |
| str | - | 교체에 사용할 문자열 |
| pos2 | - | 교체용 부분 문자열의 시작 위치 |
| count2 | - | 교체에 사용할 문자 수 |
| cstr | - | 교체에 사용할 문자 문자열 포인터 |
| ch | - | 교체에 사용할 문자 값 |
| first2, last2 | - | 교체에 사용할 문자 범위 |
| ilist | - | 교체에 사용할 문자들을 담은 초기화 리스트 |
| t | - | 교체에 사용할 문자들을 가진 객체 ( std::basic_string_view 로 변환 가능) |
| 타입 요구사항 | ||
-
InputIt
는
LegacyInputIterator
요구사항을 충족해야 함
|
||
반환값
* this .
예외
만약 연산으로 인해
size()
가
max_size()
를 초과하게 될 경우,
std::length_error
를 발생시킵니다.
어떤 이유로든 예외가 발생하면, 이 함수들은 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).
예제
|
이 섹션은 불완전합니다
이유: 예시가 없음 |
결함 보고서
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 수정된 동작 |
|---|---|---|---|
| LWG 847 | C++98 | 예외 안전성 보장이 없었음 | 강력한 예외 안전성 보장 추가됨 |
| LWG 1323 | C++98 |
first
와
last
의 타입이
iterator
였음
|
const_iterator
로 변경됨
|
| LWG 2946 | C++17 | (12,13) 오버로드가 특정 경우에 모호성 발생 | 템플릿으로 만들어 회피함 |
참고 항목
(C++23) |
문자열의 지정된 부분을 문자 범위로 대체합니다 (공개 멤버 함수) |
(C++11) |
정규 표현식과 일치하는 부분을 형식화된 대체 텍스트로 대체합니다 (함수 템플릿) |
| 특정 조건을 만족하는 모든 값을 다른 값으로 대체합니다 (함수 템플릿 & 알고리즘 함수 객체) | |
(C++20)(C++20) |