#include <iostream>
#include <queue>
using namespace std;
int main()
{
// cout<<"Hello World";
priority_queue<int> spq; // max heap
priority_queue <int, vector<int>, greater<int>> lpq; // min heap
spq.push(1);
lpq.push(2);
lpq.push(3);
cout << spq.size() - lpq.size() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这段代码给了我意想不到的非常大的价值18446744073709551615
我无法理解这里的问题。
以下代码(有std::swap两个std::priority_queue<int, std::vector<int>, decltype(some lambda)>s)会在 C++17 中导致编译器错误,但在 C++20 中不会导致编译器错误。
#include <queue>
int main()
{
auto cmp = [](int x, int y) {return x > y;};
std::priority_queue<int, std::vector<int>, decltype(cmp)> pq1(cmp), pq2(cmp);
std::swap(pq1, pq2); // adding this line results in an error
return 0;
}
Run Code Online (Sandbox Code Playgroud)
C++17下的错误是
error: object of type 'value_compare' (aka '(the lambda cmp)') cannot be assigned because its copy assignment operator is implicitly deleted
{c = _VSTD::move(__q.c); comp = _VSTD::move(__q.comp); return *this;}
note: in instantiation of member …Run Code Online (Sandbox Code Playgroud) 如果这是一个愚蠢的问题(特别是对于用 java 开发超过 4 年的人)或者它是否已得到解答(我已经搜索了堆栈溢出,尽管问题很简单,但我找不到重复),但我创建了两个优先级队列,并想检查它们是否以完全相同的顺序包含完全相同的元素。然而,当我在两个看似相同优先级的队列上调用 .equals() 时,我返回了 false。我希望通过直接比较(==)而不是通过 .equals() 方法得到这么多。我查看了 java 文档,但他们没有对此行为提供解释。
为了测试此行为,我运行了以下代码段:
PriorityQueue<Character> window = new PriorityQueue<>();
PriorityQueue<Character> base = new PriorityQueue<>();
System.out.println(window.equals(base));
System.out.println(base.equals(base));
window.offer('a');
window.offer('b');
base.offer('a');
base.offer('b');
System.out.println(window.equals(base));
Run Code Online (Sandbox Code Playgroud)
输出是:
false
true
false
Run Code Online (Sandbox Code Playgroud)
然而,我期望的是:
true
true
true
Run Code Online (Sandbox Code Playgroud)
我之前已经重写了 equals 方法,所以如果这是我必须做的事情,我可以处理它,但我只是对结果感到非常惊讶。我知道我可以将 PriorityQueue 转换为字符串或数组,然后比较它们,但这违背了使用 PriorityQueue 的全部意义(更不用说它会占用的所有多余空间)。
例如,我们有priority_queue<int> s;一些元素.以下代码的正确形式是什么:
while (!s.empty()) {
int t=s.pop();// this does not retrieve the value from the queue
cout<<t<<endl;
}
Run Code Online (Sandbox Code Playgroud) #pragma once
#ifndef PRIQUE_H
#define PRIQUE_H
typedef struct queue_node
{
int val;
int priority;
struct queue_node *link;
}
typedef struct p_queue
{
int size;
queue_node *first;
}
Run Code Online (Sandbox Code Playgroud)
这是我的头文件代码。当我运行主程序时,出现重复的typedef错误。如何解决。最初,我将所有代码都包含在一个文件中,但是期望制作一个头文件可以解决我制作此文件以及相应的定义文件的问题。请告诉我我错了,为什么会发生此问题?
我正在用C++实现一个简单的优先级队列.
然而,当它运行时,它打印出乱码数字.我在某种程度上试图在我的代码中访问数组中的无效条目?
下面是代码.
另外,我的"删除"功能是不是在做它的工作?从概念上讲,我应该将null放入第一个条目并返回刚删除的内容吗?
谢谢.
[Priority.h]
#ifndef Priority_h
#define Priority_h
class Priority
{
public:
Priority(void);
Priority(int s);
~Priority(void);
void insert(long value);
long remove();
long peekMin();
bool isEmpty();
bool isFull();
int maxSize;
long queArray [5];
int nItems;
private:
};
#endif
Run Code Online (Sandbox Code Playgroud)
[Priority.cpp]
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include "Priority.h"
using namespace std;
Priority::Priority(void)
{
}
Priority::Priority(int s)
{
nItems = 0;
}
Priority::~Priority(void)
{
}
void Priority::insert(long item)
{
int j;
if(nItems==0) // if no items,
{
queArray[0] = …Run Code Online (Sandbox Code Playgroud) 我正在尝试初始化priority_queue,这是代码
class stdnt{
public:
int indx;
int c;
int lvl;
bool operator<(const stdnt &x)
{
return this->c > x.c;
}
};
priority_queue<stdnt> pq;
Run Code Online (Sandbox Code Playgroud)
但它给了我传递const&discards限定符的错误.我还应该怎么做呢?
我想知道是否有办法构建一个支持常量get-min,delete-min和merge操作的优先级队列结构.我不关心插入的时间复杂性,也不必支持reduce-key操作.我的用例在(坏)伪代码中:
func periodical(current_state) {
// always_executed_jobs is a priority queue
queue = always_executed_jobs;
// other_jobs is an array of priority queues;
// current_state is an index to the array, so
// sometimes_executed_jobs is another priority queue
sometimes_executed_jobs = other_jobs[current_state];
queue.merge(sometimes_executed_jobs);
while (!queue.empty()) {
job = get_min(queue);
execute(job);
delete_min(queue);
}
}
Run Code Online (Sandbox Code Playgroud)
我考虑过splay树(特别是https://cs.stackexchange.com/questions/524/does-there-exist-a-priority-queue-with-o1-extracts)和Fibonacci堆,但他们不喜欢似乎满足这些要求.
我一直在玩STL容器和它们支持的比较函数/仿函数,但是我发现priority_queue不遵循通常的严格弱序,我试图理解可能是什么原因但不能弄明白,任何指针会有所帮助.
它在本博客中还提到priority_queue不遵循严格的弱排序.在此输入链接描述
#include "STL.h"
#include "queue"
#include "vector"
#include "iostream"
#include "functional"
using namespace std;
typedef bool(*func)(const int& val1 , const int& val2);
bool strict_weak_order_function(const int& val1 , const int& val2){
return val1 > val2;
}
bool comparer_function(const int& val1 , const int& val2){
return !strict_weak_order_function(val1 , val2);
}
struct Compaper_functor{
bool operator()(const int& val1 , const int& val2){
return !strict_weak_order_function(val1 , val2);
}
};
void runPriorityQueue(void){
//priority_queue<int , vector<int> , func > pq(comparer_function);
priority_queue<int , vector<int> , Compaper_functor …Run Code Online (Sandbox Code Playgroud) 我试图在C++中打印STL priority_queue,但是我在打印队列的所有元素时遇到问题.
priority_queue<Job, vector<Job>, greater<Job>> q = pq;
for(int i = 0; i <= q.size(); i++) {
cout << q.top() << "\n";
q.pop();
}
Run Code Online (Sandbox Code Playgroud)
但是,当列表中有一个或两个元素时使用此代码就可以了,但只要我输入三个或更多元素,它就会切断要打印的最后一个项目.我不太确定为什么会发生这种情况,但这让我感到困惑了一会儿.