C++字符串比较:三种方法详细解析与性能对比

介绍

在这篇文章中,你将学习如何在C++中比较字符串的方法。

在C++中,可以使用以下技术来比较字符串:

  1. 使用字符串的比较运算符进行比较。
  2. 使用字符串的成员函数compare()进行比较。
  3. 使用库函数strcmp()进行比较。
  4. 使用库函数strcoll()进行比较。(可进行本地化的字符串比较)

字符串比较方法概览

  • strcmp()函数
  • 内置的compare()函数
  • C++关系运算符(==,!=)

在C++中使用字符串strcmp()函数

C++字符串具有系统内置的操作字符串数据的函数。strcmp()函数是一个C库函数,用于按字典顺序比较两个字符串。

strcmp() 语法

  • 输入字符串必须是C风格字符串的字符数组。
  • strcmp()函数也会以区分大小写的形式比较字符串。
int strcmp(const char *str1, const char *str2);

根据匹配的情况,此函数返回以下值。

  • 如果两个字符串相同,则返回0。
  • 如果第一个字符串的字符值小于第二个字符串的输入,则返回< 0(小于零)。
  • 当第二个字符串比较大时,结果为> 0(大于零)。

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 << "\n两个输入字符串是相等的。" << std::endl;    else        std::cout << "\n输入字符串不相等。" << std::endl;}

这将会产生以下的输出结果。

输出
String 1: String Match
String 2: String Unmatch
输入字符串不相等。

strcmp(str_inp1, str_inp2)的结果是-9。str_inp1和str_inp2的值是不同的。

strcmp() 示例2

strcmp()函数用于比较两个字符串。下面是strcmp()的示例用法:

“`c++
#include
#include

int main() {
char str1[15], str2[15];
int ret;

strcpy(str1, “abcd”);
strcpy(str2, “wxyz”);

ret = strcmp(str1, str2);

if(ret < 0) { printf(“str1 小于 str2”); } else if(ret > 0) {
printf(“str1 大于 str2”);
} else {
printf(“str1 等于 str2”);
}

return 0;
}
“`

该代码段通过strcmp()函数比较了两个字符串str1和str2。如果str1小于str2,则输出“str1 小于 str2”,如果str1大于str2,则输出“str1 大于 str2”,如果两个字符串相等,则输出“str1 等于 str2”。

运行以下代码:

#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;
}

这将产生以下的输出结果:

Output

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

strcmp(str_inp1, str_inp2) 的结果是0。str_inp1 和 str_inp2 的值相同。

2. 在C++中使用compare()函数。C++ 中自带了一个 compare() 函数用于比较两个字符串。

比较(Syntax)函数的语法compare() 函数用于比较两个字符串。

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()函数
这是文章《在C++中比较字符串的三种方式》的第3部分(共6部分)。

内容片段: 在中文中,”Using compare()函数”可以翻译为 “使用compare()函数”。

运行以下代码:

#include <iostream>

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

	std::cout << "字符串1: " << str_inp1 << std::endl;
	std::cout << "字符串2: " << str_inp2 << std::endl;

	int res = str_inp1.compare(str_inp2);

	if (res == 0)
		std::cout << "\n两个输入字符串相等。" << std::endl;
	else if (res < 0)
		std::cout << "\n字符串1比字符串2小。" << std::endl;
	else
		std::cout << "\n字符串1比字符串2大。" << std::endl;
}

在这个例子中,使用compare()函数对str_inp1和str_inp2进行比较。

输出
字符串1: String Match
字符串2: String Match

两个输入字符串相等。

由于两个字符串在字典上是相同的,因此该函数返回0。

例子2:使用compare()

使用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 << "字符串1: " << str_inp1 << std::endl;

    if (str_inp1.compare(str_inp0) == 0)
        std::cout << "\n字符串相等。" << std::endl;
    else
        std::cout << "\n字符串不相等。" << std::endl;

    std::cout << "字符串2: " << str_inp2 << std::endl;

    if (str_inp2.compare(str_inp0) == 0)
        std::cout << "\n字符串相等。" << std::endl;
    else
        std::cout << "\n字符串不相等。" << std::endl;
}

在这个例子中,将str_inp0与str_inp1进行了比较。

输出结果
字符串1: String Match
字符串相等。

然后,将str_inp0与str_inp2进行比较。

输出结果
字符串2: String Unmatch
字符串不相等。

这段代码直接将一个字符串与另一个输入字符串进行比较,用于compare()函数。

3. C++中的关系运算符

C++ 中的关系运算符比如 == (等于)和 != (不等于)在比较字符串时可以很有帮助。

关系运算符语法

检查两个字符串是否相等

使用等于运算符(==)可以检查两个字符串是否相等:

string1 == string2

使用不等于运算符(!=)可以检查两个字符串是否不相等:

string1 != string2

示例1:使用C++中的”==”运算符

以下代码演示了如何使用等于运算符比较两个字符串:

#include <iostream>

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

	std::cout << "请输入字符串1:\n";
	std::cin >> str_inp1;
	std::cout << "请输入字符串2:\n";
	std::cin >> str_inp2;

	if (str_inp1 == str_inp2)
		std::cout << "两个字符串相等" << std::endl;
	else
		std::cout << "两个字符串不相等" << std::endl;
}

程序运行示例:

请输入字符串1:
Silicon Cloud
请输入字符串2:
digitalocean
两个字符串不相等

这段代码使用”==”运算符比较两个字符串的内容是否完全相同。

示例2:使用C++中的不等于运算符

使用关系运算符比较字符串

运行以下代码:

#include <iostream>

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

	std::cout << "输入字符串1:\n";
	std::cin >> str_inp1;
	std::cout << "输入字符串2:\n";
	std::cin >> str_inp2;

	if (str_inp1 != str_inp2)
		std::cout << "字符串不相等" << std::endl;
	else
		std::cout << "字符串相等" << std::endl;
}

提供”字符串1″和”字符串2″的值:

输入字符串1:
Silicon Cloud
输入字符串2:
Silicon Cloud
字符串相等

代码将使用!=运算符来比较两个字符串。

结论

在本文中,你学会了在C++中比较字符串的三种方法。其中包括C风格字符串的strcmp()函数、string类的compare()函数和关系运算符(==、!=)。

继续通过更多的C++教程来深入学习。

bannerAds