스택
다음과 같이 쓸 수 있다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
stack<int> st;
st.push(3);
st.push(4);
st.pop();
cout << st.size() << '\n';
cout << st.top();
}
파이썬과의 차이점이 있다면, pop() 연산자는 그 값을 반환하지 않는다. 그냥 말 그대로 pop하고 사라진다.
큐
다음과 같이 쓸 수 있다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
queue<int> Q;
Q.push(3);
Q.push(2);
Q.pop();
Q.push(4);
cout << Q.back() << '\n';
cout << Q.front() << '\n';
cout << Q.size() << '\n';
}
덱
다음과 같이 쓸 수 있다.
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
deque<int> dq;
dq.push_back(1);
dq.push_front(2);
dq.pop_front();
dq.pop_back();
cout << dq.size() << '\n';
dq.push_back(4);
dq.push_back(5);
cout << dq.front() << '\n';
cout << dq.back() << '\n';
}
사실 파이썬이랑 비슷해서 별 내용 없다.
꼭 자료구조들 안에 int형만 넣을 필요는 없다는 것을 명심해두자.
'C++ 같이 배워요' 카테고리의 다른 글
| [C++] Day 7 - 자료 구조(3) - 집합 자료 구조 (0) | 2025.07.27 |
|---|---|
| [C++] Day 5 - 자료 구조(1) - 배열과 벡터, 튜플 (2) | 2025.07.10 |
| [C++] Day 4 - 조건문 및 반복문 (0) | 2025.07.09 |
| [C++] Day 3 - 자료형 및 연산자 (0) | 2025.07.09 |
| [C++] Day 2 - 입력 및 출력 (0) | 2025.07.08 |