c++ 输入5个字符串按英文字典排序由大到小顺序输出
时间: 2024-10-27 17:03:37 浏览: 51
在C++中,要输入五个字符串并按照字典序(从大到小)进行排序,你可以使用`std::sort`函数结合自定义的比较函数。下面是一个简单的示例:
```cpp
#include <iostream>
#include <string>
#include <algorithm>
// 自定义比较函数,用于降序排列
bool compareStrings(const std::string &a, const std::string &b) {
return a > b;
}
int main() {
std::vector<std::string> strings;
for (int i = 0; i < 5; ++i) {
std::cout << "请输入第" << i+1 << "个字符串: ";
std::string str;
std::cin >> str;
strings.push_back(str);
}
// 使用自定义比较函数对字符串向量进行降序排序
std::sort(strings.begin(), strings.end(), compareStrings);
// 输出排序后的字符串
std::cout << "按照字典序(从大到小)排序结果: ";
for (const auto &str : strings) {
std::cout << str << " ";
}
std::cout << "\n";
return 0;
}
```
当你运行这个程序时,它会提示用户输入五个字符串,然后将它们按字典序降序排列并输出。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)