std::basic_string<CharT,Traits,Allocator>::assign - cppreference.com
来自cppreference.com
basic_string& assign( const basic_string& str ); |
(1) | (C++20 起为 constexpr) |
basic_string& assign( basic_string&& str ) noexcept(/* 见下文 */); |
(2) | (C++11 起) (C++20 起为 constexpr) |
basic_string& assign( size_type count, CharT ch ); |
(3) | (C++20 起为 constexpr) |
basic_string& assign( const CharT* s, size_type count ); |
(4) | (C++20 起为 constexpr) |
basic_string& assign( const CharT* s ); |
(5) | (C++20 起为 constexpr) |
template< class SV > basic_string& assign( const SV& t ); |
(6) | (C++17 起) (C++20 起为 constexpr) |
template< class SV > basic_string& assign( const SV& t, size_type pos, size_type count = npos); |
(7) | (C++17 起) (C++20 起为 constexpr) |
basic_string& assign( const basic_string& str, size_type pos, size_type count ); |
(8) | (C++14 前) |
basic_string& assign( const basic_string& str, size_type pos, size_type count = npos); |
(C++14 起) (C++20 起为 constexpr) |
|
template< class InputIt > basic_string& assign( InputIt first, InputIt last ); |
(9) | (C++20 起为 constexpr) |
basic_string& assign( std::initializer_list<CharT> ilist ); |
(10) | (C++11 起) (C++20 起为 constexpr) |
替换字符串的内容。
1) 等价于 return *this = str;。
2) 等价于 return *this = std::move(str);。
3) 以字符 ch 的 count 个副本替换字符串的内容。
等价于 clear(); resize(n, c); return *this;。
4) 以范围 [s, s + count) 中的字符的副本替换字符串的内容。
如果 [s, s + count) 不是有效范围,那么行为未定义。
5) 等价于 return assign(s, Traits::length(s));。
6,7) 以从 t 构造的字符串视图 sv 中的字符替换字符串的内容。
- 如果只提供了
t,那么就会以sv中的所有字符进行替换。 - 如果也提供了
pos:- 如果
count是npos,那么就会以sv中从pos处开始的所有字符进行替换。 - 否则会以
sv中从pos处开始的std::min(count, sv.size() - pos)个字符进行替换。
- 如果
这些重载只有在满足以下所有条件时才会参与重载决议:
std::is_convertible_v<const SV&, std::basic_string_view<CharT, Traits>>是true。std::is_convertible_v<const SV&, const CharT*>是false。
6) 等价于 std::basic_string_view<CharT, Traits> sv = t;return assign(sv.data(), sv.size());。
7) 等价于 std::basic_string_view<CharT, Traits> sv = t;return assign(sv.substr(pos, count));。
8) 以 str 中的字符替换字符串的内容。
- 如果
count是npos,那么就会以str中从pos处开始的所有字符进行替换。 - 否则会以
str中从pos处开始的std::min(count, str.size() - pos)个字符进行替换。
|
等价于 |
(C++20 起) |
9) 等价于 return assign(basic_string(first, last, get_allocator()));。
10) 等价于 return assign(ilist.begin(), ilist.size());。
参数
| str | - | 用作源以初始化字符的字符串 |
| count | - | 产生的字符串大小 |
| ch | - | 用以初始化字符串的字符的值 |
| s | - | 指向用作源初始化字符串字符串的指针 |
| t | - | 用以初始化字符串字符的对象(可转换到 std::basic_string_view) |
| pos | - | 要取的首字符下标 |
| first, last | - | 复制字符来源的范围 |
| ilist | - | 用以初始化字符串字符的 std::initializer_list |
返回值
*this
异常
2)
说明:
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。
7) 如果 pos > sv.size() 是 true,那么就会抛出 std::out_of_range。
8) 如果 pos > str.size() 是 true,那么就会抛出 std::out_of_range。
如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。
示例
#include <iostream> #include <iterator> #include <string> int main() { std::string s; // assign(size_type count, CharT ch) s.assign(4, '='); std::cout << s << '\n'; // "====" std::string const c("Exemplary"); // assign(const basic_string& str) s.assign(c); std::cout << c << " == " << s <<'\n'; // "Exemplary == Exemplary" // assign(const basic_string& str, size_type pos, size_type count) s.assign(c, 0, c.length() - 1); std::cout << s << '\n'; // "Exemplar"; // assign(basic_string&& str) s.assign(std::string("C++ by ") + "example"); std::cout << s << '\n'; // "C++ by example" // assign(const CharT* s, size_type count) s.assign("C-style string", 7); std::cout << s << '\n'; // "C-style" // assign(const CharT* s) s.assign("C-style\0string"); std::cout << s << '\n'; // "C-style" char mutable_c_str[] = "C-style string"; // assign(InputIt first, InputIt last) s.assign(std::begin(mutable_c_str), std::end(mutable_c_str) - 1); std::cout << s << '\n'; // "C-style string" // assign(std::initializer_list<CharT> ilist) s.assign({'C', '-', 's', 't', 'y', 'l', 'e'}); std::cout << s << '\n'; // "C-style" }
输出:
==== Exemplary == Exemplary Exemplar C++ by example C-style C-style C-style string C-style
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
|---|---|---|---|
| LWG 847 | C++98 | 没有异常安全保证 | 添加强异常安全保证 |
| LWG 2063 | C++11 | 非正式注释说可以通过交换实现重载 (2) | 更正为要求移动赋值 |
| LWG 2250 | C++98 | pos > str.size() 是 true 时重载 (8) 的行为未定义
|
此时始终会抛出异常 |
| LWG 2579 | C++98 | 重载 (1) 与复制赋值运算符的行为不同 | 它们的行为相同 |
| LWG 2946 | C++17 | 重载 (6) 在某些情况下会导致歧义 | 通过使之为模板避免 |