1. 전처리
#include<queue>
using namespace std;
2. 선언
queue<테이터형> Queue;
3. 삽입
값을 삽입한다.
Queue.push(데이터);
4. 참조
가장 먼저 추가된 데이터를 가져온다.
변수 = Queue.front();
5. 삭제
가장 먼저 추가된 데이터를 삭제한다.
Queue.pop();
6. 갯수
큐가 가지는 항목의 수를 가져온다.
변수 = Queue.size();
7. 확인
큐가 비어 있는지 검사한다.
false : 큐에 항목이 있음.
true : 큐에 항목이 없음.
#include<queue>
queue<int> Q //Q라는 이름의 int 요소들로 구성된 큐 선언
Q.push(값) //큐 Q에 값을 넣는다. 리턴 값이 없다.
Q.pop() //큐 Q의 front를 삭제한다. 리턴 값이 없다.
Q.front() //큐 Q의 front를 리턴한다. front는 삭제되지 않는다. (peek기능)
Q.back() //큐 Q의 back를 리턴한다. back는 삭제되지 않는다. (peek기능)
Q.size() //큐 Q의 크기(구성 요소 갯수)를 리턴한다.
Q.empty() //큐 Q가 비어있으면(요소가 없으면) 를 1(True)리턴하고 비어있지 않으면 0(False)를 리턴한다.
Queue.empty();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include<queue> using namespace std; int main( void ) { queue< char > Queue; Queue.push( 'A' ); Queue.push( 'B' ); Queue.push( 'C' ); cout << "Queue Size : " << Queue.size() << endl; cout << "Queue Items" << endl; while ( ! Queue.empty() ) { cout << Queue.front() << endl; Queue.pop(); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include<iostream> #include<queue> using namespace std; int main(){ queue< int > Q; cout << "size of queue: " << Q.size() << endl; cout << "Is queue empty?: " << Q.empty() << endl<<endl; Q.push(1); Q.push(2); Q.push(3); cout << "size of queue: " << Q.size() << endl; cout << "Is queue empty?: " << Q.empty() << endl << endl; cout << "Front of queue: " << Q.front() << endl; cout << "Back of queue: " << Q.back() << endl; cout << "size of queue: " << Q.size() << endl << endl; Q.pop(); cout << "size of queue after popped: " << Q.size() << endl; cout << "Front of queue: " << Q.front() << endl; return 0; } |
FIFO queue
queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the "back" of the specific container and popped from its "front".
The underlying container may be one of the standard container class template or some other specifically designed container class. This underlying container shall support at least the following operations:
- empty
- size
- front
- back
- push_back
- pop_front
The standard container classes deque and list fulfill these requirements. By default, if no container class is specified for a particular queue class instantiation, the standard container deque is used.
Template parameters
- T
- Type of the elements.
Aliased as member type queue::value_type. - Container
- Type of the internal underlying container object where the elements are stored.
Its value_type shall be T.
Aliased as member type queue::container_type.
Member types
Member functions
- (constructor)
- Construct queue (public member function )
- empty
- Test whether container is empty (public member function )
- size
- Return size (public member function )
- front
- Access next element (public member function )
- back
- Access last element (public member function )
- push
- Insert element (public member function )
- emplace
- Construct and insert element (public member function )
- pop
- Remove next element (public member function )
- swap
- Swap contents (public member function )
Non-member function overloads
- relational operators
- Relational operators for queue (function )
- swap (queue)
- Exchange contents of queues (public member function )
Non-member class specializations
- uses_allocator<queue>
- Uses allocator for queue (class template )
'Programming > C&C++' 카테고리의 다른 글
boost 컴파일 에러 fatal error C1001 (0) | 2018.01.18 |
---|---|
[C++] inet_addr, htons 함수 (0) | 2017.06.28 |
현재 시간 millisecond(밀리세컨드)까지 가져오기 (0) | 2017.06.27 |
C++ string 정리 (C++ 문자열) (0) | 2017.06.26 |
VS2013 멀티바이트(MultiByte) 설정 사용하기 위한 준비단계 (0) | 2017.05.19 |