Linux 拨号vps windows公众号手机端

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

lewis 7年前 (2018-04-08) 阅读数 7 #程序编程
文章标签 c++

在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

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门