std::basic_string<CharT,Traits,Allocator>::assign
来自cppreference.com
                    
                                        
                    < cpp | string | basic string
                    
                                                            
                    | (1) | ||
|   basic_string& assign( size_type count, CharT ch );  | 
(C++20 前) | |
|   constexpr basic_string& assign( size_type count, CharT ch );  | 
(C++20 起) | |
| (2) | ||
|   basic_string& assign( const basic_string& str );  | 
(C++20 前) | |
|   constexpr basic_string& assign( const basic_string& str );  | 
(C++20 起) | |
| (3) | ||
|   basic_string& assign( const basic_string& str, size_type pos, size_type count );  | 
(C++14 前) | |
|   basic_string& assign( const basic_string& str, size_type pos, size_type count = npos);  | 
 (C++14 起)  (C++20 前)  | 
|
|   constexpr basic_string& assign( const basic_string& str, size_type pos, size_type count = npos);  | 
(C++20 起) | |
| (4) | ||
|   basic_string& assign( basic_string&& str ) noexcept(/* 见下文 */);  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr basic_string& assign( basic_string&& str ) noexcept(/* 见下文 */);  | 
(C++20 起) | |
| (5) | ||
|   basic_string& assign( const CharT* s, size_type count );  | 
(C++20 前) | |
|   constexpr basic_string& assign( const CharT* s, size_type count );  | 
(C++20 起) | |
| (6) | ||
|   basic_string& assign( const CharT* s );  | 
(C++20 前) | |
|   constexpr basic_string& assign( const CharT* s );  | 
(C++20 起) | |
| (7) | ||
|   template< class InputIt > basic_string& assign( InputIt first, InputIt last );  | 
(C++20 前) | |
|   template< class InputIt > constexpr basic_string& assign( InputIt first, InputIt last );  | 
(C++20 起) | |
| (8) | ||
|   basic_string& assign( std::initializer_list<CharT> ilist );  | 
 (C++11 起)  (C++20 前)  | 
|
|   constexpr basic_string& assign( std::initializer_list<CharT> ilist );  | 
(C++20 起) | |
| (9) | ||
|   template < class StringViewLike > basic_string& assign( const StringViewLike& t );  | 
 (C++17 起)  (C++20 前)  | 
|
|   template < class StringViewLike > constexpr basic_string& assign( const StringViewLike& t );  | 
(C++20 起) | |
| (10) | ||
|   template < class StringViewLike > basic_string& assign( const StringViewLike& t,  | 
 (C++17 起)  (C++20 前)  | 
|
|   template < class StringViewLike > constexpr basic_string& assign( const StringViewLike& t,  | 
(C++20 起) | |
替换字符串的内容。
1) 以 count 个 ch 的副本替换字符串的内容。
2) 以 str 的副本替换字符串的内容。等价于 *this = str;。特别是可以发生分配器传播。 (C++11 起)
3) 以 str 的子串 
[pos, pos + count) 替换字符串的内容。如果请求的子串越过字符串尾,或 count == npos,那么产生的子串是 [pos, str.size())。如果 pos > str.size(),那么就会抛出 std::out_of_range。4) 用移动语义以 str 的内容替换字符串的内容。等价于 *this = std::move(str)。特别是可以发生分配器传播。
5) 以范围 
[s, s + count) 中的字符的副本替换字符串的内容。此范围可以包含空字符。6) 以 s 所指向的空终止字符串的内容替换字符串的内容。字符串的长度通过 Traits::length(s) 由首个空字符确定。
7) 以范围 
[first, last) 中的字符的副本替换字符串的内容。如果 InputIt 不满足老式输入迭代器 (LegacyInputIterator) ,那么此重载不会参与重载决议。 (C++11 起)8) 以初始化器列表 ilist 的内容替换字符串的内容。
9) 如同用 std::basic_string_view<CharT, Traits> sv = t; 将 t 隐式转换到字符串视图 sv ,然后如同用 assign(sv.data(), sv.size()) 以字符串视图 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 时才会参与重载决议。
10) 如同用 std::basic_string_view<CharT, Traits> sv = t; 将 t 隐式转换到字符串视图 sv ,然后以来自 sv 的子视图 
[pos, pos + count) 的字符替换字符串的内容。如果请求的子视图越过 sv 的结尾,或 count == npos,那么产生的子视图是 [pos, sv.size())。如果 pos > sv.size(),那么就会抛出 std::out_of_range。 此重载只有在 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 时才会参与重载决议。
参数
| count | - | 产生的字符串大小 | 
| pos | - | 要取的首字符下标 | 
| ch | - | 用以初始化字符串的字符的值 | 
| first, last | - | 复制字符来源的范围 | 
| str | - | 用作源以初始化字符的字符串 | 
| s | - | 指向用作源初始化字符串字符串的指针 | 
| ilist | - | 用以初始化字符串字符的 std::initializer_list | 
| sv | - | 用以初始化字符串字符的 std::basic_string_view | 
| t | - | 用以初始化字符串字符的对象(可转换到 std::basic_string_view) | 
| 类型要求 | ||
 -InputIt 必须符合老式输入迭代器 (LegacyInputIterator)  的要求。
 | ||
返回值
*this
复杂度
1) 与 count 成线性
2) 与 str 的大小成线性
3) 与 count 成线性
4) 常数。在给定 alloc 且 alloc != other.get_allocator() 时成线性。
5) 与 count 成线性
6) 与 s 的大小成线性
7) 与 first 和 last 间的距离成线性
8) 与 ilist 的大小成线性
异常
如果操作会导致 size() > max_size(),那么就会抛出 std::length_error。
4) 
noexcept 说明:  
noexcept(std::allocator_traits<Allocator>::
             propagate_on_container_move_assignment::value ||
如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。
示例
运行此代码
#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 | 非标准化注释曾说交换是移动赋值的合法实现 | 更正为要求移动赋值 | 
| LWG 2579 | C++11 | assign(const basic_string&) 不传播分配器 | 使之为若需要则传播分配器 | 
| LWG 2946 | C++17 | 重载 (9) 在某些情况下会导致歧义 | 通过使之为模板避免 | 
参阅
  构造 basic_string (公开成员函数)  | |
|    为字符串赋值   (公开成员函数)  |