C++ で文字列を比較する3つの方法

はじめに

この記事では、C++で文字列を比較する方法を学ぶことができます。

C++では、次のいずれかの技術を使用して文字列を比較することができます。

    1. 文字列のstrcmp()関数

 

    1. 組み込みのcompare()関数

 

    C++の比較演算子 (==, !=)

1. C++で文字列のstrcmp()関数を使用する。

C++の文字列は、String型のデータを操作するための組み込み関数を持っています。strcmp()関数は、2つの文字列を辞書式に比較するために使用されるCのライブラリ関数です。

strcmp()関数の構文

  • The input string has to be a char array of C-style String.
  • The strcmp() compares the strings in a case-sensitive form as well.
int strcmp(const char *str1, const char *str2);

この関数は、マッチする場合には次の値を返します。

  • Returns 0 if both the strings are the same.
  • Returns < 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.
  • Results out to be > 0 (greater than zero) when the second string is greater in comparison.

strcmp()の例1

以下のコードを実行してください。

#include <iostream>

#include <string.h>

int main()
{
    const char *str_inp1 = "String Match";
    const char *str_inp2 = "String Unmatch";

    std::cout << "String 1: " << str_inp1 << std::endl;
    std::cout << "String 2: " << str_inp2 << std::endl;

    if (strcmp(str_inp1, str_inp2) == 0)
        std::cout << "\nBoth the input strings are equal." << std::endl;
    else
        std::cout << "\nThe input strings are not equal." << std::endl;
}

以下の出力が生成されます。

Output

String 1: String Match String 2: String Unmatch The input strings are not equal.

str_inp1とstr_inp2をstrcmpすると、結果は-9となります。str_inp1とstr_inp2の値は異なります。

strcmp()の例2

以下のコードを実行してください。

#include <iostream>

#include <string.h>

int main()
{
    const char *str_inp1 = "String Match";
    const char *str_inp2 = "String Match";

    std::cout << "String 1: " << str_inp1 << std::endl;
    std::cout << "String 2: " << str_inp2 << std::endl;

    if (strcmp(str_inp1, str_inp2) == 0)
        std::cout << "\nBoth the input strings are equal." << std::endl;
    else
        std::cout << "\nThe input strings are not equal." << std::endl;
}

以下の結果が生成されます。 (Ika no kekka ga seisei sa remasu)

Output

String 1: String Match String 2: String Match Both the input strings are equal.

str_inp1とstr_inp2のstrcmp(str_inp1, str_inp2)は0を返します。str_inp1とstr_inp2の値は同じです。

2. C++のcompare()関数を使用する。

C++には、2つの文字列を比較するための組み込みのcompare()関数があります。

compare()の構文を比較する。

compare()関数は、2つの文字列を比較します。

int compare (const string& string-name) const;

この関数は、マッチングケースに応じて以下の値を返します。

  • Returns 0 if both the strings are the same.
  • Returns < 0 (less than zero) if the value of the character of the first string is smaller as compared to the second string input.
  • Results out to be > 0 (greater than zero) when the second string is greater in comparison.

例1:compare()を使用する

以下のコードを実行してください。 (Kakonomi no kōdo o jikkō shite kudasai.)

#include <iostream>

int main()
{
	std::string str_inp1("String Match");
	std::string str_inp2("String Match");

	std::cout << "String 1: " << str_inp1 << std::endl;
	std::cout << "String 2: " << str_inp2 << std::endl;

	int res = str_inp1.compare(str_inp2);

	if (res == 0)
		std::cout << "\nBoth the input strings are equal." << std::endl;
	else if (res < 0)
		std::cout << "\nString 1 is smaller as compared to String 2." << std::endl;
	else
		std::cout << "\nString 1 is greater as compared to String 2." << std::endl;
}

この例では、str_inp1とstr_inp2をcompare()で比較します。

Output

String 1: String Match String 2: String Match Both the input strings are equal.

両方の文字列は辞書順に同じですので、関数は0を返します。

例2:compare()を使用する

以下のコードを実行してください。

#include <iostream>

int main()
{
    std::string str_inp0("String Match");
    std::string str_inp1("String Match");
    std::string str_inp2("String Unmatch");

    std::cout << "String 1: " << str_inp1 << std::endl;

    if (str_inp1.compare(str_inp0) == 0)
        std::cout << "\nStrings are equal." << std::endl;
    else
        std::cout << "\nStrings are not equal." << std::endl;

    std::cout << "String 2: " << str_inp2 << std::endl;

    if (str_inp2.compare(str_inp0) == 0)
        std::cout << "\nStrings are equal." << std::endl;
    else
        std::cout << "\nStrings are not equal." << std::endl;
}

この例では、str_inp0とstr_inp1が比較されます。

Output

String 1: String Match Strings are equal.

その後、str_inp0はstr_inp2と比較されます。

Output

String 2: String Unmatch Strings are not equal.

このコードは、string型の一つの文字列をcompare()関数に直接他の入力文字列と比較しました。

3. C++における関係演算子

C++の関係演算子、例えば==(等しい)や!=(等しくない)は、文字列の比較に役立ちます。

関係演算子の構文

二つの値が等しいかどうかを確認してください。

string1 == string2

二つの値が等しくないかを確認してください。

string1 != string2

日本語でのいくつかのオプションを提示します。

例1: C++の「==」演算子の使用

以下のコードを実行してください。

#include <iostream>

int main()
{
	std::string str_inp1;
	std::string str_inp2;

	std::cout << "Enter the String 1:\n";
	std::cin >> str_inp1;
	std::cout << "Enter the String 2:\n";
	std::cin >> str_inp2;

	if (str_inp1 == str_inp2)
		std::cout << "Strings are equal" << std::endl;
	else
		std::cout << "Strings are not equal" << std::endl;
}

「文字列1」と「文字列2」の値を提供してください。

Enter the String 1:
Silicon Cloud
Enter the String 2:
digitalocean
Strings are not equal

コードは、== を使用して2つの文字列を比較します。

例2:C++の!=演算子の使用

以下のコードを実行してください。

#include <iostream>

int main()
{
	std::string str_inp1;
	std::string str_inp2;

	std::cout << "Enter the String 1:\n";
	std::cin >> str_inp1;
	std::cout << "Enter the String 2:\n";
	std::cin >> str_inp2;

	if (str_inp1 != str_inp2)
		std::cout << "Strings are not equal" << std::endl;
	else
		std::cout << "Strings are equal" << std::endl;
}

「String 1」と「String 2」に値を提供してください。

Enter the String 1:
Silicon Cloud
Enter the String 2:
Silicon Cloud
Strings are equal

コードは2つの文字列を「!=」で比較します。

結論

この記事では、C++で文字列を比較する方法を学びました。その中には、Stringのstrcmp()関数、組み込みのcompare()関数、および関係演算子(==、!=)が含まれていました。

もっとC++のチュートリアルで学習を続けてください。

コメントを残す 0

Your email address will not be published. Required fields are marked *