std::basic_string<CharT,Traits,Allocator>::operator+=
basic_string& operator+=( const basic_string& str );
|
(1) | (C++20부터 constexpr) |
basic_string& operator+=( CharT ch );
|
(2) | (C++20부터 constexpr) |
basic_string& operator+=( const CharT* s );
|
(3) | (C++20부터 constexpr) |
basic_string& operator+=( std::initializer_list<CharT> ilist );
|
(4) | (C++11부터) (C++20부터 constexpr) |
template< class StringViewLike >
basic_string& operator+=( const StringViewLike& t );
|
(5) | (C++17부터) (C++20부터 constexpr) |
문자열에 추가 문자들을 덧붙입니다.
str을 덧붙입니다.ch를 덧붙입니다.s이 가리키는 널 종료 문자 문자열을 덧붙입니다. ilist의 문자들을 덧붙입니다.t를 마치 sv에 의해 문자열 뷰 std::basic_string_view<CharT, Traits> sv = t;로 암시적으로 변환한 다음, 마치 sv에 의해 문자열 뷰 append(sv)의 문자들을 덧붙입니다.std::is_convertible_v<const StringViewLike&,std::basic_string_view<CharT, Traits>>이 true이고 std::is_convertible_v<const StringViewLike&, const CharT*>이 false인 경우에만 오버로드 해결에 참여합니다.매개변수
| str | - | 추가할 문자열 |
| ch | - | 추가할 문자 값 |
| s | - | 추가할 null 종료 문자 문자열에 대한 포인터 |
| ilist | - | std::initializer_list 추가할 문자들을 가진 |
| t | - | 객체 ( std::basic_string_view 로 변환 가능) 추가할 문자들을 가진 |
반환값
* this
복잡도
표준적인 복잡도 보장은 없으며, 일반적인 구현은 std::vector::insert() 와 유사하게 동작합니다.
예외
만약 연산으로 인해
size()
가
max_size()
를 초과하게 되면,
std::length_error
를 발생시킵니다.
어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).
참고 사항
오버로드
(
2
)
는 암시적으로
CharT
로 변환 가능한 모든 타입을 수용할 수 있습니다.
std::string
의 경우, 여기서
CharT
가
char
인 경우, 허용 가능한 타입 집합에는 모든 산술 타입이 포함됩니다. 이는 의도하지 않은 효과를 초래할 수 있습니다.
예제
#include <iomanip> #include <iostream> #include <string> int main() { std::string str; // 메모리 재할당을 피하기 위해 충분한 저장 공간 예약 str.reserve(50); std::cout << std::quoted(str) << '\n'; // 빈 문자열 str += "This"; std::cout << std::quoted(str) << '\n'; str += std::string(" is "); std::cout << std::quoted(str) << '\n'; str += 'a'; std::cout << std::quoted(str) << '\n'; str += {' ', 's', 't', 'r', 'i', 'n', 'g', '.'}; std::cout << std::quoted(str) << '\n'; str += 69.96; // str += static_cast<char>(69.96); 와 동일 // 오버로드 (2)에 의해 'E' (ASCII 코드 69)가 추가됨 // 이는 의도하지 않은 결과일 수 있음 // 숫자 값을 추가하려면 std::to_string() 사용 고려: str += std::to_string(1729); std::cout << std::quoted(str) << '\n'; }
출력:
"" "This" "This is " "This is a" "This is a string." "This is a string.E1729"
결함 보고서
다음 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 847 | C++98 | 예외 안전성 보장이 없었음 | 강력한 예외 안전성 보장 추가 |
| LWG 2946 | C++17 | 오버로드 ( 5 ) 가 일부 경우에 모호성 발생 | 템플릿으로 만들어 회피 |
참고 항목
|
문자열 끝에 문자들을 추가
(public member function) |