名稱空間
變體
操作

std::basic_string<CharT,Traits,Allocator>::assign

來自 cppreference.com
< cpp‎ | string‎ | basic_string
 
 
 
std::basic_string
 
basic_string& assign( const basic_string& str );
(1) (C++20 起為 constexpr)
basic_string& assign( basic_string&& str ) noexcept(/* see below */);
(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)
(8)
basic_string& assign( const basic_string& str,
                      size_type pos, size_type count );
(until 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) 用字元 chcount 份複製替換內容。
等價於 clear(); resize(n, c); return *this;
4) 用範圍 [ss + count) 中的字元複製替換內容。
[ss + count) 不是有效範圍,則行為未定義。
5) 等價於 return assign(s, Traits::length(s));
6,7) 用由 t 構造的字串檢視 sv 中的字元替換內容。
  • 若只提供了 t,則用 sv 中的所有字元替換內容。
  • 若還提供了 pos
    • countnpos,則用 sv 中從 pos 開始的所有字元替換內容。
    • 否則,用 std::min(count, sv.size() - pos) 個字元替換 sv 中從 pos 開始的字元。
僅當滿足所有以下條件時,這些過載才參與過載決議
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 中的字元替換內容。
  • countnpos,則用 str 中從 pos 開始的所有字元替換內容。
  • 否則,用 std::min(count, str.size() - pos) 個字元替換 str 中從 pos 開始的字元。
等價於 return assign(std::basic_string_view<CharT, Traits>
                  (str).substr(pos, count));
(C++20 起)
9) 等價於 return assign(basic_string(first, last, get_allocator()));

此過載只在 InputIt 滿足 LegacyInputIterator 的要求時才參與過載決議。

(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 過載 (8) 的行為在以下情況未定義
pos > str.size()true
在這種情況下總是丟擲異常
LWG 2579 C++98 過載 (1) 和複製賦值
運算子有不同的效果
它們具有相同的效果
LWG 2946 C++17 過載 (6) 在某些情況下導致歧義 透過使其成為模板來避免

[編輯] 參見

將字元範圍賦給字串
(公共成員函式) [編輯]
構造一個 basic_string
(公共成員函式) [編輯]
給字串賦值
(公共成員函式) [編輯]