c++ string 对比字符串是否相等
时间: 2023-07-21 13:30:18 浏览: 246
在 C++ 中,可以使用 `==` 运算符来比较两个字符串是否相等。例如:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "hello";
string str2 = "world";
if (str1 == str2) {
cout << "str1 and str2 are equal" << endl;
} else {
cout << "str1 and str2 are not equal" << endl;
}
return 0;
}
```
输出结果为:
```
str1 and str2 are not equal
```
相关问题
c++ string判断字符串相等
### C++ 中字符串比较方法
在 C++ 中,`std::string` 类提供了多种方式来检查两个字符串是否相等。以下是几种常用的方法:
#### 使用 `==` 运算符
最简单的方式是使用内置的 `==` 运算符来进行字符串比较。
```cpp
#include <iostream>
#include <string>
int main() {
std::string str1 = "hello";
std::string str2 = "world";
if (str1 == str2) {
std::cout << "Strings are equal." << std::endl;
} else {
std::cout << "Strings are not equal." << std::endl;
}
return 0;
}
```
此方法会逐字符对比两个字符串的内容并返回布尔值[^1]。
#### 使用成员函数 compare()
另一种更为灵活的选择是调用 `compare()` 成员函数。该函数可以接受另一个 `std::string` 对象作为参数,并且支持更复杂的匹配逻辑,比如指定子串范围内的比较。
```cpp
#include <iostream>
#include <string>
int main() {
std::string str1 = "hello";
std::string str2 = "hello";
int result = str1.compare(str2);
if (result == 0) {
std::cout << "Strings are equal." << std::endl;
} else {
std::cout << "Strings are not equal." << std::endl;
}
return 0;
}
```
当 `compare()` 返回零时表示两字符串完全相同;如果小于零则表示当前对象字典序较小;大于零反之亦然[^2]。
对于常量迭代器如 `cbegin()` 和 `cend()` 的定义,在容器类内部实现上确实通过委托给对应的非 const 版本来简化代码维护工作,但这与字符串直接比较无关。
c++ string 和字符串比较
C++中的string是一个类,用于表示和操作字符串。它提供了许多方便的方法来处理字符串,比如连接、截取、查找等。
字符串比较是指判断两个字符串是否相等或者大小关系的操作。在C++中,可以使用string类提供的比较函数来进行字符串比较,常用的有以下几种方式:
1. 使用"=="运算符:可以使用"=="运算符来判断两个字符串是否相等。例如:
```cpp
string str1 = "hello";
string str2 = "world";
if (str1 == str2) {
cout << "str1和str2相等" << endl;
} else {
cout << "str1和str2不相等" << endl;
}
```
2. 使用compare()函数:string类提供了compare()函数来比较两个字符串的大小关系。该函数返回一个整数值,表示两个字符串的大小关系。如果返回值为0,则表示两个字符串相等;如果返回值小于0,则表示第一个字符串小于第二个字符串;如果返回值大于0,则表示第一个字符串大于第二个字符串。例如:
```cpp
string str1 = "hello";
string str2 = "world";
int result = str1.compare(str2);
if (result == 0) {
cout << "str1和str2相等" << endl;
} else if (result < 0) {
cout << "str1小于str2" << endl;
} else {
cout << "str1大于str2" << endl;
}
```
3. 使用<、>、<=、>=运算符:除了使用compare()函数外,也可以直接使用<、>、<=、>=运算符来比较两个字符串的大小关系。例如:
```cpp
string str1 = "hello";
string str2 = "world";
if (str1 < str2) {
cout << "str1小于str2" << endl;
} else if (str1 > str2) {
cout << "str1大于str2" << endl;
} else {
cout << "str1和str2相等" << endl;
}
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)