c++ - 我需要帮助在 C++ 中创建优先级队列

标签 c++ templates queue priority-queue

<分区>

我正在尝试创建一个优先级队列程序,该程序将字符串作为其数据,将数字作为优先级,enqueue("Hello", 3); 以下是我到目前为止所拥有的,但我很难将所有内容放在一起,如果能帮助我做一些不同的事情或帮助我编写程序,我们将不胜感激。

我认为我应该将队列存储在一个 vector 中,并以某种方式对其中的数据进行排序以匹配相应的优先级。

#include <iostream>
#include <string>
#include <vector>

using namespace std;


template <class T1, class T2>
class PriQueue
{
public:
//PriQueue();
void enqueue(T1 str, T2 pri); //Adds to queue
void dequeue(T1 str, T2 pri); //Deletes from queue
void peek(T1 str, T2 pri); //Prints the the first in queue
void size(T1 size); //Prints how many in queue

T1 printQ();

private:
T1 s;
T2 p;

};

template <class T1, class T2>
void PriQueue<T1, T2>::enqueue(T1 str, T2 pri) //Adding an element to the queue
{


this->s = str;
this->p = pri;

}

template <class T1, class T2>
void PriQueue<T1, T2>::dequeue(T1 str, T2 pri) //Removing an element from the front of the queue
{


}

template <class T1, class T2>
void PriQueue<T1, T2>::peek(T1 str, T2 pri) //Returning a value at front of the queue (NOT removing it)
{


}

template <class T1, class T2>
void PriQueue<T1, T2>::size(T1 size) //Returning the number of items in the queue.
{


}



using namespace std;


int main()
{

PriQueue<string, int> que;

que.enqueue("Hello", 3);
que.enqueue("Bye", 2);
que.enqueue("No", 5);

cout << que.printQ() << endl;


return 0;
}

最佳答案

我会指导您使用 std::priority_queue 的 cppreference。我认为这应该有所帮助。维基百科上的 Priority Queue 也提供了一个很好的实现部分。

关于c++ - 我需要帮助在 C++ 中创建优先级队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23718552/

相关文章:

c++ - Eclipse CDT QT4 自动完成不工作

c++ - 使用资源中的对话

c++ - N选K、K-N、K-2N等,递归中递归

c++ - 多生产者单消费者队列

python - 查找给定大小的每个连续子数组的最大值

c++ - (c++23 隐式 move ) 将 move 的本地存储变量作为仅带括号的右值引用返回?

c++ - 尝试避免某些特定 C++ 代码的代码重复

c++ - 在 vs2015 的可变参数模板中找不到重载的成员函数

c++ - 为什么只能在头文件中实现模板?

api - 在 mq api 导出中获取别名队列的已解析队列名称