字符串比较大小c++
时间: 2023-11-30 10:41:59 浏览: 669
在C++中,字符串比较大小可以使用`strcmp()`函数。该函数会比较两个字符串的ASCLL值,并返回一个整数值,表示两个字符串的大小关系。具体来说,如果第一个字符串小于第二个字符串,则返回一个负整数;如果第一个字符串大于第二个字符串,则返回一个正整数;如果两个字符串相等,则返回0。
以下是一个使用`strcmp()`函数比较字符串大小的例子:
```c++
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char str1[] = "abc";
char str2[] = "def";
char str3[] = "abc";
int result1 = strcmp(str1, str2);
int result2 = strcmp(str1, str3);
cout << "str1 和 str2 比较的结果是:" << result1 << endl;
cout << "str1 和 str3 比较的结果是:" << result2 << endl;
return 0;
}
```
输出结果为:
```
str1 和 str2 比较的结果是:-3
str1 和 str3 比较的结果是:0
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.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)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)