Hinweis
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, sich anzumelden oder das Verzeichnis zu wechseln.
Für den Zugriff auf diese Seite ist eine Autorisierung erforderlich. Sie können versuchen, das Verzeichnis zu wechseln.
Der folgende Beispielcode veranschaulicht, wie die queue::push
Funktionen , queue::pop
, , queue::empty
, queue::back
, queue::front
und queue::size
STL in Visual C++ verwendet werden. Die Informationen in diesem Artikel gelten nur für nicht verwalteten Visual C++-Code.
Originalproduktversion: Visual C++
Ursprüngliche KB-Nummer: 157622
Zusammenfassung
Der queue
Adapter enthält Objekte des Typs, der durch den Typ des containers definiert wird, der von der queue
. Die beiden unterstützten Container sind die list
und die deque
. Objekte werden von push()
und entfernt von pop()
. front()
gibt das älteste Element im queue
(auch als FIFO bezeichnet) zurück und back()
gibt das neueste Element zurück, das in der queue
.
Erforderlicher Header
<queue>
Prototypen
queue::push();
queue::pop();
queue::empty();
queue::back();
queue::front();
queue::size();
Notiz
Die Klassen- oder Parameternamen in den Prototypen stimmen möglicherweise nicht mit der Version in der Headerdatei überein. Einige wurden geändert, um die Lesbarkeit zu verbessern.
Beispielcode
Das Beispiel zeigt die Warteschlangenimplementierung mithilfe list
und deque
Container.
//////////////////////////////////////////////////////////////////////
// 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();
}
}
Programmausgabe
201
size of q is:4
42
100
49
201
horse
size of p is:5
cat
ape
dog
mouse
horse
References
Informationen zu Memberfunktionen der STL-Klasse queue
finden Sie in der Warteschlange.