#include <iostream>
#include <string>
using namespace std;
int main() {
string str1, str2;
// 输入两个字符串
cout << "Enter first string: ";
getline(cin, str1);
cout << "Enter second string: ";
getline(cin, str2);
// 比较两个字符串
if (str1 == str2) {
cout << "The strings are equal." << endl;
} else if (str1 < str2) {
cout << "The first string is less than the second string." << endl;
} else {
cout << "The first string is greater than the second string." << endl;
}
return 0;
}
这段代码演示了如何在 C++ 中比较两个字符串。我们使用 std::string 类型来存储用户输入的字符串,并使用关系运算符 (==, <, >) 来比较它们。
==:检查两个字符串是否相等。<:检查第一个字符串是否字典序小于第二个字符串。>:检查第一个字符串是否字典序大于第二个字符串。通过这种方式,我们可以轻松地比较两个字符串并输出相应的结果。
上一篇:c++ 函数重载
下一篇:c++ 内部类
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站