c++将字符串进行排序。(bdcaegf——abcdefg)
作者:野牛程序员:2024-11-01 11:43:28 C++阅读 2644
c++将字符串进行排序。(bdcaegf——abcdefg)
可以使用 C++ 中的 std::sort 函数对字符串进行排序。以下是一个示例代码,演示如何对字符串进行排序:
#include <iostream>
#include <algorithm> // 包含 sort 函数
#include <string>
using namespace std;
int main() {
string str = "bdcaegf";
// 使用 sort 函数对字符串进行排序
sort(str.begin(), str.end());
cout << "排序后的字符串: " << str << endl;
return 0;
}代码说明:
#include <algorithm>:包含算法库,以便使用std::sort函数。std::sort(str.begin(), str.end()):对字符串str的所有字符进行排序。输出结果:输出排序后的字符串。
运行该程序后,输入的字符串 "bdcaegf" 将被排序为 "abcdefg"。
野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892

