string类型比较
时间: 2023-07-23 22:21:39 浏览: 42
在C++中,可以使用比较运算符 `==`、`!=`、`<`、`>`、`<=`、`>=` 来进行string类型的比较。
示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
string str3 = "hello";
if (str1 == str2) {
cout << "str1 equals to str2" << endl;
} else {
cout << "str1 does not equal to str2" << endl;
}
if (str1 == str3) {
cout << "str1 equals to str3" << endl;
} else {
cout << "str1 does not equal to str3" << endl;
}
if (str1 < str2) {
cout << "str1 is less than str2" << endl;
} else {
cout << "str1 is not less than str2" << endl;
}
return 0;
}
```
输出:
```
str1 does not equal to str2
str1 equals to str3
str1 is less than str2
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)