最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中std::sort函數(shù)介紹和使用場(chǎng)景

 更新時(shí)間:2024年02月19日 15:19:22   作者:老狼IT工作室  
std::sort函數(shù)是C++標(biāo)準(zhǔn)庫(kù)中常用的排序函數(shù)之一,它可以對(duì)各種類(lèi)型的序列進(jìn)行排序,本文就來(lái)介紹一下C++中std::sort函數(shù)介紹和使用場(chǎng)景,感興趣的可以了解一下

std::sort是C++標(biāo)準(zhǔn)庫(kù)中的一個(gè)函數(shù),用于對(duì)容器中的元素進(jìn)行排序。它可以按照默認(rèn)的升序方式對(duì)元素進(jìn)行排序,也可以指定自定義的比較函數(shù)來(lái)實(shí)現(xiàn)降序排序等其他排序方式。

函數(shù)介紹

std::sort函數(shù)位于<algorithm>頭文件中,其原型如下:

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

其中,firstlast表示待排序序列的起始和結(jié)束迭代器,comp是一個(gè)可選的比較函數(shù),用于指定排序規(guī)則。如果不提供比較函數(shù),則默認(rèn)按照升序排序。

使用場(chǎng)景

對(duì)數(shù)組進(jìn)行排序

int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(arr, arr + sizeof(arr) / sizeof(arr[0]));
// 現(xiàn)在 arr 數(shù)組已經(jīng)按照升序排列為 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}

對(duì) vector 進(jìn)行排序

std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
std::sort(v.begin(), v.end());
// 現(xiàn)在 v 向量已經(jīng)按照升序排列為 {1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9}

對(duì)字符串進(jìn)行排序

std::string str = "hello world";
std::sort(str.begin(), str.end());
// 現(xiàn)在 str 字符串已經(jīng)按照字母表順序排列為 "dehllloorw"

對(duì) pair 進(jìn)行排序

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());
// 現(xiàn)在 vec 向量已經(jīng)按照第一個(gè)元素升序排列為 {{1, "banana"}, {2, "orange"}, {3, "apple"}}

完整示例

例子1: 對(duì)數(shù)組進(jìn)行正向排序

#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;
}

輸出結(jié)果為:Sorted array is: 1 1 2 3 3 4 5 5 5 6 9

例子2: 對(duì) vector 進(jìn)行正向排序

#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.begin(), v.end());

    cout << "Sorted vector is: ";
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;

    return 0;
}

輸出結(jié)果為:Sorted vector is: 1 1 2 3 3 4 5 5 5 6 9

例子3: 對(duì)字符串進(jìn)行正向排序

#include <iostream>
#include <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;
}

輸出結(jié)果為:Sorted string is: dehllloorw

例子4: 對(duì) pair 進(jìn)行正向排序

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

bool compare(const pair<int, string>& a, const pair<int, string>& b) {
    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 = 0; i < v.size(); i++) {
        cout << v[i].first << " " << v[i].second << " ";
    }
    cout << endl;

    return 0;
}

輸出結(jié)果為:Sorted vector of pairs is: 1 banana 2 orange 3 apple

例子5: 對(duì)數(shù)組進(jìn)行反向排序

#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;
}

輸出結(jié)果為:Reverse sorted array is: 9 6 5 5 5 4 3 3 2 1 1

例子6: 對(duì) vector 進(jìn)行反向排序

#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 is: ";
    for (int i = 0; i < v.size(); i++) {
        cout << v[i] << " ";
    }
    cout << endl;

    return 0;
}

輸出結(jié)果為:Reverse sorted vector is: 9 6 5 5 5 4 3 3 2 1 1

例子7: 對(duì)字符串進(jìn)行反向排序

#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;
}

輸出結(jié)果為:Reverse sorted string is: dlrow olleh

總結(jié)

std::sort函數(shù)是C++標(biāo)準(zhǔn)庫(kù)中常用的排序函數(shù)之一,它可以對(duì)各種類(lèi)型的序列進(jìn)行排序。通過(guò)指定不同的比較函數(shù),可以實(shí)現(xiàn)不同的排序方式。在實(shí)際開(kāi)發(fā)中,我們經(jīng)常需要對(duì)數(shù)據(jù)進(jìn)行排序以便進(jìn)行后續(xù)處理或分析。掌握std::sort函數(shù)的使用技巧可以幫助我們更高效地完成這些任務(wù)。

到此這篇關(guān)于C++中std::sort函數(shù)介紹和使用場(chǎng)景的文章就介紹到這了,更多相關(guān)C++ std::sort函數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 關(guān)于C++內(nèi)部類(lèi)的介紹與使用示例

    關(guān)于C++內(nèi)部類(lèi)的介紹與使用示例

    今天小編就為大家分享一篇關(guān)于關(guān)于C++內(nèi)部類(lèi)的介紹與使用示例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-12-12
  • sizeof()的簡(jiǎn)單介紹

    sizeof()的簡(jiǎn)單介紹

    sizeof操作符以字節(jié)形式給出了其操作數(shù)的存儲(chǔ)大小
    2013-04-04
  • 解析C語(yǔ)言中位字段內(nèi)存分配的問(wèn)題

    解析C語(yǔ)言中位字段內(nèi)存分配的問(wèn)題

    本篇文章是對(duì)C語(yǔ)言中位字段內(nèi)存分配的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • C語(yǔ)言如何實(shí)現(xiàn)三子棋

    C語(yǔ)言如何實(shí)現(xiàn)三子棋

    這篇文章主要介紹了C語(yǔ)言如何實(shí)現(xiàn)三子棋問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • C++成員函數(shù)中const的使用詳解

    C++成員函數(shù)中const的使用詳解

    這篇文章主要為大家詳細(xì)介紹了C++成員函數(shù)中const的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • C++ lambda函數(shù)詳解

    C++ lambda函數(shù)詳解

    小編可以明確告訴大家:lambda函數(shù)是C++11中最重要的,使用最廣泛的,最具現(xiàn)代風(fēng)格的內(nèi)容,lambda函數(shù)的出現(xiàn)改變了C++編程的思維方式。所以快和小編學(xué)習(xí)一下C++11中l(wèi)ambda函數(shù)的使用吧
    2023-02-02
  • boost.asio框架系列之調(diào)度器io_service

    boost.asio框架系列之調(diào)度器io_service

    這篇文章介紹了boost.asio框架系列之調(diào)度器io_service,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-06-06
  • C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出實(shí)例

    C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出實(shí)例

    這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)單鏈表逆序與逆序輸出,是數(shù)據(jù)結(jié)構(gòu)與算法中比較基礎(chǔ)的重要內(nèi)容,有必要加以牢固掌握,需要的朋友可以參考下
    2014-08-08
  • C語(yǔ)言菜鳥(niǎo)基礎(chǔ)教程之單精度浮點(diǎn)數(shù)與雙精度浮點(diǎn)數(shù)

    C語(yǔ)言菜鳥(niǎo)基礎(chǔ)教程之單精度浮點(diǎn)數(shù)與雙精度浮點(diǎn)數(shù)

    在C語(yǔ)言中,單精度浮點(diǎn)數(shù)(float)和雙精度浮點(diǎn)數(shù)(double)類(lèi)型都是用來(lái)儲(chǔ)存實(shí)數(shù)的,雙精度是用記憶較多,有效數(shù)字較多,數(shù)值范圍較大。
    2017-10-10
  • 關(guān)于在C程序中處理UTF-8文本的方法詳解

    關(guān)于在C程序中處理UTF-8文本的方法詳解

    這篇文章主要給大家介紹了關(guān)于在C程序中處理UTF-8文本的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-11-11

最新評(píng)論

无为县| 兴安县| 嘉峪关市| 如皋市| 冷水江市| 陇川县| 宁夏| 永丰县| 台湾省| 苏尼特右旗| 福鼎市| 巧家县| 宜宾市| 颍上县| 砚山县| 出国| 高清| 山阳县| 赞皇县| 昆山市| 如东县| 盱眙县| 香格里拉县| 宁都县| 武陟县| 准格尔旗| 大埔区| 花垣县| 苏州市| 昌吉市| 宾川县| 台东市| 荥经县| 洪雅县| 湖南省| 百色市| 桂阳县| 沈丘县| 庆城县| 德兴市| 大理市|