c++字符串按字典序排序
作者:野牛程序员:2023-12-22 08:57:57 C++阅读 3323
C++中,你可以使用std::sort算法对字符串按字典序进行排序。以下是一个简单的示例代码:
#include <iostream>
#include <algorithm>
#include <vector>
int main() {
// 创建一个字符串数组
std::vector<std::string> strings = {"banana", "apple", "orange", "grape"};
// 使用 std::sort 对字符串数组按字典序进行排序
std::sort(strings.begin(), strings.end());
// 输出排序后的结果
for (const auto& str : strings) {
std::cout << str << " ";
}
return 0;
}这段代码创建了一个字符串数组,然后使用std::sort对数组中的字符串按字典序进行排序。最后,通过循环输出排序后的结果。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

- 上一篇:组合数学字典序C++实现
- 下一篇:c++将数字1--n按字典序排序后输出前k个数
