优先队列 priority_queue

基本信息

头文件: #include <queue>
小根堆:priority_queue<int , vector<int> , greater<int> >que
大根堆: priority_queue<int> que

相关用法

  • que.push(x) 加入节点 O(logn)
  • que.top(x) 访问栈顶 O(1)
  • que.pop() 弹出栈顶 O(logn)

闲话

关于 nth_element
std::nth_element
参见:std::nth_element

用法:

std::nth_element(first, nth, last);
std::nth_element(first, nth, last, cmp);

它重排 [first, last) 中的元素,使得 nth 所指向的元素被更改为 [first, last) 排好序后该位置会出现的元素。这个新的 nth 元素前的所有元素小于或等于新的 nth 元素后的所有元素。
对于以上两种用法,C++ 标准要求它的平均时间复杂度为 O(n),其中 n 为 std::distance(first, last)。

posted @ 2023-10-29 11:20  _Unnamed  阅读(49)  评论(0)    收藏  举报