温馨提示×

c++字符串比较是否相等的方法是什么

c++
小亿
182
2024-04-01 09:43:05
栏目: 编程语言

在C++中,可以使用==运算符来比较两个字符串是否相等。例如:

#include <iostream>
#include <string>

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

    if (str1 == str2) {
        std::cout << "The strings are equal." << std::endl;
    } else {
        std::cout << "The strings are not equal." << std::endl;
    }

    return 0;
}

上面的代码中,使用==运算符来比较str1str2是否相等,如果相等则输出"The strings are equal.“,否则输出"The strings are not equal.”。

0