2014-05-18 13 views
0

我想創建一個優先級隊列程序,它有一個字符串作爲它的數據和數字作爲優先級,入隊(「Hello」,3); 以下是我迄今爲止的內容,但我很難將所有內容放在一起,對於我應該採取什麼樣的改變或幫助我編寫程序的任何幫助將不勝感激。我需要幫助在C++中創建一個優先級隊列

我相信我應該將隊列存儲在向量中,並以某種方式對數據進行排序以匹配相應的優先級。

#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; 
} 
+2

你可能應該使用['''std :: priority_queue''](http://www.cplusplus.com/reference/queue/priority_queue/)和一個自定義的''comparitor''來傳遞它作爲數據類型的'std :: pair ''。除非你的任務是從頭開始實際執行,否則我會強烈推薦研究[堆](http://en.wikipedia.org/wiki/Heap_(data_structure)) – aruisdante

+0

要填補的空白太多你的代碼。我只想指出一件跳出來的事情。所有函數中的'pri'類型都需要是'int',而不是'T'。 –

+0

@RSahu爲什麼呢? 'pri'也可以是'double',我不覺得它是一個模板有什麼問題。 – nwp

回答

2

我會指導你參考std :: priority_queue的cppreference。這應該有助於我的想法。除了維基百科上的優先級隊列,還提供了一個很好的實現部分。