我真的很挣扎如何在我的程序中实现一个向量来存储我的数据和优先级,以便我以后可以访问它们。我尝试了几种方法,但是它们导致了一些错误,有人可以帮助我。任何帮助将不胜感激。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template <class T1>
class PriQueue
{
public:
vector<T1> tasks; // My vector which does absolutely NOTHING right now
PriQueue();
void enqueue(T1 str, int pri); //Adds to queue
void setStr(T1 newStr);
void setPri(int newPri);
T1 getStr();
T1 getPri();
void dequeue(T1 str, int pri); //Deletes from queue
void peek(); //Prints the the first in queue
void size(); //Prints how many in queue
//T1 printQ();
private:
T1 s;
T1 p;
};
template <class T1>
PriQueue<T1>::PriQueue()
{
}
template <class T1>
void PriQueue<T1>::enqueue(T1 str, int pri) //Adding an element to the queue
{
this->s = str;
this->p = pri;
tasks.push_back(s); //If I do this, how would I print the front most item?
tasks.push_back(p);
}
template <class T1>
void PriQueue<T1>::setStr(T1 newStr)
{
this->s = newStr;
}
template <class T1>
void PriQueue<T1>::setPri(int newPri)
{
this->p = newPri;
}
template <class T1>
T1 PriQueue<T1>::getStr()
{
return s;
}
template <class T1>
T1 PriQueue<T1>::getPri()
{
return p;
}
template <class T1>
void PriQueue<T1>::dequeue(T1 str, int pri) //Removing an element from the front of the queue
{
}
template <class T1>
void PriQueue<T1>::peek() //Returning a value at front of the queue (NOT removing it)
{
for (auto& v : tasks)
cout << v << endl;
}
template <class T1>
void PriQueue<T1>::size() //Returning the number of items in the queue.
{
}
int main()
{
string more;
string task;
int priority;
do {
PriQueue<string> que;
cout << "What is your task in one word?" << endl;
cin >> task;
que.setStr(task);
cout << "What is the priority level of the task on a scale of 1 to 10 (1 has the highest priority)?" << endl;
cin >> priority;
que.setPri(priority);
//cout << que.getStr() << endl; //Test #1
//cout << que.getPri() << endl; //Test #2
cout << "Would you like to add another task (y/n)?" << endl;
cin >> more;
} while (more == "y" || more == "Y" || more == "Yes" || more == "yes");
que.peek();
return 0;
}