• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <string>

    std::basic_string::コンストラクタ

    // デフォルトコンストラクタ
    basic_string();                                                 // (1) C++14
    basic_string() noexcept(noexcept(Allocator()))                  // (1) C++17
      : basic_string(Allocator()) {}
    constexpr basic_string() noexcept(noexcept(Allocator()))        // (1) C++20
      : basic_string(Allocator()) {}
    
    explicit basic_string(const Allocator& a);                      // (2) C++14
    explicit basic_string(const Allocator& a) noexcept;             // (2) C++17
    constexpr explicit basic_string(const Allocator& a) noexcept;   // (2) C++20
    
    explicit basic_string(const Allocator& a = Allocator());        // (1) + (2) C++03
    
    // コピーコンストラクタ
    basic_string(const basic_string& str);                          // (3) C++03
    constexpr basic_string(const basic_string& str);                // (3) C++20
    
    // ムーブコンストラクタ
    basic_string(basic_string&& str) noexcept;                      // (4) C++11
    constexpr basic_string(basic_string&& str) noexcept;            // (4) C++20
    
    // basic_stringの指定範囲から構築するコンストラクタ
    basic_string(const basic_string& str,
                 size_type pos,
                 size_type n = npos,
                 const Allocator& a = Allocator());                 // (5) C++03
    basic_string(const basic_string& str,
                 size_type pos,
                 size_type n,
                 const Allocator& a = Allocator());                 // (5) C++17
    constexpr basic_string(const basic_string& str,
                           size_type pos,
                           size_type n,
                           const Allocator& a = Allocator());       // (5) C++20
    
    basic_string(const basic_string& str,
                 size_type pos,
                 const Allocator& a = Allocator());                 // (6) C++17
    constexpr basic_string(const basic_string& str,
                           size_type pos,
                           const Allocator& a = Allocator());       // (6) C++20
    
    constexpr basic_string(basic_string&& str,
                           size_type pos,
                           const Allocator& a = Allocator());       // (17) C++23
    
    constexpr basic_string(basic_string&& str,
                           size_type pos,
                           size_type n,
                           const Allocator& a = Allocator());       // (18) C++23
    
    // 文字列ポインタから構築するコンストラクタ
    basic_string(const charT* s,
                 size_type n,
                 const Allocator& a = Allocator());                 // (7) C++03
    constexpr basic_string(const charT* s,
                           size_type n,
                           const Allocator& a = Allocator());       // (7) C++20
    
    basic_string(const charT* s,
                 const Allocator& a = Allocator());                 // (8) C++03
    constexpr basic_string(const charT* s,
                           const Allocator& a = Allocator());       // (8) C++20
    
    basic_string(nullptr_t) = delete;                               // (16) C++23
    
    // 文字個数から構築するコンストラクタ
    basic_string(size_type n,
                 charT c,
                 const Allocator& a = Allocator());                 // (9) C++03
    constexpr basic_string(size_type n,
                           charT c,
                           const Allocator& a = Allocator());       // (9) C++20
    
    // イテレータ範囲から構築するコンストラクタ
    template <class InputIterator>
    basic_string(InputIterator begin, InputIterator end,
                 const Allocator& a = Allocator());                 // (10) C++03
    template <class InputIterator>
    constexpr basic_string(InputIterator begin, InputIterator end,
                           const Allocator& a = Allocator());       // (10) C++20
    
    // 初期化子リストから構築するコンストラクタ
    basic_string(initializer_list<charT> init,
                 const Allocator& = Allocator());                   // (11) C++11
    constexpr basic_string(initializer_list<charT> init,
                           const Allocator& = Allocator());         // (11) C++20
    
    // アロケータ指定コピー/ムーブコンストラクタ
    basic_string(const basic_string& str, const Allocator&);           // (12) C++11
    constexpr basic_string(const basic_string& str, const Allocator&); // (12) C++20
    
    basic_string(basic_string&& str, const Allocator&);             // (13) C++11
    constexpr basic_string(basic_string&& str, const Allocator&);   // (13) C++20
    
    // string_viewから構築するコンストラクタ
    template<class T>
    explicit basic_string(const T& t,
                          const Allocator& a = Allocator());           // (14) C++17
    template<class T>
    constexpr explicit basic_string(const T& t,
                                    const Allocator& a = Allocator()); // (14) C++20
    
    template<class T>
    basic_string(const T& t,
                 size_type pos,
                 size_type n,
                 const Allocator& a = Allocator());                  // (15) C++17
    template<class T>
    constexpr basic_string(const T& t,
                           size_type pos,
                           size_type n,
                           const Allocator& a = Allocator());        // (15) C++20
    
    template <container-compatible-range<CharT> R>
    constexpr basic_string(from_range_t, R&& rg,
                           const Allocator& a = Allocator());        // (19) C++23
    

    概要

    • (1) : デフォルトコンストラクタ。アロケータをデフォルト構築して空のbasic_stringオブジェクトを構築する。
    • (2) : アロケータを受け取るデフォルトコンストラクタ。空のbasic_stringオブジェクトを構築する。
    • (3) : コピーコンストラクタ。strオブジェクトと同じ文字列を構築する。
    • (4) : ムーブコンストラクタ。strオブジェクトが指すデータの所有権を自身に移動する。str未規定の値になる。
    • (5) : strオブジェクトの部分文字列のコピーからbasic_stringオブジェクトを構築する。strオブジェクトのpos番目からn文字の部分文字列がコピーされる。n == nposの場合、pos番目から末尾までの部分文字列がコピーされる。
    • (6) : strオブジェクトの部分文字列のコピーからbasic_stringオブジェクトを構築する。strオブジェクトのpos番目から末尾までの部分文字列がコピーされる。
    • (7) : 文字配列sの先頭n文字からなる部分文字列のコピーからbasic_stringオブジェクトを構築する。
    • (8) : 文字配列sのコピーからbasic_stringオブジェクトを構築する。
    • (9) : 文字cn回繰り返した文字列からなるbasic_stringオブジェクトを構築する。
    • (10) : 文字列のイテレータ範囲[begin, end)からbasic_stringオブジェクトを構築する。
    • (11) : 文字の初期化子リストからbasic_stringオブジェクトを構築する。
    • (12) : アロケータを受け取るコピーコンストラクタ。
    • (13) : アロケータを受け取るムーブコンストラクタ。
    • (14) : std::basic_string_viewオブジェクトからの変換コンストラクタ。basic_string_view<charT, traits>に変換可能なtが参照する範囲の文字列を*thisにコピーする。
    • (15) : basic_string_view<charT, traits>に変換可能なtが参照する範囲の文字列のpos番目からn文字の部分文字列がコピーされる。n == nposの場合、pos番目から末尾までの部分文字列がコピーされる。
    • (17) : strオブジェクトの部分文字列のコピーからbasic_stringオブジェクトを構築する。strオブジェクトのpos番目からn文字の部分文字列がコピーされる。n == nposの場合、pos番目から末尾までの部分文字列がコピーされる。str未規定の値になる。
    • (18) : strオブジェクトの部分文字列のコピーからbasic_stringオブジェクトを構築する。strオブジェクトのpos番目から末尾までの部分文字列がコピーされる。str未規定の値になる。
    • (19) : Rangergからbasic_stringオブジェクトを構築する。

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

    要件

    • (7)
      • C++03 : sがヌルポインタではないこと。n < nposであること。
      • C++14 : sは、charT型の要素を少なくてもn個を持つ配列を指していること。
    • (8)
      • C++03 : sがヌルポインタではないこと。
      • C++14 : sは、charT型の要素を少なくてもtraits::length(s) + 1個持つ配列を指していること。

    例外

    備考

    • C++14 では、explicit basic_string(const Allocator& a = Allocator()) がデフォルト引数を使用しない 2 つのオーバーロードに分割された。
      これは、デフォルトコンストラクタに explicit が付いていると、

      std::basic_string<char> s = {};
      

      のようなコード(C++11 から導入された、コピーリスト初期化によるデフォルトコンストラクタ呼び出し)がエラーになってしまうためである。

    #include <iostream>
    #include <string>
    #include <utility>
    
    int main()
    {
      // デフォルト構築
      std::string s1;
      std::cout << "s1 : " << s1 << std::endl;
    
      // 文字配列からの構築
      std::string s2 = "hello";
      std::cout << "s2 : " << s2 << std::endl;
    
      // コピー構築
      std::string s3 = s2;
      std::cout << "s3 : " << s3 << std::endl;
    
      // ムーブ構築
      std::string s4 = std::move(s3);
      std::cout << "s4 : " << s4 << std::endl;
    
      // 部分文字列のコピーから構築
      // s4文字列オブジェクトの1番目の文字から3文字
      std::string s5(s4, 1, 3);
      std::cout << "s5 : " << s5 << std::endl;
    
      // 文字配列の先頭N文字から構築
      std::string s6("hello", 3);
      std::cout << "s6 : " << s6 << std::endl;
    
      // 文字をN回繰り返して構築
      std::string s7(3, 'a');
      std::cout << "s7 : " << s7 << std::endl;
    
      // 文字列の範囲から構築
      std::string s8(s4.begin(), s4.end());
      std::cout << "s8 : " << s8 << std::endl;
    
      // 文字の初期化子リストから構築
      std::string s9 = {'h', 'e', 'l', 'l', 'o'};
      std::cout << "s9 : " << s9 << std::endl;
    
      // string_viewからの変換
      auto sv = std::string_view{"Hello World"}.substr(0, 5);
      std::string s14 {sv};
      std::cout << "s14 : " << s14 << std::endl;
    }
    

    出力

    s1 : 
    s2 : hello
    s3 : hello
    s4 : hello
    s5 : ell
    s6 : hel
    s7 : aaa
    s8 : hello
    s9 : hello
    s14 : Hello
    

    参照