본문 바로가기
Develop/Cpp

[Cpp] STL (스택, 큐, 우선순위 큐)

by Tarra 2022. 6. 22.

 

 


개인 공부 후 자료를 남겨놓기 위한 목적이므로,
생략되거나 오류가 있을 수 있음을 알립니다.

 

 

Cpp에서 사용하는 STL중 스택, 큐, 우선순위 큐를 간단히 적어보았다.

 

STL


stack(스택)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <stack>
using namespace std;
 
int main() {
    
    stack<int> t;
    // 스택 생성 
 
    t.push(1);
    t.push(2);
    t.push(3);
    
    cout << t.top();
    t.pop();
    
    return 0;
}
cs

 

 

 

 


Queue (큐)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <queue>
using namespace std;
 
int main() {
 
    queue<int> q;
    // 큐 생성
 
    q.push(1);
    q.push(2);
    q.push(3);
    
    cout << q.front();
    q.pop();
    
    return 0;
}
cs

 

 

 


우선순위 큐 (priority queue)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <queue>
using namespace std;
 
int main() {
 
    priority_queue<int> q
    // 우선순위 큐 생성    
 
    q.push(1);
    q.push(3);
    q.push(2);
    q.push(6);
   
    cout << q.top();  // 가장 큰 값이 나옴.
    q.pop();
        
    return 0;
}
cs

 

 

 

'Develop > Cpp' 카테고리의 다른 글

[Cpp] Direct 배열  (0) 2022.06.22
[Cpp] 문자열 함수 (cstring)  (0) 2022.06.22
[Cpp] 구조체  (0) 2022.06.21
[Cpp] 함수 호출 방식 (Call by value, Call by reference)  (0) 2022.06.20
[Cpp] 포인터 기본  (0) 2022.06.20