名前空間
変種
操作

append

提供: cppreference.com
< cpp‎ | string‎ | basic string
2012年5月1日 (火) 04:07時点におけるP12 (トーク | 投稿記録)による版

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)

Syntax:

    #include <string>
    string& append( const string& str );
    string& append( const charT* str );
    string& append( const string& str, size_type index, size_type len );
    string& append( const charT* str, size_type num );
    string& append( size_type num, charT ch );
    string& append( input_iterator start, input_iterator end );

append()関数は、以下のいずれかの動作をします。

  • (1,2番目) "str"を文字列の最後に追加します。
  • (3番目) "str"の"index"から始まり長さ"len"の部分文字列を文字列の最後に追加します。
  • (4番目) "str"から"num"数の文字を文字列の最後に追加します。
  • (5番目) 文字"ch"を"num"分繰り返し、文字列の最後に追加します。
  • (6番目) イテレータ"start"から"end"までを文字列の最後に追加します。

たとえば、次のコードは、文字列に"!"を10個コピーして追加するために、append()を使っています。

     string str = "Hello World";
     str.append( 10, '!' );
     cout << str << endl;

出力:

    Hello World!!!!!!!!!!

</syntaxhighlight>

次の例は、文字列と別の文字列の一部をつなぎ合わせるために、append()を使っています。

   string str1 = "Eventually I stopped caring... ";
   string str2 = "but that was the '80s so nobody noticed.";
 
   str1.append( str2, 25, 15 );
   cout << "str1 is " << str1 << endl;

実行すると次の出力になります。

<code terminal>

  str1 is Eventually I stopped caring... nobody noticed.

</syntaxhighlight>