'Stack(스택)'에 해당되는 글 1건

  1. 2020.12.16 STL 컨테이너 어댑터

STL 컨테이너 어댑터

C++ 2020. 12. 16. 21:22

STL 컨테이너 어댑터

- STL 컨테이너 어댑터 라이브러리는 매우 활용도가 높은 자료구조를 제공

- 기존의 C언어를 이용하면 구현하기 까다로웠던 다양한 자료구조를 손쉽게 이용할 수 있음

 

Stack(스택), Queue(큐), Priority Queue(우선순위 큐)

 

STL 컨테이너 어댑터: 스택

- C++ Stack STL은 다음과 같은 함수로 구성

 

추가 : push(원소)

삭제 : pop()

조회 : top()

검사 : empty() / size()

 

#include <iostream>
#include <string>
#include <stdio.h>
#include <stack>

using namespace std;

int main(void) {
    stack<int> s;
    s.push(7); s.push(5); s.push(4); s.pop(); s.push(6); s.pop();
    while (!s.empty()) { // 스택에 원소가 하나라도 남아 있는 경우 실행
        cout << s.top() << ' ';
        s.pop();
    }
    system("pause");
}

 

STL 컨테이너 어댑터: 큐

- C++ Stack STL은 다음과 같은 함수로 구성

 

추가 : push(원소)

삭제 : pop()

조회 : front() / back()

검사 : empty() / size()

 

#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>

using namespace std;

int main(void) {
    queue<int> q;
    q.push(7); q.push(5); q.push(4); q.pop(); q.push(6); q.pop();
    while (!q.empty()) {
        cout << q.front() << ' ';
        q.pop();
    }
    system("pause");
}

 

STL 컨테이너 어댑터: 우선순위 큐

- 큐 라이브러리에 포함

#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>

using namespace std;

int main(void) {
    int n, x;
    cin >> n;
    priority_queue<int> pq;
    for (int i = 0; i < n; i++) { cin >> x; pq.push(x); }
    while (!pq.empty()) {
        cout << pq.top() << ' ';
        pq.pop();
    }
    system("pause");
}

'C++' 카테고리의 다른 글

STL 연관 컨테이너  (0) 2020.12.16
STL 시퀀스 컨테이너  (0) 2020.12.16
스마트 포인터  (0) 2020.12.16
템플릿  (0) 2020.12.16
다형성 기법  (0) 2020.12.16
Posted by khon98
,