erase

提供:cppreference.com
移動: 案内, 検索

Syntax:

    #include <string>
    iterator erase( iterator loc );
    iterator erase( iterator start, iterator end );
    basic_string& erase( size_type index = 0, size_type num = npos );

eraase()関数は、次の動作をします。

  • "loc"の位置にある文字を削除し、削除された文字の次の文字を指すイテレータを返します。
  • "start"と"end"の間の文字をすべて削除し、その次の文字を指すイテレータを返す。(最後の1文字で無いなら)
  • "index"から"num"文字削除し、修正された文字列を返します。
 The parameters index and num have default values, which means that erase() can be called with just index to erase all characters after index or with no arguments to erase all characters.
パラメータの "index" と "num" はデフォルトの値を持っています。そのため、erase()関数を引数なしで呼び出すとすべての文字列を削除します。

例)

  string s("So, you like donuts, eh? Well, have all the donuts in the world!");
  cout << "The original string is '" << s << "'" << endl;
 
  s.erase( 50, 13 );
  cout << "Now the string is '" << s << "'" << endl;
  s.erase( 24 );
  cout << "Now the string is '" << s << "'" << endl;
  s.erase();
  cout << "Now the string is '" << s << "'" << endl;

出力

  The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'
  Now the string is 'So, you like donuts, eh? Well, have all the donuts!'
  Now the string is 'So, you like donuts, eh?'
  Now the string is ''

erase() は線形時間で実行されます。

Related Topics: insert

個人用ツール
名前空間
変種
操作
案内
ツールボックス
他の言語