• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <string>

    std::basic_string::operator=

    basic_string& operator=(const basic_string& str);                  // (1) C++03
    constexpr basic_string& operator=(const basic_string& str);        // (1) C++20
    
    basic_string& operator=(basic_string&& str) noexcept;              // (2) C++11
    basic_string& operator=(basic_string&& str) noexcept
      (allocator_traits<Allocator>::propagate_on_container_move_assignment::value
        || allocator_traits<Allocator>::is_always_equal::value);       // (2) C++17
    constexpr basic_string& operator=(basic_string&& str) noexcept
      (allocator_traits<Allocator>::propagate_on_container_move_assignment::value
        || allocator_traits<Allocator>::is_always_equal::value);       // (2) C++20
    
    basic_string& operator=(const charT* s);                           // (3) C++03
    constexpr basic_string& operator=(const charT* s);                 // (3) C++20
    
    basic_string& operator=(charT c);                                  // (4) C++03
    constexpr basic_string& operator=(charT c);                        // (4) C++20
    
    basic_string& operator=(initializer_list<charT> il);               // (5) C++11
    constexpr basic_string& operator=(initializer_list<charT> il);     // (5) C++20
    
    // string_viewを引数に取るオーバーロード
    template<class T>
    basic_string& operator=(const T& t);                               // (6) C++17
    template<class T>
    constexpr basic_string& operator=(const T& t);                     // (6) C++20
    
    basic_string& operator=(nullptr_t) = delete;                       // (7) C++23
    

    概要

    • (1) : str*this へコピーする。*thisstr が同一オブジェクトである場合は何も行わない。
    • (2) : str から *this へデータの所有権を移動する。*thisstr が同一オブジェクトである場合は何も行わない。
    • (3) : *this = basic_string(s); と等価。
    • (4) : *this = basic_string(1, c); と等価。
    • (5) : *this = basic_string(il); と等価。
    • (6) : std::basic_string_viewオブジェクトからの変換。以下と等価。
      basic_string_view<charT, traits> sv = t;
      return assign(sv);
      

    テンプレートパラメータ制約

    効果

    コピーを行った場合と、ムーブ代入を行った場合で効果が異なる

    メンバ関数 コピーの場合 ムーブ代入の場合
    data() str.data() をコピーした領域の先頭ポインタ str.data()
    size() str.size() と同じ値 str.size()と同じ値
    capacity() size() 以上の値 size() 以上の値

    戻り値

    *this

    例外

    ムーブ代入の場合は例外を投げない。

    #include <iostream>
    #include <string>
    
    int main()
    {
      std::string s;
    
      // (1) basic_string左辺値の代入
      {
        std::string r = "hello";
        s = r;
      }
    
      // (2) basic_string右辺値の代入
      {
        s = std::string("hello");
      }
    
      // (3) 文字配列の代入
      {
        s = "hello";
      }
    
      // (4) 文字の代入
      {
        s = 'a';
      }
    
      // (5) 文字の初期化子リストを代入
      {
        s = {'h', 'e', 'l', 'l', 'o'};
      }
    
      // (6) std::basic_string_viewを代入
      {
        s = std::string_view{"Hello World"}.substr(0, 5);
      }
    
      std::cout << s << std::endl;
    }
    

    出力

    Hello
    

    参照