std::basic_string::erase のサンプルコード

2024-04-02

C++ の Strings における std::basic_string::erase の解説

std::basic_string::erase は、C++ 標準ライブラリ std::string クラスのメンバ関数であり、文字列の一部を削除するために使用されます。この関数は、文字列の長さを短縮し、その内容を変更します。

使い方

std::basic_string::erase は、以下の3つの方法で呼び出すことができます。

位置と長さによる削除

std::string str = "This is an example sentence.";

// 位置5番目から10文字を削除
str.erase(5, 10);

// 結果: "This is an example."

この方法では、最初の引数に削除を開始する位置、2番目の引数に削除する文字数を指定します。

イテレータによる削除

std::string str = "This is an example sentence.";

// イテレータ 's' と 'e' で指定された範囲を削除
str.erase(std::find(str.begin(), str.end(), ' '), str.end());

// 結果: "This"

この方法では、最初の引数に削除する範囲の開始位置、2番目の引数に削除する範囲の終了位置を指定します。

文字へのポインタによる削除

std::string str = "This is an example sentence.";

// 文字 's' を削除
str.erase(std::find(str.begin(), str.end(), 's'));

// 結果: "Thi is an example sentence."

この方法では、最初の引数に削除する文字へのポインタを指定します。

戻り値

std::basic_string::erase は、削除された文字の最初の位置を指すイテレータを返します。

注意点

  • 削除する位置または範囲が文字列の長さを超えると、std::out_of_range 例外が発生します。
  • 削除された文字列はメモリから解放されます。

以下のコードは、std::basic_string::erase の使用方法をいくつか示しています。

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence.";

  // 位置5番目から10文字を削除
  str.erase(5, 10);
  std::cout << str << std::endl; // 出力: "This is an example."

  // イテレータ 's' と 'e' で指定された範囲を削除
  str = "This is an example sentence.";
  str.erase(std::find(str.begin(), str.end(), ' '), str.end());
  std::cout << str << std::endl; // 出力: "This"

  // 文字 's' を削除
  str = "This is an example sentence.";
  str.erase(std::find(str.begin(), str.end(), 's'));
  std::cout << str << std::endl; // 出力: "Thi is an example sentence."

  return 0;
}


std::basic_string::erase のサンプルコード

位置と長さによる削除

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence.";

  // 位置5番目から10文字を削除
  str.erase(5, 10);
  std::cout << str << std::endl; // 出力: "This is an example."

  return 0;
}

イテレータによる削除

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence.";

  // イテレータ 's' と 'e' で指定された範囲を削除
  std::string::iterator s = std::find(str.begin(), str.end(), ' ');
  std::string::iterator e = str.end();
  str.erase(s, e);
  std::cout << str << std::endl; // 出力: "This"

  return 0;
}

文字へのポインタによる削除

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence.";

  // 文字 's' を削除
  char* p = std::find(str.begin(), str.end(), 's');
  str.erase(p);
  std::cout << str << std::endl; // 出力: "Thi is an example sentence."

  return 0;
}

文字列の末尾にある空白文字を削除

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence.   ";

  // 末尾にある空白文字を削除
  str.erase(std::find_if(str.rbegin(), str.rend(), [](char c) { return !std::isspace(c); }).base(), str.end());
  std::cout << str << std::endl; // 出力: "This is an example sentence."

  return 0;
}

特定の文字列をすべて削除

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence. This is another example sentence.";

  // "This" という文字列をすべて削除
  std::string::size_type pos = 0;
  while ((pos = str.find("This", pos)) != std::string::npos) {
    str.erase(pos, 4);
  }
  std::cout << str << std::endl; // 出力: " is an example sentence. is another example sentence."

  return 0;
}


std::basic_string::erase の代替方法

std::replace 関数は、指定された文字列を別の文字列に置き換えることができます。削除したい文字列を空文字列に置き換えることで、削除することができます。

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence.";

  // 文字列 "This" を削除
  str.replace(str.find("This"), 4, "");
  std::cout << str << std::endl; // 出力: " is an example sentence."

  return 0;
}

std::copy_if 関数は、条件を満たす文字のみを別の文字列にコピーすることができます。削除したい文字を除外してコピーすることで、削除することができます。

#include <iostream>
#include <string>
#include <algorithm>

int main() {
  std::string str = "This is an example sentence.";
  std::string dst;

  // 文字 's' 以外をコピー
  std::copy_if(str.begin(), str.end(), std::back_inserter(dst), [](char c) { return c != 's'; });
  std::cout << dst << std::endl; // 出力: "Thi i an example entence."

  return 0;
}

手動で削除

文字列の長さが短く、削除する部分も明確な場合は、手動で削除することもできます。

#include <iostream>
#include <string>

int main() {
  std::string str = "This is an example sentence.";

  // 文字列 "This" を削除
  str[0] = ' ';
  str[1] = ' ';
  str[2] = ' ';
  str[3] = ' ';
  str.resize(str.size() - 4);
  std::cout << str << std::endl; // 出力: " is an example sentence."

  return 0;
}

これらの方法は、それぞれ異なる利点と欠点があります。

  • std::replace は、単純な置換に適しています。
  • std::copy_if は、複雑な条件に基づいて削除するのに適しています。
  • 手動での削除は、短く単純な文字列に適しています。

自分の目的に合った方法を選択してください。

  • std::string::substr 関数を使って、削除したい部分を除外した新しい文字列を作成することができます。
  • std::stringstream クラスを使って、文字列を操作することもできます。



C++ Stringsにおけるstd::basic_string::substr

std::basic_string::substrは以下の形式で呼び出されます。pos: 部分文字列の開始位置を指定する整数値です。len: 部分文字列の長さを指定する整数値です。例上記の例では、strの7番目から6文字分の部分文字列を取得し、substr変数に格納しています。



C++ の Strings における std::wcslen 関数の詳細解説

std::wcslen 関数の使い方std::wcslen 関数の使い方は非常に簡単です。以下のコード例のように、取得したいワイド文字列の先頭アドレスを関数に渡すだけです。std::wcslen 関数の詳細引数: str: ワイド文字列の先頭アドレス


std::char_traits::eof を使って C++ の Strings を操作する方法

文字列の終端をチェックする: std::char_traits::eof を使って、文字列がどこで終わるかを判断することができます。文字列の長さを計算する: std::char_traits::eof を使って、文字列の長さを計算することができます。


C++ Stringsで空白**以外**の最後の文字を見つける:find_last_not_of関数

この関数は、以下の2つの重要な役割を果たします。特定の文字列以外の最後の文字を見つける検索対象となる文字列をstr、検索対象となる文字列をnot_ofとします。find_last_not_of関数は、strの中でnot_ofに含まれない最後の文字の位置を返します。


C++ プログラマー必見! ワイド文字列と浮動小数点数の変換テクニック: std::wcstold 関数

std::wcstold 関数は、ワイド文字列 (wstring) から double 型の浮動小数点数を解釈し、変換するものです。ワイド文字列とは、wchar_t 型の文字列で、通常の文字列 (char) よりも広範囲の文字を表すことができます。



std::basic_string::append_range を選択する際のポイント

std::basic_string::append_rangeは、C++標準ライブラリで提供される関数で、文字列オブジェクトに別の範囲(range)の文字列を追加します。std::stringクラスだけでなく、std::wstringなど他の文字列クラスでも使用できます。


C++ Stringsにおけるstd::basic_string_view::rfind

概要std::basic_string_view::rfind は、部分文字列と検索を開始する位置を受け取り、部分文字列が最後に出現する位置を返します。部分文字列が見つからない場合は、std::basic_string_view::npos が返されます。


C++ Stringsにおけるstd::basic_string::substr

std::basic_string::substrは以下の形式で呼び出されます。pos: 部分文字列の開始位置を指定する整数値です。len: 部分文字列の長さを指定する整数値です。例上記の例では、strの7番目から6文字分の部分文字列を取得し、substr変数に格納しています。


C++で文字コード変換をマスターしよう!std::btowcの使い方とサンプルコード

この関数を使うことで、異なるエンコード間で文字列を効率的に変換したり、マルチバイト文字を扱うプログラムを作成することができます。std::btowcは以下の形式で定義されています。c: 変換する単一バイト文字std::wint_t: 変換結果のワイド文字


std::basic_string_view::find_last_of の使い方:C++で文字列の最後の出現位置を探す

引数: str: 検索対象となる文字列 pos: 検索を開始する位置(省略可能、デフォルトは文字列末尾)str: 検索対象となる文字列pos: 検索を開始する位置(省略可能、デフォルトは文字列末尾)返値: 見つかった場合は、最後の出現位置 見つからない場合は、std::basic_string_view::npos