std::basic_string<CharT,Traits,Allocator>::operator=
来自cppreference.com
                    
                                        
                    < cpp | string | basic string
                    
                                                            
                    | (1) | ||
|   basic_string& operator=( const basic_string& str );  | 
(C++20 前) | |
|   constexpr basic_string& operator=( const basic_string& str );  | 
(C++20 起) | |
| (2) | ||
|   basic_string& operator=( basic_string&& str );  | 
 (C++11 起)  (C++17 前)  | 
|
|   basic_string& operator=( basic_string&& str ) noexcept(/* see below */);  | 
 (C++17 起)  (C++20 前)  | 
|
|   constexpr basic_string& operator=( basic_string&& str ) noexcept(/* see below */);  | 
(C++20 起) | |
| (3) | ||
|   basic_string& operator=( const CharT* s );  | 
(C++20 前) | |
|   constexpr basic_string& operator=( const CharT* s );  | 
(C++20 起) | |
| (4) | ||
|   basic_string& operator=( CharT ch );  | 
(C++20 前) | |
|   constexpr basic_string& operator=( CharT ch );  | 
(C++20 起) | |
| (5) | ||
|   basic_string& operator=( std::initializer_list<CharT> ilist );  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr basic_string& operator=( std::initializer_list<CharT> ilist );  | 
(C++20 起) | |
| (6) | ||
|   template<class T> basic_string& operator=( const T& t );  | 
 (C++17 起)  (C++20 前)  | 
|
|   template<class T> constexpr basic_string& operator=( const T& t );  | 
(C++20 起) | |
替换字符串的内容。
1) 以 
str 的副本替换内容。若 *this 与 str 为同一对象,则此函数无效果。2) 用移动语义以 
str 的内容替换内容。之后 str 在合法但未指定的状态。若 std::allocator_traits<Allocator>::propagate_on_container_move_assignment() 为 true ,则以源分配器的副本替换目标分配器。若它为 false 且源与目标分配器不比较相等,则目标不能接收源内存的所有权,而必须逐个字符赋值,用其自身的分配器按需分配额外内存。不同于其他容器移动赋值,指向 str 的引用、指针和迭代器可能被非法化。3) 以 
s 所指向的空终止字符串的内容替换内容,如同用 assign(s, Traits::length(s)) 。5) 以 initializer_list  
ilist 的内容替换内容,如同用 assign(ilist.begin(), ilist.size()) 。6) 如同用 std::basic_string_view<CharT, Traits> sv = t; 隐式转换 
t 为 string_view sv ,然后如同用 assign(sv) ,以 sv 的内容替换内容。此重载仅若 std::is_convertible_v<const T&, std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const T&, const CharT*> 为 false 才参与重载决议。参数
| ch | - | 用以初始化 string 的字符 | 
| str | - | 用作源初始化 string 的 string | 
| s | - | 指向空终止字符串的指针,用作源初始化 string | 
| init | - | 用作源初始化 string 的 std::initializer_list | 
| t | - | 可转换为用以初始化 string 的 std::basic_string_view 的对象 | 
返回值
*this
复杂度
1) 与 
str 的大小成线性2) 与 
this 的大小成线性(正式而言,必须销毁每个 CharT )。若分配器比较不相等且不传播,则亦与 str 的大小成线性(必须进行复制)3) 与 
s 的大小成线性4) 常数。
5) 与 
init 的大小成线性异常
| 
 2)  
noexcept 规定:  
 noexcept(std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value || std::allocator_traits<Allocator>::is_always_equal::value)  | 
(C++17 起) | 
若操作将导致 size() > max_size() ,则抛出 std::length_error 。
| 
 若因任何原因抛异常,则此函数无效果(强异常保证)。  | 
(C++11 起) | 
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| DR | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| LWG 2063 | C++11 | 非标准化注释曾说交换是移动赋值的合法实现 | 更正以支持分配器 | 
| LWG 2946 | C++17 |  string_view 重载在某些情况下导致歧义
 | 
通过使之为模板来避免 | 
示例
运行此代码
#include <string> #include <iostream> #include <iomanip> 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'; // "" 或 "alpha" (未指定) // (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" "#"
参阅
  构造 basic_string (公开成员函数)  | |
|   赋值字符给字符串  (公开成员函数)  |