📜  STL优先级队列是否允许重复值?

📅  最后修改于: 2021-05-30 16:31:45             🧑  作者: Mango

是的,在C++ priority_queue中,我们可能有重复的值。

// C++ program to demonstrate that duplicate
// values are allowed in a priority queue
// (with maximum value at the top)
#include 
using namespace std;
  
int main()
{
    priority_queue pq;
    pq.push(30);
    pq.push(5);
    pq.push(30);
    cout << pq.top() << " ";
    pq.pop();
    cout << pq.top() << " ";
    pq.pop();
    return 0;
}
输出:
30 30
// C++ program to demonstrate that duplicate
// values are allowed in a priority queue
// (with minimum value at the top)
#include 
using namespace std;
  
int main()
{
    priority_queue pq;
    pq.push(5);
    pq.push(5);
    pq.push(5);
    cout << pq.top() << " ";
    pq.pop();
    cout << pq.top() << " ";
    pq.pop();
    cout << pq.top() << " ";
    pq.pop();
    return 0;
}
输出:
5 5 5
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”