std::basic_string<CharT,Traits,Allocator>:: operator=
|
basic_string
&
operator
=
(
const
basic_string
&
str
)
;
|
(1) | (constexpr since C++20) |
|
basic_string
&
operator
=
(
basic_string
&&
str
)
noexcept ( /* see below */ ) ; |
(2) |
(since C++11)
(constexpr since C++20) |
|
basic_string
&
operator
=
(
const
CharT
*
s
)
;
|
(3) | (constexpr since C++20) |
|
basic_string
&
operator
=
(
CharT ch
)
;
|
(4) | (constexpr since C++20) |
|
basic_string
&
operator
=
(
std::
initializer_list
<
CharT
>
ilist
)
;
|
(5) |
(since C++11)
(constexpr since C++20) |
|
template
<
class
StringViewLike
>
basic_string & operator = ( const StringViewLike & t ) ; |
(6) |
(since C++17)
(constexpr since C++20) |
|
basic_string
&
operator
=
(
std::
nullptr_t
)
=
delete
;
|
(7) | (since C++23) |
문자열의 내용을 대체합니다.
std:: basic_string_view < CharT, Traits >> 가 true 이고 std:: is_convertible_v < const StringViewLike & , const CharT * > 가 false 인 경우입니다.
std::basic_string
는
nullptr
로부터 할당될 수 없습니다.
목차 |
매개변수
| ch | - | 문자열의 문자들을 초기화하는 데 사용할 값 |
| str | - | 문자열을 초기화하는 데 사용할 소스 문자열 |
| s | - | 문자열을 초기화하는 데 사용할 null로 종료되는 문자 문자열에 대한 포인터 |
| ilist | - | std::initializer_list 문자열을 초기화하는 데 사용할 |
| t | - | std::basic_string_view 로 변환 가능한 객체로 문자열을 초기화하는 데 사용 |
반환값
* this
복잡도
CharT
는 파괴되어야 합니다). 할당자(allocators)가 동등하지 않고 전파(propagate)되지 않으면,
str
의 크기에 대해서도 선형적입니다(복사본이 생성되어야 함).
예외
propagate_on_container_move_assignment
::
value
||
만약 연산으로 인해
size()
가
max_size()
를 초과하게 될 경우,
std::length_error
를 발생시킵니다.
어떤 이유로든 예외가 발생하면, 이 함수는 아무런 효과를 가지지 않습니다( strong exception safety guarantee ).
예제
#include <iomanip> #include <iostream> #include <string> int main() { std::string str1; std::string str2{"alpha"}; // (1) operator=(const basic_string&); str1 = str2; std::cout << std::quoted(str1) << ' ' // "alpha" << std::quoted(str2) << '\n'; // "alpha" // (2) operator=(basic_string&&); str1 = std::move(str2); std::cout << std::quoted(str1) << ' ' // "alpha" << std::quoted(str2) << '\n'; // "" or "alpha" (unspecified) // (3) operator=(const CharT*); str1 = "beta"; std::cout << std::quoted(str1) << '\n'; // "beta" // (4) operator=(CharT); str1 = '!'; std::cout << std::quoted(str1) << '\n'; // "!" // (5) operator=(std::initializer_list<CharT>); str1 = {'g', 'a', 'm', 'm', 'a'}; std::cout << std::quoted(str1) << '\n'; // "gamma" // (6) operator=(const T&); str1 = 35U; // equivalent to str1 = static_cast<char>(35U); std::cout << std::quoted(str1) << '\n'; // "#" (ASCII = 35) }
가능한 출력:
"alpha" "alpha" "alpha" "" "beta" "!" "gamma" "#"
결함 보고서
다음 동작 변경 결함 보고서는 이전에 게시된 C++ 표준에 소급 적용되었습니다.
| DR | 적용 대상 | 게시된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 847 | C++98 | 예외 안전성 보장이 없었음 | 강력한 예외 안전성 보장 추가 |
| LWG 2063 | C++11 |
이동 할당 연산자가
SequenceContainer 의 의미론적 요구사항을 따르지 않음 |
따르도록 수정 |
| LWG 2946 | C++17 | 오버로드 ( 6 ) 가 일부 경우에 모호성 발생 | 템플릿으로 만들어 회피 |
참고 항목
basic_string
을 생성함
(public member function) |
|
|
문자열에 문자들을 할당함
(public member function) |
|
|
뷰를 할당함
(
std::basic_string_view<CharT,Traits>
의 public member function)
|