아래 샘플 코드는 Visual C++에서 , queue::pop
, queue::empty
, queue::front
queue::back
및 queue::size
STL 함수를 사용하는 queue::push
방법을 보여 줍니다. 이 문서의 정보는 관리되지 않는 Visual C++ 코드에만 적용됩니다.
원래 제품 버전: Visual C++
원래 KB 번호: 157622
요약
queue
어댑터는 에서 지원하는 컨테이너 형식으로 정의된 형식의 개체를 보유합니다queue
. 지원되는 두 컨테이너는 다음과 deque
같습니다list
. 개체는 에 의해 push()
삽입되고 제거됩니다 pop()
. front()
는 FIFO(FIFO라고도 함)에서 queue
가장 오래된 항목을 반환하고 back()
에 삽입된 queue
최신 항목을 반환합니다.
필수 헤더
<queue>
프로토타입
queue::push();
queue::pop();
queue::empty();
queue::back();
queue::front();
queue::size();
참고 항목
프로토타입의 클래스 또는 매개 변수 이름이 헤더 파일의 버전과 일치하지 않을 수 있습니다. 일부는 가독성을 개선하기 위해 수정되었습니다.
샘플 코드
샘플은 사용 및 deque
컨테이너를 사용하는 list
큐 구현을 보여 줍니다.
//////////////////////////////////////////////////////////////////////
// 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();
}
}
프로그램 출력
201
size of q is:4
42
100
49
201
horse
size of p is:5
cat
ape
dog
mouse
horse