Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Dit artikel helpt u bij het oplossen van het probleem waarbij u de priority_queue::pushfuncties , priority_queue::pop, priority_queue::emptyen priority_queue::size priority_queue::topSTL gebruikt in Visual C++.
Oorspronkelijke productversie: Visual C++
Oorspronkelijk KB-nummer: 157623
Samenvatting
In de onderstaande voorbeeldcode ziet u hoe u de priority_queue::pushfuncties , priority_queue::pop, en priority_queue::size priority_queue::emptypriority_queue::topSTL gebruikt in Visual C++.
De priority_queue adapter bevat objecten van het type dat is gedefinieerd door het type container dat wordt ondersteund door de priority_queue. De twee ondersteunde containers zijn de vector en de deque. Objecten worden ingevoegd door push() en verwijderd door pop(). top() retourneert het bovenste item in de priority_queue.
Omdat adapters iteratie niet ondersteunen, heeft een priority_queue iterator geen gekoppelde iterator.
Priority_queue hiermee kunt u een gesorteerde verzameling items onderhouden die worden bepaald door een bijbehorende comparatorfunctie, zoals minder, groter, enzovoort. Het bovenste item wordt daarom de kandidaat van keuze, laagste of hoogste op basis van de gekozen functie.
Vereiste header
<queue>
Prototype
priority_queue::push();
priority_queue::pop();
priority_queue::empty();
priority_queue::top();
priority_queue::size();
Notitie
De namen van klassen/parameters in het prototype komen mogelijk niet overeen met de versie in het headerbestand. Sme is gewijzigd om de leesbaarheid te verbeteren.
Voorbeeldcode
In het voorbeeld ziet u priority_queueimplementatie met behulp van deque en vector containers.
//////////////////////////////////////////////////////////////////////
// Compile options needed: /GX
// <filename> : priority_queue.cpp
// Functions:
// priority_queue::push(), priority_queue::pop(),
// priority_queue::empty(), priority_queue::top(), queue::size()
// of Microsoft Product Support Services,
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
#include <iostream>
#include <queue>
#include <deque>
#include <vector>
#include <functional>
using namespace std;
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
// Using priority_queue with deque
// Use of function greater sorts the items in ascending order
typedef deque<int, allocator<int> > INTDQU;
typedef priority_queue<int,INTDQU, greater<int> > INTPRQUE;
// Using priority_queue with vector
// Use of function less sorts the items in descending order
typedef vector<char, allocator<char> > CHVECTOR;
typedef priority_queue<char,CHVECTOR,less<char> > CHPRQUE;
void main(void)
{
int size_q;
INTPRQUE q;
CHPRQUE p;
// Insert items in the priority_queue(uses deque)
q.push(42);
q.push(100);
q.push(49);
q.push(201);
// Output the item at the top using top()
cout << q.top() << endl;
// Output the size of priority_queue
size_q = q.size();
cout << "size of q is:" << size_q << endl;
// Output items in priority_queue using top()
// and use pop() to get to next item until
// priority_queue is empty
while (!q.empty())
{
cout << q.top() << endl;
q.pop();
}
// Insert items in the priority_queue(uses vector)
p.push('c');
p.push('a');
p.push('d');
p.push('m');
p.push('h');
// Output the item at the top using top()
cout << p.top() << endl;
// Output the size of priority_queue
size_q = p.size();
cout << "size of p is:" << size_q << endl;
// Output items in `priority_queue`using top()
// and use pop() to get to next item until
// `priority_queue`is empty
while (!p.empty())
{
cout << p.top() << endl;
p.pop();
}
}
Programma-uitvoer:
4
size of q is:4
42
49
100
201
m
size of p is:5
m
h
d
c
a