어댑터
구성 요소의 인터페이스를 변경
어댑터 종류
- 컨테이너 어댑터 : Stack, Queue, Priority_Queue
- 반복자 어댑터: reverse_iterator, back_insert_iterator, front_insert_iterator, insert_iterator
- 함수 어댑터: 바인더(binder), 부정자(negator), 함수 포인터 어댑터
Stack 컨테이너
#include <iostream>
#include <stack>
using namespace std;
using namespace std;
int main() {
stack<int> st; //스택은 deque컨테이너를 가지고 만들어진 클래스
//어댑터는 기존의 컨테이너의 인터페이스 기능을 변경해서 만들어진 컨테이너(?)
st.push(10);
st.push(20);
st.push(30);
cout << st.top() << endl;
st.pop();
cout << st.top() << endl;
st.pop();
cout << st.top() << endl;
st.pop();
if (st.empty()) {
cout << "스택에 데이터 없음" << endl;
}
return 0;
}
출력
30
20
10
스택에 데이터 없음
Stack 클래스
클래스 타입 1개와 컨테이너 1개를 받는데 디폴트로 Deque 컨테이너로 구성된 클래스입니다.
Deque클래스의 구성 인터페이스를 개조(?)하여 만들어진게 Stack입니다.
Stack의 컨테이너를 Vector 컨테이너로 적용
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main() {
stack<int, vector<int>> st; //stack을 vector 컨테이너 구조의 어댑터로
st.push(10);
st.push(20);
st.push(30);
cout << st.top() << endl;
st.pop();
cout << st.top() << endl;
st.pop();
cout << st.top() << endl;
st.pop();
if (st.empty()) {
cout << "스택에 값이 없음" << endl;
}
return 0;
}
기본 Stack은 First-in-Last-Out 구조인데
이를 vector컨테이너를 적용시킬 수 있습니다.
하지만 출력에는 변함 없습니다.
'C++ > STL' 카테고리의 다른 글
[STL] 어댑터2 (0) | 2025.03.03 |
---|---|
[STL] 함수객체 (0) | 2025.03.03 |
[STL] 알고리즘 Algorithm (0) | 2025.03.03 |
[STL] 반복자 Iterator (0) | 2025.03.03 |
[STL] 컨테이너 ( container ) (0) | 2025.03.01 |