C++ 같이 배워요

[C++] Day 6 - 자료 구조(2) - 스택, 큐, 덱

kkigon 2025. 7. 10. 15:19

스택

다음과 같이 쓸 수 있다.

#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형만 넣을 필요는 없다는 것을 명심해두자.