GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::basic_string::assign - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. string
  3. basic_string
  4. assign

最終更新日時: 2026年10月03日 22時31分04秒 (JST)
Akira Takahashi が更新

履歴 編集

function
<string>

std::basic_string::assign

basic_string& assign(const basic_string& str);                  // (1) C++98
constexpr basic_string& assign(const basic_string& str);        // (1) C++20

basic_string& assign(basic_string&& str) noexcept;              // (2) C++11
constexpr basic_string& assign(basic_string&& str) noexcept;    // (2) C++20

basic_string&
  assign(const basic_string& str,
         size_type pos,
         size_type n);             // (3) C++98
basic_string&
  assign(const basic_string& str,
         size_type pos,
         size_type n = npos);      // (3) C++14
constexpr basic_string&
  assign(const basic_string& str,
         size_type pos,
         size_type n = npos);      // (3) C++20

basic_string& assign(const charT* s, size_type n);              // (4) C++98
constexpr basic_string& assign(const charT* s, size_type n);    // (4) C++20

basic_string& assign(const charT* s);                           // (5) C++98
constexpr basic_string& assign(const charT* s);                 // (5) C++20

basic_string& assign(size_type n, charT c);                     // (6) C++98
constexpr basic_string& assign(size_type n, charT c);           // (6) C++20

template <class InputIterator>
basic_string&
  assign(InputIterator first,
         InputIterator last);  // (7) C++98
template <class InputIterator>
constexpr basic_string&
  assign(InputIterator first,
         InputIterator last);  // (7) C++20

basic_string& assign(initializer_list<charT> il);           // (8) C++11
constexpr basic_string& assign(initializer_list<charT> il); // (8) C++20

// string_viewを引数に取るオーバーロード
template<class T>
basic_string& assign(const T& t);           // (9) C++17
template<class T>
constexpr basic_string& assign(const T& t); // (9) C++20

template<class T>
basic_string&
  assign(const T& t,
         size_type pos,
         size_type n = npos);   // (10) C++17
template<class T>
constexpr basic_string&
  assign(const T& t,
         size_type pos,
         size_type n = npos);   // (10) C++20

constexpr basic_string&
  assign(const charT* s,
         size_type pos,
         size_type n);          // (11) C++26

概要

文字列の再代入を行う。

この関数は、アロケータを除き、basic_stringクラスのコンストラクタと同様のパラメータを受け取り、再代入を行う。代入演算子が一つのパラメータしか扱えないため、複数パラメータによる代入として使用する。

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

  • (9), (10) : 以下の両方を満たしていること
    • is_convertible_v<const T&, basic_string_view<charT, traits>> == true
    • is_convertible_v<const T&, const charT*> == false

要件

  • (3) : pos <= str.size()であること。
  • (4) : s は少なくとも n の長さを持つ charT 型の配列を指していること。
  • (5) : s は少なくとも traits_type::length(s) + 1 の長さを持つ charT 型の配列を指していること。

効果

  • (1) : コピー代入。strオブジェクトと同じ文字列を構築する。
    • C++98 : assign(str, 0, npos)と等価
    • C++17 : *this = strと等価
      • アロケータの伝播(propagate_on_container_copy_assignment)について、コピー代入演算子と一貫した扱いとなる
  • (2) : ムーブ代入。strオブジェクトが指すデータの所有権を自身に移動する。strは未規定の値になる。
  • (3) : strオブジェクトの部分文字列のコピーから構築する。strオブジェクトのpos番目からn文字の部分文字列がコピーされる。
    • 文字列の長さ rlen は、n と str.size() - pos の小さい方である。 n == npos の場合は、 str.size() - pos が使用される。
    • assign(str.data() + pos, rlen)を呼び出す。
  • (4) : 文字配列sの先頭n文字からなる部分文字列のコピーから構築する。
  • (5) : 文字配列sのコピーから構築する。
    • assign(s, traits::length(s))を呼び出す。
  • (6) : 文字cのn回繰り返した文字列からなるbasic_stringオブジェクトを構築する。
    • assign(basic_string(n, c))と等価。
  • (7) : 文字列のイテレータ範囲[begin, end)からbasic_stringオブジェクトを構築する。
    • assign(basic_string(first, last))と等価。
  • (8) : 文字の初期化子リストからbasic_stringオブジェクトを構築する。
    • assign(il.begin(), il.end())を呼び出す。
  • (9) : basic_string_view<charT, traits>に変換可能なtが参照する範囲をコピーして、basic_stringオブジェクトを構築する。
    以下と等価。

    basic_string_view<charT, traits> sv = t;
    return assign(sv.data(), sv.size());
    

  • (10) : basic_string_view<charT, traits>に変換可能なtが参照する文字列を範囲指定でコピーして、basic_stringオブジェクトを構築する。
    以下と等価。

    basic_string_view<charT, traits> sv = t;
    return assign(sv.substr(pos, n));
    

  • (11) : s が指すNULL終端された文字列の pos 番目から n 文字をコピーして、basic_stringオブジェクトを構築する。
    以下と等価。

    return assign(basic_string_view<charT, traits>(s).substr(pos, n));
    

戻り値

*this

例外

  • (3) : pos > str.size()である場合、out_of_range例外を送出する
  • (4) : n > max_size()である場合、length_error例外を送出する
  • (10) : pos > sv.size()である場合、out_of_range例外を送出する
  • (11) : pos が s の長さ (traits_type::length(s)) を超える場合、out_of_range例外を送出する

例

#include <iostream>
#include <string>
#include <string_view>

int main()
{
  // (1) コピー代入
  std::string s1;
  std::string s1_tmp = "hello";
  s1.assign(s1_tmp);
  std::cout << "s1 : " << s1 << std::endl;

  // (2) ムーブ代入
  std::string s2;
  s2.assign(std::string("hello"));
  std::cout << "s2 : " << s2 << std::endl;

  // (3) 部分文字列のコピーを代入
  // s2文字列オブジェクトの1番目の文字から3文字
  std::string s3;
  s3.assign(s2, 1, 3);
  std::cout << "s3 : " << s3 << std::endl;

  // (4) 文字配列の先頭N文字を代入
  std::string s4;
  s4.assign("hello", 3);
  std::cout << "s4 : " << s4 << std::endl;

  // (5) 文字配列を代入
  std::string s5;
  s5.assign("hello");
  std::cout << "s5 : " << s5 << std::endl;

  // (6) 文字をN回繰り返して代入
  std::string s6;
  s6.assign(3, 'a');
  std::cout << "s6 : " << s6 << std::endl;

  // (7) 文字列の範囲を代入
  std::string s7;
  s7.assign(s1.begin(), s1.end());
  std::cout << "s7 : " << s7 << std::endl;

  // (8) 文字の初期化子リストを代入
  std::string s8;
  s8.assign({'h', 'e', 'l', 'l', 'o'});
  std::cout << "s8 : " << s8 << std::endl;

  // (9) std::basic_string_viewオブジェクトを代入
  std::string s9;
  s9.assign(std::string_view{"Hello World"}.substr(0, 5));
  std::cout << "s9 : " << s9 << std::endl;

  // (10) std::basic_string_viewオブジェクトを範囲指定して代入
  std::string s10;
  s10.assign(std::string_view{"Hello World"}, 0, 5);
  std::cout << "s10 : " << s10 << std::endl;

  // (11) 文字配列を範囲指定して代入
  std::string s11;
  s11.assign("Hello World", 0, 5);
  std::cout << "s11 : " << s11 << std::endl;
}

出力

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

参照

  • N2679 Initializer Lists for Standard Containers(Revision 1)
    • (7)の経緯となる提案文書
  • LWG ISsue 2268. Setting a default argument in the declaration of a member function assign of std::basic_string
    • C++14から(3)のオーバーロードに、n = nposのデフォルト引数を追加。
  • P0254R2 Integrating std::string_view and std::string
  • LWG Issue 2579. Inconsistency wrt Allocators in basic_string assignment vs. basic_string::assign
    • C++17で、(1)の効果を*this = strと等価に変更し、アロケータ伝播についてコピー代入演算子と一貫させた
  • LWG Issue 2758. std::string{}.assign("ABCDE", 0, 1) is ambiguous
  • LWG Issue 2946. LWG 2758's resolution missed further corrections
    • C++20で、意図しない暗黙変換防止のためにstring_viewを受けるオーバーロード(9), (10)の引数型をconst T&に変更
  • P0980R1 Making std::string constexpr
  • LWG Issue 3662 basic_string::append/assign(NTBS, pos, n) suboptimal
    • C++26で、NULL終端文字列の一部分を代入する(11)のオーバーロードが追加された
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.