C++ 判断两个string相同内容
时间: 2023-08-21 11:05:07 浏览: 171
详解C++ string字符串类
在C++中,可以使用`==`运算符来判断两个`string`对象是否相等,例如:
```cpp
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "hello";
string s2 = "world";
string s3 = "hello";
if (s1 == s2) {
cout << "s1 and s2 are equal" << endl;
} else {
cout << "s1 and s2 are not equal" << endl;
}
if (s1 == s3) {
cout << "s1 and s3 are equal" << endl;
} else {
cout << "s1 and s3 are not equal" << endl;
}
return 0;
}
```
输出结果为:
```
s1 and s2 are not equal
s1 and s3 are equal
```
在上面的代码中,我们定义了三个`string`对象`s1`、`s2`和`s3`,并使用`==`运算符来比较它们的内容是否相等。当`s1`和`s2`的内容不同时,输出`s1 and s2 are not equal`,当`s1`和`s3`的内容相同时,输出`s1 and s3 are equal`。
阅读全文