Namespaces
Variants

std::basic_string<CharT,Traits,Allocator>::operator=

From ko.cppreference.net
 
 
 
std::basic_string
 
basic_string& operator=( const basic_string& str );
(1) (C++20부터 constexpr)
basic_string& operator=( basic_string&& str )
    noexcept(/* see below */);
(2) (C++11부터)
(C++20부터 constexpr)
basic_string& operator=( const CharT* s );
(3) (C++20부터 constexpr)
basic_string& operator=( CharT ch );
(4) (C++20부터 constexpr)
basic_string& operator=( std::initializer_list<CharT> ilist );
(5) (C++11부터)
(C++20부터 constexpr)
template<class StringViewLike>
basic_string& operator=( const StringViewLike& t );
(6) (C++17부터)
(C++20부터 constexpr)
basic_string& operator=( std::nullptr_t ) = delete;
(7) (C++23부터)

문자열의 내용을 교체합니다.

1) 내용을 str의 복사본으로 교체합니다. *this과(와) str이(가) 동일한 객체이면 이 함수는 아무런 효과가 없습니다.
2) str의 내용을 SequenceContainer의 이동 할당 의미 체계를 사용하여 교체합니다.
다른 시퀀스 컨테이너 이동 할당과 달리, str의 요소에 대한 참조, 포인터 및 반복자는 무효화될 수 있습니다.
3) 내용을 s이 가리키는 널 종료 문자 문자열의 내용으로 교체합니다. 마치 assign(s, Traits::length(s))을(를) 사용한 것과 같습니다.
4) 내용을 문자 ch로 교체합니다. 마치 assign(std::addressof(ch), 1)을(를) 사용한 것과 같습니다.
5) 내용을 초기화자 목록 ilist의 내용으로 교체합니다. 마치 assign(ilist.begin(), ilist.size())을(를) 사용한 것과 같습니다.
6) t을(를) 묵시적으로 문자열 뷰 sv으로 변환합니다. 마치 std::basic_string_view<CharT, Traits> sv = t;을(를) 사용한 것과 같으며, 그런 다음 내용을 sv의 내용으로 교체합니다. 마치 assign(sv)을(를) 사용한 것과 같습니다.
이 오버로드는 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>>
이(가) true이고 std::is_convertible_v<const StringViewLike&, const CharT*>이(가) false인 경우에만 오버로드 해결에 참여합니다.
7) std::basic_string nullptr에서 할당할 수 없습니다.

매개변수

ch - 문자열의 문자들을 초기화하는 데 사용할 값
str - 문자열을 초기화하는 데 사용할 소스 문자열
s - 문자열을 초기화하는 데 사용할 null로 종료되는 문자 문자열에 대한 포인터
ilist - std::initializer_list 문자열을 초기화하는 데 사용할
t - std::basic_string_view 로 변환 가능한 객체로 문자열을 초기화하는 데 사용

반환값

* this

복잡도

1) str 의 크기에 선형적입니다.
2) * this 의 크기에 선형적입니다(공식적으로, 각 CharT 는 파괴되어야 합니다). 할당자(allocators)가 동등하지 않고 전파(propagate)되지 않으면, str 의 크기에 대해서도 선형적입니다(복사본이 생성되어야 함).
3) 크기가 s 에 선형적입니다.
4) 상수.
5) ilist 크기에 선형적입니다.
6) 크기에 선형적으로 비례하는 t .

예외

2)
noexcept 명세:
noexcept ( std:: allocator_traits < Allocator > ::

propagate_on_container_move_assignment :: value ||

std:: allocator_traits < Allocator > :: is_always_equal :: 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)