C 中应用sort对普遍器皿排列

STL中sort的操作方法及其应用sort对vector、list和map开展排列

文中关键处理下列难题

  1. STL中sort的操作方法
  2. 应用sort对vector的排列
  3. 应用sort对map排序
  4. 应用sort对list排列

STL中sort的操作方法

C STL 标准库中的 sort() 涵数,实质便是一个模板函数。该涵数专业用于对器皿或一般二维数组中特定范畴内的原素开展排列,排列标准默认设置以原素值的尺寸做降序排列,此外大家还可以挑选标准库给予的其他排列标准(例如std::greater降序排列标准),乃至还能够自定义排序标准。

值得一提的是,sort() 涵数坐落于头文件中,因而在应用该涵数前,程序流程中应包括以下句子:

#include <algorithm>

sort() 涵数有 2 种使用方法,其英语的语法文件格式各自为:

//对 [first, last) 地区内的原素做默认设置的降序排列
void sort (RandomAccessIterator first, RandomAccessIterator last);
//依照特定的 comp 排列标准,对 [first, last) 地区内的原素开展排列
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

在其中,first 和 last 都为任意浏览迭代器,他们的组成 [first, last) 用于特定要排列的总体目标地区;此外在第 2 种文件格式中,comp 能够是 C STL 标准库给予的排列标准(例如 std::greater),还可以是自定的排列标准。

数组排序示例:

#include <algorithm>
#include <algorithm>

using namespace std;

int main(){
    int arr[] = {2,6,3,5,4,8,1,0,9,10};
    sort(arr, arr 10);
    for(int i = 0;i < 10;i  )
        cout << arr[i] << " ";
}
// out
/*
0 1 2 3 4 5 6 8 9 10
*/

应用 STL 标准库给予的排列标准

int main(){
    int arr[] = {2,6,3,5,4,8,1,0,9,10};
    sort(arr, arr 10, std::greater<int>());
    for(int i = 0;i < 10;i  )
        cout << arr[i] << " ";
    cout << endl;
    sort(arr, arr 10, std::less<int>());
    for(int i = 0;i < 10;i  )
        cout << arr[i] << " ";
}
// out
/*
10 9 8 6 5 4 3 2 1 0
0 1 2 3 4 5 6 8 9 10
*/

应用自定电压比较器

bool cmp(const int a, const int b){
    return a < b;
}
int main(){
    int arr[] = {2,6,3,5,4,8,1,0,9,10};
    sort(arr, arr 10, cmp);
    for(int i = 0;i < 10;i  )
        cout << arr[i] << " ";
}
// out
/*
0 1 2 3 4 5 6 8 9 10
*/

应用 lambda 关系式自定电压比较器

int main(){
    int arr[] = {2,6,3,5,4,8,1,0,9,10};
    sort(arr, arr 10, [](const int a, const int b){
         return a < b;
         });
    for(int i = 0;i < 10;i  )
        cout << arr[i] << " ";
}
// out
/*
0 1 2 3 4 5 6 8 9 10
*/

应用sort对vector的排列

在 C 中基本上实际操作vector时,基本上能够看作是在实际操作二维数组,能够将vector当作对二维数组的封裝。因而,应用sort对vector开展排列时彻底能够遵照上边应用sort对二维数组的排序算法。

一维vector排列

int main(){
    vector<int> vec = {2,6,3,5,4,8,1,0,9,10};
    sort(vec.begin(), vec.end());
    for(int item: vec)
        cout << item << " ";
    return 0;
}
// out
/*
0 1 2 3 4 5 6 8 9 10
*/

二维vector排列。二维数组储存一系列的座标,先依照第二维开展降序排序,再依照第一维降序排序

int main(){
    vector<vector<int>> vvi = {{9,1}, {2,3}, {8,7}, {6,2}, {5,2}};

    sort(vvi.begin(), vvi.end(), [](const vector<int>& v1, const vector<int>& v2){
         if(v1[1] < v2[1]) return true;
         else if(v1[1] == v2[1]) return v1[0] < v2[0];
         else return false;
         });

    for(vector<int> v: vvi){
        for(int item: v){
            cout << item << " ";
        }
        cout << endl;
    }

    return 0;
}
// out
/*
9 1
5 2
6 2
2 3
8 7
*/

应用sort对map排序

map是用于储放<key, value>键值对的算法设计,能够很便捷迅速的依据key查出相对应的value,map自身的完成方法含有了电压比较器的设定,只需我们在map复位的情况下传到电压比较器,就可以进行相匹配的排列。

界定包括新鲜水果以及数量的map,依照水果名字字典序开展排列 (按key排列)

#include<map>

using namespace std;

int main(){
    map<string, int, less<string>> msi;
    msi["apple"] = 5;
    msi["watermelon"] = 2;
    msi["pear"] = 3;
    msi["peach"] = 6;
    msi["cherry"] = 10;

    for(auto item: msi)
        cout << item.first << " " << item.second << endl;

    return 0;
}
// out
/*
apple 5
cherry 10
peach 6
pear 3
watermelon 2
*/

界定包括新鲜水果以及数量的map,依照新鲜水果数量开展排列,当新鲜水果数量同样时,依照水果名字字典序排列 (将map变为vector开展排列)

bool cmp(const pair<string, int>& a, const pair<string, int>& b){
    if(a.second < b.second) return true;
    else if(a.second == b.second) return a.first < b.first;
    else return false;
}
int main(){
    map<string, int> msi;
    msi["apple"] = 5;
    msi["watermelon"] = 2;
    msi["pear"] = 3;
    msi["peach"] = 5;
    msi["cherry"] = 10;

    vector<pair<string, int>> vpi(msi.begin(), msi.end());
    sort(vpi.begin(), vpi.end(), cmp);

    for(auto item: vpi){
        cout << item.first << " " << item.second << endl;
    }

    return 0;
}
// out
/*
watermelon 2
pear 3
apple 5
peach 5
cherry 10
*/

应用sort对list排列

sort() 函数模板界定在头文件 algorithm 中,规定应用任意浏览迭代器。但 list 器皿并不给予任意浏览迭代器,只给予双重迭代器,因而不可以对 list 中的原素应用 sort() 优化算法。可是,或是能够开展原素排列,由于 list 模版界定了自身的 sort() 涵数。sort() 有两个版本号:无参 sort() 涵数将全部原素降序排序。第二个版本号的 sort() 接纳一个涵数目标或 lambda 关系式做为主要参数,这二种主要参数都界定一个肯定用于较为2个原素。

list排列实例

int main(){
    list<string> ls = {"one", "two", "three"};
    ls.sort([](const string& a, const string& b){
                return a < b;
            });
    for(string item: ls) cout << item << " ";

    return 0;
}
// out
/*
one three two
*/

论文参考文献

C 中SORT涵数操作方法

C sort()排序函数使用方法详细说明

C Lambda关系式详细说明

STL-map的简易使用方法

C STL中Map的按Key排列和按Value排列

C list(STL list)排列及合拼原素方式 详细说明

评论(0条)

刀客源码 游客评论