Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
Exempelkoden nedan visar hur du använder queue::push
funktionerna , queue::pop
, queue::empty
, queue::back
, queue::front
och queue::size
STL i Visual C++. Informationen i den här artikeln gäller endast ohanterad Visual C++-kod.
Ursprunglig produktversion: Visual C++
Ursprungligt KB-nummer: 157622
Sammanfattning
Adaptern queue
innehåller objekt av den typ som definieras av den typ av container som stöds av queue
. De två containrar som stöds är list
och deque
. Objekt infogas av push()
och tas bort av pop()
. front()
returnerar det äldsta objektet i queue
(även kallat FIFO) och back()
returnerar det senaste objektet som infogats queue
i .
Obligatoriskt huvud
<queue>
Prototyper
queue::push();
queue::pop();
queue::empty();
queue::back();
queue::front();
queue::size();
Kommentar
Klass- eller parameternamnen i prototyperna kanske inte matchar versionen i huvudfilen. Vissa har ändrats för att förbättra läsbarheten.
Exempelkod
Exemplet visar köimplementering med hjälp av list
och deque
containrar.
//////////////////////////////////////////////////////////////////////
// Compile options needed: none
// <filename> : queue.cpp
// Functions:
// queue::push(), queue::pop(), queue::empty(), queue::back(),
// queue::front(),queue::size()
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
//////////////////////////////////////////////////////////////////////
/* Compile options needed: /GX */
#include <list>
#include <iostream>
#include <queue>
#include <deque>
using namespace std;
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
// Using queue with list
typedef list<int, allocator<int>> INTLIST;
typedef queue<int, INTLIST> INTQUEUE;
// Using queue with deque
typedef deque<char *, allocator<char *>> CHARDEQUE;
typedef queue<char *, CHARDEQUE> CHARQUEUE;
void main(void)
{
int size_q;
INTQUEUE q;
CHARQUEUE p;
// Insert items in the queue(uses list)
q.push(42);
q.push(100);
q.push(49);
q.push(201);
// Output the item inserted last using back()
cout << q.back() << endl;
// Output the size of queue
size_q = q.size();
cout << "size of q is:" << size_q << endl;
// Output items in queue using front()
// and use pop() to get to next item until
// queue is empty
while (!q.empty())
{
cout << q.front() << endl;
q.pop();
}
// Insert items in the queue(uses deque)
p.push("cat");
p.push("ape");
p.push("dog");
p.push("mouse");
p.push("horse");
// Output the item inserted last using back()
cout << p.back() << endl;
// Output the size of queue
size_q = p.size();
cout << "size of p is:" << size_q << endl;
// Output items in queue using front()
// and use pop() to get to next item until
// queue is empty
while (!p.empty())
{
cout << p.front() << endl;
p.pop();
}
}
Programmets utdata
201
size of q is:4
42
100
49
201
horse
size of p is:5
cat
ape
dog
mouse
horse
Referenser
Samma information om medlemsfunktioner i STL-klassen queue
finns i kön.