目录

    • 方法一:使用 "==" 操作符
      • 思路
      • 代码示例
    • 方法二:使用 "compare()" 成员函数
      • 思路
      • 代码示例
    • 方法三:手动比较C风格字符数组
      • 思路
      • 代码示例
    • 如何选择?

在C++中,除了C语言标准的 “strcmp” 函数,你还有多种方法可以比较两个字符串是否相等,特别是当你使用 “std::string” 类时,操作会更加简便直观。

下面是一个快速总结不同方法的表格,方便你根据需求选择。

方法 适用对象 核心思路 主要特点
“==” 操作符 “std::string” 直接比较两个字符串对象的内容。 最常用、最简洁,代码可读性高。
“compare()” 成员函数 “std::string” 返回整数值表示比较结果(0为相等)。 可提供更多大小关系信息,支持部分比较。
手动比较字符数组 “char[]” 或 “char*” 遍历每个字符并进行对比。 不依赖标准库函数,有助于理解底层原理。

方法一:使用 “==” 操作符

这是比较两个 “std::string” 字符串最推荐的方式,因为它和比较基本数据类型(如 “int”)一样简单直观。

思路

C++的 “std::string” 类重载了 “==” 运算符。当你使用 “s1 == s2” 时,它会在底层自动完成以下步骤:

  1. 比较长度:如果两个字符串的长度不同,则直接判定为不相等。
  2. 逐字符比较:如果长度相同,则逐个比较对应位置上的字符。

代码示例

#include <iostream>
#include <string>

int main() {
    std::string str1 = "hello";
    std::string str2 = "hello";
    std::string str3 = "world";

    // 使用 == 进行比较
    if (str1 == str2) {
        std::cout << "str1 and str2 are equal." << std::endl;  // 会执行
    } else {
        std::cout << "str1 and str2 are not equal." << std::endl;
    }

    if (str1 == str3) {
        std::cout << "str1 and str3 are equal." << std::endl;
    } else {
        std::cout << "str1 and str3 are not equal." << std::endl;  // 会执行
    }

    // 也可以直接比较 string 和 C风格字符串(字符数组)
    if (str1 == "hello") {
        std::cout << "They are equal." << std::endl;  // 会执行
    }

    return 0;
}

方法二:使用 “compare()” 成员函数

“compare()” 函数提供了更详细的比较信息,不仅限于判断是否相等。

思路

“compare()” 函数返回一个整数来表示比较结果:

  • 返回 0:两个字符串相等。
  • 返回 负数:调用字符串小于参数字符串。
  • 返回 正数:调用字符串大于参数字符串。

代码示例

#include <iostream>
#include <string>

int main() {
    std::string s1 = "apple";
    std::string s2 = "apple";
    std::string s3 = "banana";

    // 基本比较
    if (s1.compare(s2) == 0) {
        std::cout << "s1 and s2 are equal." << std::endl;  // 会执行
    }

    // 比较不同字符串
    int result = s1.compare(s3);
    if (result == 0) {
        std::cout << "s1 and s3 are equal." << std::endl;
    } else if (result < 0) {
        std::cout << "s1 is less than s3." << std::endl;  // 会执行
    } else {
        std::cout << "s1 is greater than s3." << std::endl;
    }

    // 还可以进行部分比较,例如比较s3的前3个字符和"app"
    if (s3.compare(0, 3, "app", 0, 3) == 0) {
        std::cout << "The first three characters of s3 are 'app'." << std::endl;
    } else {
        std::cout << "The first three characters of s3 are not 'app'." << std::endl; // 会执行(是'ban')
    }

    return 0;
}

方法三:手动比较C风格字符数组

如果你在处理C风格的字符串(“char[]” 或
“char*”)且不能使用 “strcmp”,可以手动实现比较逻辑。

思路

  1. 遍历两个字符数组,逐个比较对应位置的字符。
  2. 如果遇到不相等的字符,根据字符的ASCII码值大小返回结果。
  3. 如果遇到字符串结束符
    “\0”,检查另一个字符串是否也同时结束。如果是,则相等;否则,较长的字符串更大。

代码示例

#include <iostream>

// 自定义字符串比较函数
int myStringCompare(const char* str1, const char* str2) {
    int i = 0;
    // 遍历两个字符串,直到遇到不同的字符或字符串结束符
    while (str1[i] != '\0' && str2[i] != '\0') {
        if (str1[i] != str2[i]) {
            // 返回当前字符的差值
            return str1[i] - str2[i];
        }
        i++;
    }
    // 检查哪个字符串先结束
    return str1[i] - str2[i];
}

int main() {
    const char* str1 = "hello";
    const char* str2 = "hello";
    const char* str3 = "hell";

    if (myStringCompare(str1, str2) == 0) {
        std::cout << "str1 and str2 are equal." << std::endl;  // 会执行
    } else {
        std::cout << "str1 and str2 are not equal." << std::endl;
    }

    int result = myStringCompare(str1, str3);
    if (result == 0) {
        std::cout << "str1 and str3 are equal." << std::endl;
    } else if (result < 0) {
        std::cout << "str1 is less than str3." << std::endl;
    } else {
        std::cout << "str1 is greater than str3." << std::endl;  // 会执行
    }

    return 0;
}

如何选择?

  • 对于 “std::string”,绝大多数情况下直接使用
    “==” 操作符是最佳选择,因为它简洁明了。

  • 如果你需要知道两个字符串的 字典序关系(即谁大谁小),而不仅仅是是否相等,那么使用
    “compare()” 函数更合适。

  • 手动比较 通常用于学习目的、特殊需求(如不区分大小写的比较需要自定义逻辑),或者在受限环境中无法使用标准库函数的情况。

希望这些讲解和代码示例能帮助你更好地理解和应用C++中的字符串比较方法。

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐