本文整理汇总了C++中PriorityQueue::insertItem方法的典型用法代码示例。如果您正苦于以下问题:C++ PriorityQueue::insertItem方法的具体用法?C++ PriorityQueue::insertItem怎么用?C++ PriorityQueue::insertItem使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PriorityQueue
的用法示例。
在下文中一共展示了PriorityQueue::insertItem方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: dijkstra
void Graph::dijkstra(Vertex *src)
{
comparisons = 0;//tallies number of comparisons made
src->d = 0;
src->p = NULL;
PriorityQueue<Vertex*> pq;
for(int i = 0;i<vertList.size();++i){//Fill priority queue
if(vertList[i]->getID() != src->getID()){
vertList[i]->d = INT_MAX;
vertList[i]->p = NULL;
}
pq.insertItem(vertList[i]->d, vertList[i], vertList[i]->getID());
}
while(!pq.isEmpty()){//Start sifting through the vertices
Vertex* u = pq.minElement();
pq.removeMin();
int sz = u->outList.size();
for(int i =0; i < sz;++i){
int alt = u->d + u->outList[i]->getWeight();
Vertex* evalVert = u->outList[i]->geteVertP();
comparisons++;
if(u->outList[i]->geteVertP()->d == INT_MAX){//relax function
Locator repkey = pq.getLoc(evalVert->getID());
pq.replaceKey(repkey,alt);
evalVert->setD(alt);
evalVert->setP(u);
}
else if(alt < u->outList[i]->geteVertP()->d){//relax function
Locator repkey = pq.getLoc(evalVert->getID());
pq.replaceKey(repkey,alt);
evalVert->setD(alt);
evalVert->setP(u);
}
}
}
comparisons += pq.getComps();//grab comparisons from priority queue
return;
}
示例2: main
int main() {
PriorityQueue<string> pq;
PQVector<string> pqv;
string elem;
int what = 1;
cout << "1. Priority Queue with Binary Heap\n2. Priority Queue with Vector\nChoic: ";
cin >> what;
if(what ==1){
string again, right, left;
left = 'y'; right = 'Y'; again = 'Y';
elem = "abc"; pq.insertItem(3, elem);
elem = "2ab"; pq.insertItem(4, elem);
elem = "bbc"; pq.insertItem(1, elem);
elem = "cnn"; pq.insertItem(5, elem);
elem = "ddn"; pq.insertItem(6, elem);
elem = "eda"; pq.insertItem(7, elem);
elem = "ggg"; pq.insertItem(9, elem);
elem = "kkk"; pq.insertItem(10, elem);
elem = "lll"; pq.insertItem(11, elem);
elem = "mmm"; pq.insertItem(12, elem);
// elem = "qqq"; pq.insertItem(16, elem);
cout << "\nAlready inserted heaps\n";
cout << "===============\n";
pq.printPQ();
cout << "===============\n";
// cout << "---------Before Removing Min\n";
// cout << "minElement: " << pq.minElement() << endl;
// cout << "minKey: " << pq.minKey() << endl;
// cout << "Min element removed." << endl;
// pq.remove(pq.min());
// cout << "--------After removing min\n";
// cout << "minElement: " << pq.minElement() << endl;
// cout << "minKey: " << pq.minKey() << endl;
// pq.decreaseKey(pq.min(),pq.minKey());
// cout << "--------After decreasing loc 3\n";
// pq.decreaseKey(3,6);
// cout << "================\n";
// pq.decreaseKey(pq.minKey(), pq.min());
while (again.compare(right) == 0 || again.compare(left) == 0)
{
char choi;
cout << "What would you like to do? (Press corresponding number and hit enter)"<<endl;
cout << "1. Insert items into a priority queue."<<endl;
cout << "2. Print out priority queue."<<endl;
cout << "3. Remove an item with a specific location."<<endl;
cout << "4. Find item with minimum key."<<endl;
cout << "5. Decrease Key."<<endl;
cout << "6. Build Priority Queue." << endl;
cout << "7. Exit."<<endl;
cout << "Choie: ";
cin >> choi;
if(choi == '1')
{
int key;
char element[255];
cout << "Key: ";
cin >> key;
cout << "Elem: ";
cin >> element;
pq.insertItem(key, element);
}
else if(choi == '2')