C++中顶点的队列
Queue of vertex in C++
我想实现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;
}
阅读评论部分。
需要记住的主要一点是,priority_queue
是一个已排序的容器,因此它要求您为要存储的对象定义一个比较(必须遵循严格的弱排序)。
既然你谈到了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; }
};
现在,顶点对象的优先级队列非常简单:
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";
}
相关文章:
- 如何循环打印顶点结构
- boost::进程间消息队列引发错误
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- Android NDK传感器向事件队列报告奇怪的间隔
- C++优先级队列,按对象的唯一指针的特定方法升序排列
- 按对象的特定方法按升序排列的C++优先级队列
- 使用2个键的cpp-stl::优先级队列排序不正确
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 在一个读写器队列中,我可以用volatile替换原子吗
- 为什么我的多线程作业队列崩溃
- 尝试将lambda函数放在队列中时出现一般分配器错误(可能是与unique_ptr有关的错误)
- D3D11-将混合权重和索引传递到顶点着色器
- 使用"Task"函数指针队列定义作业管理器
- 在c++队列中使用pop和visit实现线程安全
- 为什么我需要C++中不同的排序格式来对这个USACO代码上的数组和优先级队列进行排序
- 从返回的顶点缓冲区查询顶点结构
- Vulkan 中的动态顶点缓冲区格式设置
- 打印优先级队列
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- C++中顶点的队列