开发者

C++中std::sort函数介绍和使用场景

开发者 https://www.devze.com 2024-08-13 13:54 出处:网络 作者: 老狼IT工作室
目录函数介绍使用场景对数组进行排序对 vector 进行排序对字符串进行排序对 pair 进行排序完整示例例子1: 对数组进行正向排序例子2: 对 vector 进行正向排序例子3: 对字符串进行正向排序例子4: 对 pair 进行正向排序
目录
  • 函数介绍
  • 使用场景
    • 对数组进行排序
    • 对 vector 进行排序
    • 对字符串进行排序
    • 对 pair 进行排序
  • 完整示例
    • 例子1: 对数组进行正向排序
    • 例子2: 对 vector 进行正向排序
    • 例子3: 对字符串进行正向排序
    • 例子4: 对 pair 进行正向排序
    • 例子5: 对数组进行反向排序
    • 例子6: 对 vector 进行反向排序
    • 例子7: 对字符串进行反向排序
  • 总结

    std::sort是C++标准库中的一个函数,用于对容器中的元素进行排序。它可以按照默认的升序方式对元素进行排序,也可以指定自定义的比较函数来实现降序排序等其他排序方式。

    函数介绍

    std::sort函数位于<algorithm>头文件中,其原型如下:

    template <class RandomAccessIterator, class Compare>
    void sort (RandoMACcessIterator first, RandomAccessIterator last, Compare comp);
    

    其中,firstlast表示待排序序列的起始和结束迭代器,comp是一个可选的比较函数,用于指定排序规则。如果不提供比较函数,则默认按照升序排序。

    使用场景

    对数组进行排序

    int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    std::sort(arr, arr + sizeof(arr) / sizeof(arr[0]));
    // 现在 arr 数组已经按照升序排列为 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}
    

    对 vector 进行排序

    std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    std::sort(v.begin(), v.end());
    // 现在 v 向量已经按照升序排列为 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}
    

    对字符串进行排序

    std::string str = "hello world";
    std::sort(str.begin(), str.end());
    // 现在 str 字符串已经按照字母表顺序排列为 "dehllloorw"
    

    对 pair 进行排序

    std::pair<int, std::string> p1 = {3, "apple"};
    std::pair<int, std::string> p2 = {1, "banana"};
    std::pair<int, std::string> p3 = {2, "orange"};
    std::vector<std::pair<int, std::string>> vec = {p1, p2, p3};
    std::sort(vec.begin(), vec.end());
    // 现在 vec 向量已经按照第一个元素升序排列为 {{1, "banana"}, {2, "orange"}, {3, "apple"}}
    

    完整示例

    例子1: 对数组进行正向排序

    #include <IOStream>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
        int n = sizeof(arr) / sizeof(arr[0]);
    
        sort(arr, arr + n);
    
        cout << "Sorted array is: ";
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    输出结果为:Sorted array is: 1 1 2 3 3 4 5 5 5 6 9

    例子2: 对 vector 进行正向排序

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    
       www.devze.com sort(v.begin(), v.end());
    
        cout << "Sorted vector is: ";
        for (int i = 0; i < v.size(); i++) {
            cout << v[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    输出结果为:Sorted vector is: 1 1 2 3 3 4 5 5 5 6 9

    例子3: 对字符串进行正向排序

    #include <iostream>
    #include <js;string>
    #include <algorithm>
    using namespace std;
    
    int main() {
        string str = "hello world";
    
        sort(str.begin(), str.end());
    
        cout << "Sorted string is: " << str << endl;
    
        return 0;
    }
    

    输出结果为:Sorted string is: dehllloorw

    例子4: 对 pair 进行正向排序

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    bool compare(const pair<int, string>& a, const pair<int, string>& b) {
       http://www.devze.com return a.first < b.first;
    }
    
    int main() {
        vector<pair<int, string>> v = {{3, "apple"}, {1, "banana"}, {2, "orange"}};
    
        sort(v.begin(), v.end(), compare);
    
        cout << "Sorted vector of pairs is: ";
        for (int i php= 0; i < v.size(); i++) {
            cout << v[i].first << " " << v[i].second << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    输出结果为:Sorted vector of pairs is: 1 banana 2 orange 3 apple

    例子5: 对数组进行反向排序

    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main() {
        int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
        int n = sizeof(arr) / sizeof(arr[0]);
    
        sort(arr, arr + n, greater<int>());
    
        cout << "Reverse sorted array is: ";
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    输出结果为:Reverse sorted array is: 9 6 5 5 5 4 3 3 2 1 1

    例子6: 对 vector 进行反向排序

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
    
        sort(v.rbegin(), v.rend(), greater<int>());
    
        cout << "Reverse sorted vector i编程s: ";
        for (int i = 0; i < v.size(); i++) {
            cout << v[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    输出结果为:Reverse sorted vector is: 9 6 5 5 5 4 3 3 2 1 1

    例子7: 对字符串进行反向排序

    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;
    
    bool compare(const string& a, const string& b) {
        return a > b;
    }
    
    int main() {
        string str = "hello world";
    
        sort(str.rbegin(), str.rend(), compare);
    
        cout << "Reverse sorted string is: " << str << endl;
    
        return 0;
    }
    

    输出结果为:Reverse sorted string is: dlrow olleh

    总结

    std::sort函数是C++标准库中常用的排序函数之一,它可以对各种类型的序列进行排序。通过指定不同的比较函数,可以实现不同的排序方式。在实际开发中,我们经常需要对数据进行排序以便进行后续处理或分析。掌握std::sort函数的使用技巧可以帮助我们更高效地完成这些任务。

    到此这篇关于C++中std::sort函数介绍和使用场景的文章就介绍到这了,更多相关C++ std::sort函数内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号