std::basic_string<CharT,Traits,Allocator>::assign
来自cppreference.com
< cpp | string | basic string
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, | (7) | (C++17 起) (C++20 起为 constexpr ) |
(8) | ||
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 ) | |
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)
中的字符的副本替换字符串的内容。5) 等价于 return assign(s, Traits::length(s));。
6,7) 以从 t 构造的字符串视图 sv 中的字符替换字符串的内容。
这些重载只有在满足以下所有条件时才会参与重载决议:
- 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());。
return assign(sv.data(), sv.size());。
7) 等价于 std::basic_string_view<CharT, Traits> sv = t;
return assign(sv.substr(pos, count));。
return assign(sv.substr(pos, count));。
8) 以 str 中的字符替换字符串的内容。
等价于 return assign(std::basic_string_view<CharT, Traits> (str).substr(pos, count));。 | (C++20 起) |
9) 等价于 return assign(basic_string(first, last, get_allocator()));。
此重载只有在 | (C++11 起) |
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 说明:
noexcept(std::allocator_traits<Allocator>::
propagate_on_container_move_assignment::value||
如果操作会导致 size()
超出 max_size()
,那么就会抛出 std::length_error。
如果因为任何原因抛出了异常,那么此函数无效果(强异常安全保证)。
[编辑]示例
运行此代码
#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::stringconst 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) 在某些情况下会导致歧义 | 通过使之为模板避免 |
[编辑]参阅
(C++23) | 赋值范围内的字符到字符串 (公开成员函数) |
构造 basic_string (公开成员函数) | |
为字符串赋值 (公开成员函数) |