C++自定义排序算法

就目前所利用的知识中,有两处用到了自定义排序算法。 第一个是sort函数;第二个是部分排序容器的建立,例如map,set,priority_queue。在此记录一些通用的方法,至于其他更多原理,等有时间在记录。

  • 在C++ STL中,对于 vector,有 sort 函数,可以对 vector 中的元素进行排序。

注意,下面的例子, sort(vec.begin(), vec.end(), cmp()),都加了括号,使用的是函数对象,更快。sort() 只对 array、vector、deque 这 3 个容器提供支持

例子一:对基本数据类型进行排序

//首先,sort()函数,默认是 升序。

struct cmp{
    bool operator()(int& a, int& b) {
        return a > b;
    }
};

int main()
{
    vector<int> vec;
    vec.push_back(3);
    vec.push_back(2);
    vec.push_back(4);

//调用函数对象定义的排序规则
    sort(vec.begin(), vec.end(), cmp());

    for (int i : vec) {
        cout << i << endl;
    }
//输出:4 3 2
//默认为升序
    sort(vec.begin(), vec.end());
    for (int i : vec) {
        cout << i << endl;
    }
//输出:2 3 4
    return 0;
}

例子二:对结构体,复杂类型进行排序(在写代码的时候,注意括号匹配什么的)

struct node{
    int age;
    string name;
    node(int a, string n):age(a),name(n){}
};

struct cmp{
    bool operator() (node& a, node& b) {
        if (a.name != b.name)
            return a.name < b.name;
        return a.age < b.age;
    }//先对名字升序排序,如果名字相等,对年龄升序
};

int main()
{
    vector<node> vec;
    vec.push_back(node(15,"zhangsan"));
    vec.push_back(node(17,"zhangsan"));
    vec.push_back(node(13,"zhangsan"));
    vec.push_back(node(15,"zz"));

    sort(vec.begin(), vec.end(), cmp());
    for (auto i : vec) {
        cout << i.name << " " << i.age << endl;
    }
    return 0;
}

//输出:
zhangsan 13
zhangsan 15
zhangsan 17
zz 15
  • 在C++中,有一些容器,priority_queue,map,multimap,multiset等,在添加到容器内,都会按照规则排序存进去。

例子一,以priotity_map,讲解,默认是大顶堆。

struct node{
    int age;
    string name;
    node(int a, string n):age(a),name(n){}
};
struct cmp {
    //先对年龄升序,年龄一样,对名字升序,注意是 >
    bool operator() (node& a, node& b) {
        if (a.age != b.age)
            return a.age > b.age;
        return a.name > b.name;
    }
};

int main()
{
    priority_queue<node, vector<node>, cmp> pque;
    pque.push(node(15,"zhangsan"));
    pque.push(node(13,"lisi"));
    pque.push(node(15,"lisi"));

    while(!pque.empty()) {
        cout << pque.top().age << " " << pque.top().name << endl;
        pque.pop();
    }

    return 0;
}
//输出:
13 lisi
15 lisi
15 zhangsan

priority_queue  是以堆形式存储。

struct cmp{
    bool operator() (int a, int b) {
        return a < b; //升序
    }
};

int main()
{
    vector<int> vec = {1,4,2,3,5};
    sort(vec.begin(), vec.end(), cmp());
    for (auto i : vec) {
        cout << i << endl;
    }
    return 0;
}//输出 1 2 3 4 5
struct cmp{
    bool operator() (int a, int b) {
        return a < b; //大顶堆,类似于降序。
    }
};

int main()
{
    priority_queue<int, vector<int>, cmp > que;
    que.push(1);
    que.push(5);
    que.push(3);
    que.push(2);

    while (!que.empty()) {
        cout << que.top() << endl;
        que.pop();
    }
    return 0;
}// 输出 5 3 2 1

例子二,set map

struct cmp{
    bool operator() (int a, int b) {
        return a > b;//降序
    }
};

int main()
{
    set<int,cmp> se;
    se.insert(1);
    se.insert(5);
    se.insert(2);

    for(auto i = se.begin(); i != se.end(); i++) {
        cout << *i << endl;
    }
    return 0;
}
//输出 5 2 1
struct cmp{
    bool operator() (int a, int b) {
        return a > b; //降序
    }
};

int main()
{
    map<int,int,cmp> mp;
    mp[1] = 10;
    mp[4] = 3;
    mp[2] = 6;

    for(auto i = mp.begin(); i != mp.end(); i++) {
        cout << i->first << " " << i->second << endl;
    }
    return 0;
}
输出// 4 3
     2 6
     1 10
对key 进行降序

猜你喜欢

转载自blog.csdn.net/JACKSONMHLK/article/details/114485674