【超解説】C++のstd::basic_stringを使いこなす!サンプルコード満載


基本的な操作

  • 比較
    std::string str1 = "Hello";
    std::string str2 = "World";
    if (str1 == str2) {
        // 文字列が等しい場合
    } else if (str1 < str2) {
        // 文字列 str1 が str2 より小さい場合
    } else {
        // 文字列 str1 が str2 より大きい場合
    }
    
  • 変更
    std::string str = "This is a string";
    str[6] = 'e'; // 文字列の 6 番目の文字を変更
    str.append("!"); // 文字列の末尾に "!" を追加
    
  • アクセス
    std::string str = "This is a string";
    char ch = str[6]; // 文字列の 6 番目の文字を取得
    
  • 長さ
    std::string str = "This is a string";
    int len = str.length(); // 文字列の長さを取得
    
  • 初期化
    std::string str1 = "Hello, World!"; // 文字列リテラルで初期化
    std::string str2(10, 'c'); // 10 個の 'c' で初期化
    std::string str3(str1); // コピー初期化
    

高度な操作

  • 正規表現
    std::string str = "This is a string with numbers 123 and 456";
    std::regex re("\\d+");
    std::smatch match;
    while (std::regex_search(str, match, re)) {
        std::cout << match[0] << std::endl; // 数字を抽出
    }
    
  • 置換
    std::string str = "This is a string";
    str.replace(5, 2, "world"); // 文字列 "is" を "world" に置換
    
  • 部分文字列
    std::string str = "This is a string";
    std::string sub = str.substr(5, 4); // 文字列 "is a" を取得
    
  • 検索
    std::string str = "This is a string";
    int pos = str.find("is"); // 文字列 "is" の最初の出現位置を検索
    

利点

  • 汎用性
    文字列操作に関する様々な機能を提供しており、様々な用途に利用できます。
  • 安全性
    範囲外アクセスやメモリリークなどの問題を防ぐための機能が備わっています。
  • メモリ効率
    動的にメモリを割り当て・解放するため、メモリ使用量を効率的に管理できます。

デメリット

  • テンプレートクラスであるため、理解が難しい
    初心者にとっては、理解するのが難しい場合があります。
  • C 言語の char 配列よりもオーバーヘッドが大きい
    複雑な操作を行う場合は、パフォーマンスが低下する可能性があります。

std::basic_string は、C++ で文字列を操作するための強力なツールです。基本的な操作から高度な操作まで、様々な機能を提供しており、効率的かつ安全に文字列を処理することができます。

  • C++ で文字列を操作する場合は、std::basic_string を利用することを強くお勧めします。


文字列の初期化と基本的な操作

#include <iostream>
#include <string>

int main() {
  // 文字列リテラルで初期化
  std::string str1 = "Hello, World!";

  // 長さを取得
  int len = str1.length();
  std::cout << "文字列の長さ: " << len << std::endl;

  // 文字列の 6 番目の文字を取得
  char ch = str1[6];
  std::cout << "6 番目の文字: " << ch << std::endl;

  // 文字列の末尾に "!" を追加
  str1.append("!");
  std::cout << "追加後の文字列: " << str1 << std::endl;

  return 0;
}

文字列の比較

#include <iostream>
#include <string>

int main() {
  std::string str1 = "Hello";
  std::string str2 = "World";

  // 文字列が等しいか比較
  if (str1 == str2) {
    std::cout << "文字列は等しい" << std::endl;
  } else {
    std::cout << "文字列は等しくない" << std::endl;
  }

  // 文字列の大小を比較
  if (str1 < str2) {
    std::cout << str1 << " は " << str2 << " より小さい" << std::endl;
  } else if (str1 > str2) {
    std::cout << str1 << " は " << str2 << " より大きい" << std::endl;
  } else {
    std::cout << str1 << " と " << str2 << " は等しい" << std::endl;
  }

  return 0;
}

文字列の検索

#include <iostream>
#include <string>

int main() {
  std::string str = "This is a string";

  // 文字列 "is" の最初の出現位置を検索
  int pos = str.find("is");
  if (pos != std::string::npos) {
    std::cout << "文字列 \"is\" は " << pos << " 番目に存在する" << std::endl;
  } else {
    std::cout << "文字列 \"is\" は見つかりませんでした" << std::endl;
  }

  return 0;
}

部分文字列

#include <iostream>
#include <string>

int main() {
  std::string str = "This is a string";

  // 文字列 "is a" を取得
  std::string sub = str.substr(5, 4);
  std::cout << "部分文字列: " << sub << std::endl;

  return 0;
}

文字列の置換

#include <iostream>
#include <string>

int main() {
  std::string str = "This is a string";

  // 文字列 "is" を "world" に置換
  str.replace(5, 2, "world");
  std::cout << "置換後の文字列: " << str << std::endl;

  return 0;
}
#include <iostream>
#include <string>
#include <regex>

int main() {
  std::string str = "This is a string with numbers 123 and 456";

  // 数字を抽出
  std::regex re("\\d+");
  std::smatch match;
  while (std::regex_search(str, match, re)) {
    std::cout << match[0] << std::endl;
  }

  return 0;
}


C 言語の char 配列

  • 欠点
    • 手動でメモリ管理を行う必要がある
    • 文字列操作機能が限られている
    • 範囲外アクセスやメモリリークなどの問題が発生しやすい
    • 可読性が低くなる
  • 利点
    • 軽量で、メモリ効率が良い
    • シンプルで理解しやすい

C++ の std::array

  • 欠点
    • 可変長の文字列を扱うことができない
    • std::basic_string ほど多くの文字列操作機能が提供されていない
  • 利点
    • std::basic_string よりも軽量で、メモリ効率が良い
    • 固定長の文字列を扱う場合に適している
    • メモリ管理が自動的に行われる

C++ の std::vector<char>

  • 欠点
    • std::basic_string よりもメモリ使用量が多い場合がある
    • 複雑な操作を行う場合は、パフォーマンスが低下する可能性がある
  • 利点
    • 可変長の文字列を扱うことができる
    • メモリ管理が自動的に行われる
    • std::basic_string とほぼ同等の機能を提供している

サードパーティ製のライブラリ

  • 欠点
    • 学習コストがかかる
    • 標準ライブラリではないため、移植性が低い場合がある
  • 利点
    • std::basic_string にはない高度な機能を提供しているものがある
    • 特定のニーズに特化したライブラリを選択できる

カスタム文字列クラス

  • 欠点
    • 開発と保守に時間と労力が必要
    • 複雑でエラーが発生しやすい
  • 利点
    • アプリケーションの特定のニーズに完全に適合した文字列クラスを作成できる

どの代替方法を選択するかは、状況によって異なります。 以下のような点を考慮する必要があります。

  • 移植性
    コードを他のプラットフォームに移植する必要があるかどうか
  • 使いやすさ
    コードの理解しやすさと保守性
  • 機能性
    必要な文字列操作機能
  • パフォーマンス
    速度とメモリ使用量

一般的には、std::basic_string は、ほとんどの要件を満たすため、最初の選択肢として最適です。 しかし、上記で説明した代替手段の方が、特定の状況ではより適している場合があります。

  • 適切な代替方法を選択することは重要ですが、std::basic_string は非常に汎用性が高く強力なツールであることを忘れないでください。
  • 上記以外にも、様々な代替方法があります。