C++の標準ライブラリで文字列を比較する:std::basic_string::compareの解説


std::basic_string::compare は、C++ の標準ライブラリに含まれる std::basic_string クラスのメンバー関数であり、2 つの文字列を比較するために使用されます。比較結果は、負の値、0、正の値のいずれかになります。

  • 正の値
    最初の文字列が2番目の文字列よりも大きいことを示します。
  • 0
    2つの文字列が等しいことを示します。
  • 負の値
    最初の文字列が2番目の文字列よりも小さいことを示します。

構文

int std::basic_string::compare(
    const basic_string& str,
    size_t pos = 0,
    size_t len = npos,
    const Compare cmp_op = std::string::compare_char
) const;

パラメータ

  • cmp_op: 文字列比較のアルゴリズム (デフォルトは std::string::compare_char)
  • len: 比較する文字列の長さ (デフォルトは npos、つまり文字列の最後まで)
  • pos: 比較を開始する文字列内の位置 (デフォルトは0)
  • str: 比較対象の文字列

戻り値

比較結果を表す整数值

#include <iostream>
#include <string>

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

  // str1 と str2 を比較
  int result = str1.compare(str2);

  if (result < 0) {
    std::cout << str1 << " は " << str2 << " より小さい" << std::endl;
  } else if (result == 0) {
    std::cout << str1 << " は " << str2 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " は " << str2 << " より大きい" << std::endl;
  }

  // 文字列の先頭5文字のみを比較
  result = str1.compare(str2, 0, 5);

  if (result < 0) {
    std::cout << str1 << " の先頭5文字は " << str2 << " より小さい" << std::endl;
  } else if (result == 0) {
    std::cout << str1 << " の先頭5文字は " << str2 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " の先頭5文字は " << str2 << " より大きい" << std::endl;
  }

  return 0;
}

この例では、str1str2 を比較し、結果を出力します。また、str1 の先頭5文字のみを str2 と比較します。

  • std::basic_string::compare は、3 値比較と呼ばれる比較方法を提供します。これは、2 つの文字列が等しいかどうかだけでなく、どちらが大きいか小さいかを判断できることを意味します。3 値比較は、多くの場合、文字列の順序付けや検索などのタスクで役立ちます。
  • 文字列比較のアルゴリズムは、cmp_op パラメータを使用して指定できます。デフォルトのアルゴリズムは std::string::compare_char であり、文字列をバイト単位で比較します。他のアルゴリズムとしては、std::string::compare_locale (ロケールに基づいた比較)、std::string::compare_case_insensitive (大文字小文字を区別しない比較) などがあります。
  • std::basic_string::compare は、文字列の長さを比較してから、個々の文字を比較します。
  • [std::string::compare_case_insensitive](https://en.cppreference


#include <iostream>
#include <string>

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

  // str1 と str2 を比較
  int result = str1.compare(str2);

  if (result < 0) {
    std::cout << str1 << " は " << str2 << " より小さい" << std::endl;
  } else if (result == 0) {
    std::cout << str1 << " は " << str2 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " は " << str2 << " より大きい" << std::endl;
  }

  // str1 と str3 を比較
  result = str1.compare(str3);

  if (result < 0) {
    std::cout << str1 << " は " << str3 << " より小さい" << std::endl;
  } else if (result == 0) {
    std::cout << str1 << " は " << str3 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " は " << str3 << " より大きい" << std::endl;
  }

  return 0;
}

このコードでは、str1str2 を比較し、str1str3 を比較します。結果はコンソールに出力されます。

文字列の先頭部分のみを比較

#include <iostream>
#include <string>

int main() {
  std::string str1 = "Hello, World!";
  std::string str2 = "Hi, There!";

  // str1 と str2 の先頭5文字のみを比較
  int result = str1.compare(str2, 0, 5);

  if (result < 0) {
    std::cout << str1 << " の先頭5文字は " << str2 << " より小さい" << std::endl;
  } else if (result == 0) {
    std::cout << str1 << " の先頭5文字は " << str2 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " の先頭5文字は " << str2 << " より大きい" << std::endl;
  }

  return 0;
}

このコードでは、str1str2 の先頭5文字のみを比較します。結果はコンソールに出力されます。

ロケールに基づいた文字列比較

#include <iostream>
#include <string>
#include <locale>

int main() {
  std::string str1 = "こんにちは";
  std::string str2 = "さようなら";

  // ロケールに基づいて str1 と str2 を比較
  std::locale loc("ja_JP.UTF-8");
  int result = str1.compare(str2, loc);

  if (result < 0) {
    std::cout << str1 << " は " << str2 << " より小さい" << std::endl;
  } else if (result == 0) {
    std::cout << str1 << " は " << str2 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " は " << str2 << " より大きい" << std::endl;
  }

  return 0;
}

このコードでは、std::locale クラスを使用してロケールを設定し、ロケールに基づいて str1str2 を比較します。結果はコンソールに出力されます。

#include <iostream>
#include <string>
#include <locale>

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

  // 大文字小文字を区別せずに str1 と str2 を比較
  std::locale loc(std::locale::C);
  int result = str1.compare


文字列演算子

C++ には、文字列を比較するためのいくつかの演算子が用意されています。

  • !=: 2 つの文字列が等しくないかどうかを比較します。
  • ==: 2 つの文字列が等しいかどうかを比較します。
  • >=: 左側の文字列が右側の文字列よりも大きいか、または等しいかどうかを比較します。
  • >: 左側の文字列が右側の文字列よりも大きいかどうかを比較します。
  • <=: 左側の文字列が右側の文字列よりも小さいか、または等しいかどうかを比較します。
  • <: 左側の文字列が右側の文字列よりも小さいかどうかを比較します。

長所

  • 多くの場合、std::basic_string::compare よりも効率的
  • シンプルで分かりやすい

短所

  • ロケールに基づいた比較や、大文字小文字を区別しない比較などの高度な機能を提供しない
  • 3 値比較を提供しない


#include <iostream>
#include <string>

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

  // str1 と str2 を比較
  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;
}

手動ループ

2 つの文字列を文字ずつ比較することで、手動で比較することもできます。

長所

  • 必要な比較処理のみを実行できる
  • 柔軟性が高い

短所

  • 多くの場合、std::basic_string::compare よりも非効率的
  • 冗長でコードが長くなる


#include <iostream>
#include <string>

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

  // str1 と str2 を比較
  bool equal = true;
  for (size_t i = 0; i < str1.size() && i < str2.size(); ++i) {
    if (str1[i] != str2[i]) {
      equal = false;
      break;
    }
  }

  if (equal) {
    std::cout << str1 << " は " << str2 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " は " << str2 << " と等しくない" << std::endl;
  }

  return 0;
}

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

Boost C++ Libraries や Range-v3 などのサードパーティのライブラリには、文字列比較用の追加機能が提供されている場合があります。

長所

  • コードを簡潔にすることができる
  • 標準ライブラリよりも高度な機能を提供する場合がある

短所

  • ライブラリのインストールと学習が必要
#include <iostream>
#include <string>
#include <boost/range/algorithm.hpp>

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

  // str1 と str2 を比較
  bool equal = boost::equals(str1.begin(), str1.end(), str2.begin(), str2.end());

  if (equal) {
    std::cout << str1 << " は " << str2 << " と等しい" << std::endl;
  } else {
    std::cout << str1 << " は " << str2 << " と等しくない" << std::endl;
  }

  return 0;
}