C++11 priority_queue priority queue use

For containers like STL, we can first refer to the manual
http://www.cplusplus.com/reference/queue/priority_queue/?kw=priority_queue

scenes to be used

For example, the TopN problem
has a task set, the element contains task information and the timestamp when the task was added.
When the number of tasks reaches a certain upper limit, the task with the longest time is deleted.
Then how to find the N tasks with the longest time can be solved by the priority queue.

Common operations
  • top Visit the head of the team
  • empty is the queue empty
  • size returns the number of elements in the queue
  • push inserts elements to the end of the queue (and sorts)
  • pop pop up the leader element
  • emplace constructs an element in place and inserts it into the queue (C++11)
  • swap exchange content (C++11)

The internal structure of priority_queue can be seen as a heap

This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue).

It can only access the elements at the top of the queue, which means that the elements can only be obtained through top, and it does not support random access or subscript access.
Here is an example, insert d0-d5 tasks into the set, then take the first N elements into the priority queue, and output

// g++ -std=c++11 ./demo2.cpp -o demo2
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>
using namespace std;

typedef struct TASK_DATA  //运算符重载<
{
    
    
    std::string task_name;
    int tim;
    TASK_DATA(string i, int t) : task_name(i), tim(t) {
    
    }
    bool operator<(const TASK_DATA& a) const {
    
     return tim < a.tim; }
} TASK_DATA;
bool operator==(const struct TASK_DATA& X, const struct TASK_DATA& Y) {
    
    
    return Y.task_name == X.task_name;
}
struct DATA_hash {
    
    
    size_t operator()(const struct TASK_DATA& _r) const {
    
    
        return std::hash<string>()(_r.task_name);
    }
};

int main() {
    
    
    TASK_DATA d0("100", 101);
    TASK_DATA d1("900", 102);
    TASK_DATA d2("800", 103);
    TASK_DATA d3("700", 100);
    TASK_DATA d4("333", 105);
    TASK_DATA d5("555", 106);

    std::unordered_set<TASK_DATA, DATA_hash> sets;
    sets.insert(d0);
    sets.insert(d1);
    sets.insert(d2);
    sets.insert(d3);
    sets.insert(d4);
    sets.insert(d5);

    std::cout << "unordered_set---" << std::endl;
    for (auto& s : sets) {
    
    
        std::cout << s.task_name << "--" << s.tim << std::endl;
    }

    TASK_DATA d6("555", 888);
    auto s = sets.find(d6);
    if (s != sets.end()) {
    
    
        std::cout << "find it" << std::endl;
    }

    // 优先队列
    priority_queue<TASK_DATA> task_queue;

    // 取top N个元素
    int N = 3;
    for (auto& s : sets) {
    
    
        if (task_queue.size() < N) {
    
    
            task_queue.push(s);
        } else {
    
    
            auto b = task_queue.top();
            if (s < b) {
    
    
                task_queue.pop();
                task_queue.push(s);
            }
        }
    }

    // 遍历
    while (!task_queue.empty()) {
    
    
        auto b = task_queue.top();
        cout << b.task_name << "--" << b.tim << '\n';
        task_queue.pop();
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/niu91/article/details/109121528