C++ C+中的顶点队列+;

C++ C+中的顶点队列+;,c++,queue,C++,Queue,我想实现Dijkstra算法,并且非常需要在队列中存储顶点 #include <iostream> #include <queue> using namespace std; int main () { priority_queue<int> mypq;//I want to put a pointer to vertex instead of int mypq.push(10);//Here I want to push vertex mypq.pu

我想实现Dijkstra算法,并且非常需要在队列中存储顶点

#include <iostream>
#include <queue>
using namespace std;

int main ()
{

priority_queue<int> mypq;//I want to put a pointer to vertex instead of int

mypq.push(10);//Here I want to push vertex 
mypq.push(20);
mypq.push(15);

cout << "mypq.top() is now " << mypq.top() << endl; 

return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
priority_queue mypq;//我想把指针放在顶点而不是int
mypq.push(10);//这里我要推顶点
mypq.push(20);
mypq.push(15);

cout需要记住的主要一点是,
优先级队列是一个已排序的容器,因此它要求您为所存储的对象定义一个比较(必须遵循严格的弱排序)

既然你谈到了Dijkstra的算法,让我们假设每个顶点都有一个权重,我们希望顶点按这些权重排序

struct vertex { 
    int x, y;
    unsigned weight;

    vertex(int x, int y, unsigned weight) : x(x), y(y), weight(weight) {}
    bool operator <(vertex &other) { return weight < other.weight; }
};
结构顶点{
int x,y;
无符号重量;
顶点(intx,inty,无符号权重):x(x),y(y),权重(权重){}

BoOL运算符你问如何在C++中创建一个结构吗?你非常需要一本C++的书。看看你想要一个队列还是一个优先队列?两个非常不同的事情。优先级队列不是这个词的正常意义上的队列。谢谢你理解我的问题,有时间回答我的问题。我已经编译成你的研究所。解释过了,但我得到了这些错误:::还必须提到它应该是顶点.push;而不是顶点.push_back。@FewTem:请参阅编辑过的答案(感谢您提醒我关于
push
vs.
push_
,对此表示抱歉)。
std::priority_queue<vertex> vertices;

vertices.push(vertex(1, 2, 3));
vertices.push(vertex(0, 1, 2));
vertices.push(vertex(10, 11, 12));

std::cout << "Top = " << vertices.top() << "\n";
std::ostream &operator<<(std::ostream &os, vertex const &v) { 
    return os << "(" << v.x << ", " << v.y << '[' v.weight << "])\n";
}