c++字符串数组排序的方法是什么

c++
927
2024/3/4 10:03:09
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C++中,可以使用标准库中的sort函数对字符串数组进行排序。示例如下:

#include <iostream>
#include <algorithm>
#include <string>

int main() {
    std::string arr[] = {"apple", "banana", "orange", "grape"};
    int n = sizeof(arr) / sizeof(arr[0]);

    std::sort(arr, arr + n);

    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

上述代码中,首先包含了必要的头文件,然后定义了一个字符串数组arr,并计算数组的大小。接着使用std::sort函数对数组进行排序,最后遍历输出排序后的数组。最终输出结果为apple banana grape orange

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: islowe在C++字符串处理中的应用