관리 메뉴

Kim's Programming

STL(Standard Template Library) - Container - Queue 본문

STL - Container

STL(Standard Template Library) - Container - Queue

Programmer. 2016. 1. 27. 22:01

큐는 선입선출의 자료구조로 먼저 들어온 데이터가 제일 먼저 출력되는 구조의 자료구조입니다.


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<iostream>
#include<queue>
 
void print(std::queue<int> Print_Queue)
{
    while (!Print_Queue.empty())
    {
        std::cout << Print_Queue.front()<<" ";
        Print_Queue.pop();
    }
    std::cout << std::endl;
}
 
void main()
{
    //Queue doesn't have Iterator
    std::queue<int> Queue;
    std::queue<int> Queue_Cpoy;
    
 
    //if Queueis empty, empty() function return TRUE
    if (Queue.empty())
    {
        std::cout << "Queue is Empty" << std::endl;
    }
 
    //push(x) function writes 'x' to the rear of this Queue (FIFO)
    Queue.push(3);
    Queue.push(7);
    Queue.push(1);
    Queue.push(3);
    Queue.push(4);
    Queue.push(5);
    Queue.push(9);
    print(Queue);
 
    
    //'=' operator can copy to another Queue, prvious data will be deleted
    Queue_Cpoy = Queue;
    print(Queue_Cpoy);
 
    //back() function read data from rear of this Queue
    std::cout <<"Rear data ->" <<Queue.back() << std::endl;
 
    //front() function read data from front of this Queue
    std::cout << "Front data ->" << Queue.front() << std::endl;
    
    //pop() function remove data from front of this Queue (FIFO)
    print(Queue);
    Queue.pop();
    print(Queue);
 
    //size() function display this Queue's size
    std::cout <<"The Size of Queue : "<< Queue.size() << std::endl;
 
    //x1.swap(x2) function swap 'x1' and 'x2'
    Queue.swap(Queue_Cpoy);
    print(Queue);
    print(Queue_Cpoy);
    
    //emplace(x) function writes 'x' to rear of this Queue 
    Queue.emplace(11);
    print(Queue);
}
cs


각 함수들의 기능들을 알아보겠습니다.


    1. empty()

      큐가 비어있는지 확인합니다. 비어있으면 true를 비어있지 않으면 false를 리턴합니다.

    2. push(x)

      큐 뒤쪽으로 데이터 x를 씁니다.

    3. Operator =

      a=b인경우 큐 b의 데이터를 큐 a에 모두 넣습니다. 기존의 큐a의 데이터는 삭제됩니다.

    4. back()

      큐에서 큐 가장 마지막 데이터를 리턴합니다.

    5. front()

      규에서 가장 앞에 있는 데이터를 리턴합니다.

    6. pop()

      큐에서 가장 앞에 있는 데이터를 삭제합니다.

    7. size()

      현재 큐의 사이즈를 리턴합니다.

    8. swap()

      a.swap(b)인 경우 큐 a와 큐 b의 내용을 완전히 교체합니다.

    9. emplace(x)

      x를 큐의 뒤쪽에 넣습니다.