std::basic_string<CharT,Traits,Allocator>::assign
出自cppreference.com
| (1) | (C++20 起為 constexpr) |
|
| (2) | (C++11 起) (C++20 起為 constexpr) |
|
| (3) | (C++20 起為 constexpr) |
|
| (4) | (C++20 起為 constexpr) |
|
| (5) | (C++20 起為 constexpr) |
|
| (6) | (C++17 起) (C++20 起為 constexpr) |
|
| (7) | (C++17 起) (C++20 起為 constexpr) |
|
| (8) | ||
| |
(C++14 前) | |
| (C++14 起) (C++20 起為 constexpr) |
||
| (9) | (C++20 起為 constexpr) |
|
| (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 中的字符替換字符串的內容。
- 如果只提供了
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)個字符進行替換。
|
等價於 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 || 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) 在某些情況下會導致歧義 | 通過使之為模板避免 |
參閱
(C++23) |
賦值範圍內的字符到字符串 (公開成員函數) |
構造 basic_string (公開成員函數) | |
| 為字符串賦值 (公開成員函數) |