C++ string比较
时间: 2023-11-18 08:05:09 浏览: 133
C++中可以使用`==`运算符来比较两个字符串是否相等,例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
string str3 = "hello";
if (str1 == str2) {
cout << "str1 is equal to str2" << endl;
} else {
cout << "str1 is not equal to str2" << endl;
}
if (str1 == str3) {
cout << "str1 is equal to str3" << endl;
} else {
cout << "str1 is not equal to str3" << endl;
}
return 0;
}
```
输出结果为:
```
str1 is not equal to str2
str1 is equal to str3
```
注意,C++中的字符串比较是区分大小写的。
阅读全文