std::basic_string<CharT,Traits,Allocator>:: assign
|
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) |
替换字符串的内容。
[
s
,
s
+
count
)
中字符的副本。
- std:: is_convertible_v < const SV & , std:: basic_string_view < CharT, Traits >> 为 true 。
- std:: is_convertible_v < const SV & , const CharT * > 为 false 。
return assign ( sv. data ( ) , sv. size ( ) ) ; 。
return assign ( sv. substr ( pos, count ) ) ; 。
|
等价于
return
assign
(
std::
basic_string_view
<
CharT, Traits
>
( str ) . substr ( pos, count ) ) ; 。 |
(C++20 起) |
|
此重载仅在
|
(C++11 起) |
目录 |
参数
| str | - | 用作初始化字符来源的字符串 |
| count | - | 结果字符串的大小 |
| ch | - | 用于初始化字符串字符的值 |
| s | - | 用作字符串初始化来源的字符字符串指针 |
| t | - | 用于初始化字符串字符的对象(可转换为 std::basic_string_view ) |
| pos | - | 起始字符的索引位置 |
| first, last | - | 复制字符的源范围 |
| ilist | - | 用于初始化字符串字符的 std::initializer_list |
返回值
* this
异常
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::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 ) 在某些情况下引发歧义 | 通过模板化避免歧义 |
另请参阅
|
(C++23)
|
将字符范围赋值给字符串
(公开成员函数) |
构造
basic_string
(公开成员函数) |
|
|
为字符串赋值
(公开成员函数) |