std::basic_string<CharT,Traits,Allocator>::operator=
来自 cppreference.cn
< cpp | string | basic string
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) |
替换字符串的内容。
1) 使用 str 的副本替换内容。如果 *this 和 str 是同一对象,则此函数无效。
与其他序列容器移动赋值不同,指向 str 元素的引用、指针和迭代器可能会失效。
3) 使用以空字符结尾的字符字符串(由 s 指向)替换内容,如同通过 assign(s, Traits::length(s))。
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 时,此重载才参与重载解析。
std::basic_string_view<CharT, Traits>> 为 true 且 std::is_convertible_v<const StringViewLike&, const CharT*> 为 false 时,此重载才参与重载解析。
7)
std::basic_string
不能从 nullptr 赋值。目录 |
[编辑] 参数
ch | - | 用于初始化字符串字符的值 |
str | - | 用作初始化字符串源的字符串 |
s | - | 指向以空字符结尾的字符字符串的指针,用作初始化字符串的源 |
ilist | - | std::initializer_list 用于初始化字符串 |
t | - | 可转换为 std::basic_string_view 的对象,用于初始化字符串 |
[编辑] 返回值
*this
[编辑] 复杂度
1) 与 str 的大小呈线性关系。
2) 与 *this 的大小呈线性关系(形式上,每个
CharT
都必须销毁)。如果分配器不相等且不传播,则也与 str 的大小呈线性关系(必须进行复制)。3) 与 s 的大小呈线性关系。
4) 常数。
5) 与 ilist 的大小呈线性关系。
6) 与 t 的大小呈线性关系。
[编辑] 异常
2)
noexcept 规范:
noexcept(std::allocator_traits<Allocator>::
propagate_on_container_move_assignment::value ||
如果操作将导致 size()
超过 max_size()
,则抛出 std::length_error。
如果由于任何原因抛出异常,则此函数无效(强异常安全保证)。
[编辑] 示例
运行此代码
#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) | |
赋值视图 (public member function of std::basic_string_view<CharT,Traits> ) |