STL: set 和 priority_queue

  1. 优先级队列默认是底层为vector实现的heap
  2. set 基于平衡红黑树
  3. 两者插入元素时间复杂度都是O(log(n))
  4. 使用默认的greater 得到的结果不同
#include <iostream>
#include <queue>
#include <vector>
#include <set>
int main(){
    using namespace std;
    vector<int>vec{1,3,5,78,4,23};
    set<int, greater<int> > myset;
    priority_queue<int, vector<int>, greater<int> >myque;

    for(int i=0; i<6; ++i){
        myset.insert(vec[i]); 
        myque.push(vec[i]);         
    }

    for(set<int, greater<int> >::iterator it=myset.begin() ; it!=myset.end(); ++it){
        cout << *it <<" ";   
    }//从大到小:78 23 5 4 3 1
    cout << endl;

    for(int i=0; i<6; ++i){
        cout << myque.top() <<" ";  
        myque.pop();        
    }// 小顶堆: 1 3 4 5 23 78

    getchar();
    getchar();
    return 0;

}

优先级队列、sort、qsort 与 重载、自定义cmp函数的关系

#include <iostream>
#include <string>
#include <algorithm> // for sort()
#include <queue> // for priority_queue
#include <map>
#include <set>

using namespace std;

/*************************************************************************/
// 重载 < 运算
class student_cmp{
private:
    string name;
    int id;
public:
    student_cmp(string name = "default", int id = 0) :
        name(name) ,
        id(id)
    {
        ;
    }
    inline
        string get_name(){
        return name;
    }

    inline
        int get_id(){
        return id;
    }
    // priority_queue: id 大的优先级低, 从小到大排列 == greater<int>
    // vector sort 中 从大到小排
    bool operator < (const student_cmp &s)const{
        return this->id > s.id;
    }

    friend ostream &operator << (ostream & os, const student_cmp &s){
        os << s.name << ": " << s.id << endl;
        return os;
    }

};

/**********************************************************************/
// 没有重载< 运算, 使用自定义cmp函数
class student_nocmp{
private:
    string name;
    int id;

public:
    student_nocmp(string name = "default", int id = 0) :
        name(name),
        id(id)
    {
        ;
    }

    inline 
    string get_name(){
        return name;
    }

    inline
    int get_id(){
        return id;
    }

    friend ostream &operator << (ostream & os, const student_nocmp &s){
        os << s.name << ": " << s.id << endl;
        return os;
    }

};
// 优先级队列:从小到大排列
class mynocmp{
public:
    bool operator ()(student_nocmp &a, student_nocmp &b)
    {
        return a.get_id() > b.get_id();
    }
};

// for sort 必须返回bool类型
// vector: 从大到小排列
bool sort_nocmp (student_nocmp &a, student_nocmp &b)
{
    return a.get_id() > b.get_id();
}

// for qsort 必须返回int类型, 参数是指针类型
// array: 从大到小排列
// 如果返回值小于0,则qsort 就得知第一个元素应该在前,如果返回值大于0,则第一个元素应该在后。
// 相减顺序与参数一致就是从小到大排序
int qsort_nocmp(const void *a, const void *b)
{
    student_nocmp * p1, *p2;
    p1 = (student_nocmp *)a;
    p2 = (student_nocmp *)b;

    return p1->get_id() - p2->get_id();
}
/**********************************************************************/
int main()
{
    cout << "overload operator < in priority_queue..." << endl;

    priority_queue<student_cmp> stuQueue;

    stuQueue.push(student_cmp());
    stuQueue.push(student_cmp("zhangsan", 23));
    stuQueue.push(student_cmp("lisi", 14));
    stuQueue.push(student_cmp("laowang", 36));
    stuQueue.push(student_cmp("xiaoming", 28));

    while (!stuQueue.empty()){
        cout << stuQueue.top();
        stuQueue.pop();
    }
    cout << endl;

    cout << "without overload operator < in priority_queue..." << endl;
    priority_queue<student_nocmp, vector<student_nocmp>, mynocmp> stunoQueue;

    stunoQueue.push(student_nocmp());
    stunoQueue.push(student_nocmp("zhangsan", 23));
    stunoQueue.push(student_nocmp("lisi", 14));
    stunoQueue.push(student_nocmp("laowang", 36));
    stunoQueue.push(student_nocmp("xiaoming", 28));

    while (!stunoQueue.empty()){
        cout << stunoQueue.top();
        stunoQueue.pop();
    }
    cout << endl;

    /******sort******************/
    cout << "overload operator < in vector..." << endl;
    vector<student_cmp> veccmp;
    veccmp.push_back(student_cmp());
    veccmp.push_back(student_cmp("zhangsan", 23));
    veccmp.push_back(student_cmp("lisi", 14));
    veccmp.push_back(student_cmp("laowang", 36));
    veccmp.push_back(student_cmp("xiaoming", 28));

    sort(veccmp.begin(), veccmp.end());
    for (vector<student_cmp>::iterator it = veccmp.begin(); it != veccmp.end(); ++it)
        cout << *it;
    cout << endl;

    cout << "without overload operator < in vector..." << endl;
    vector<student_nocmp> vecnocmp;
    vecnocmp.push_back(student_nocmp());
    vecnocmp.push_back(student_nocmp("zhangsan", 23));
    vecnocmp.push_back(student_nocmp("lisi", 14));
    vecnocmp.push_back(student_nocmp("laowang", 36));
    vecnocmp.push_back(student_nocmp("xiaoming", 28));

    sort(vecnocmp.begin(), vecnocmp.end(), &sort_nocmp);
    for (vector<student_nocmp>::iterator it = vecnocmp.begin(); it != vecnocmp.end(); ++it)
        cout << *it;
    cout << endl;

    cout << "without overload operator < in array..." << endl;
    student_nocmp arrnocmp[5] = { student_nocmp(), 
                                 student_nocmp("zhangsan", 23),
                                 student_nocmp("lisi", 14),
                                 student_nocmp("laowang", 36),
                                 student_nocmp("xiaoming", 28)
    };

    qsort(arrnocmp, 5, sizeof(student_nocmp), &qsort_nocmp);
    for (int i = 0; i < 5; ++i)
        cout << arrnocmp[i];
    getchar();

    return 0;
}


结果:


结论:
1. 优先级队列定义的< 运算 规定低优先级, 返回为ture的条件说明优先级低;
2. 类外部定义的cmp相当于实现重载operator <;
3. 由于qsort返回值是int型, /如果返回值小于0,则qsort 就得知第一个元素应该在前,如果返回值大于0,则第一个元素应该在后。即,相减顺序与参数一致就是从小到大排序;
4. sort 的效率比qsort高, 还是使用sort 就好了;

你可能感兴趣的:(STL: set 和 priority_queue)