玩命加载中 . . .

STL-deque


deque

push_front(): 从头部插入
pop_front(): 从头部弹出
template<typename T>
void info(const deque<T>& q) {
    for (auto& x : q) {
        cout << x << " ";
    }
    cout << endl;
}

void disp(const deque<int>& q) {
    for (auto it = q.begin(); it != q.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}

int main(int argc, char const *argv[])
{
    deque<int> q;
    q.push_back(10);
    q.push_back(11);
    q.push_back(12);
    q.push_front(9);
    info(q);

    q.pop_back();
    q.pop_front();
    disp(q);
    return 0;
}
9 10 11 12 
10 11 

栈和队列

#include <iostream>
#include <stack>
#include <queue>
using namespace std;

int main(int argc, char const *argv[]) {
    stack<int> st;
    st.push(12);
    st.push(34);
    st.push(56);
    while (!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;

    queue<int> q;
    q.push(14);
    q.push(25);
    q.push(36);
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;

    return 0;
}
56 34 12 // 先进后出
14 25 36 // 先进先出

优先队列

priority_queue<int> q;
q.push(14);
q.push(25);
q.push(36);
while (!q.empty()) {
    cout << q.top() << " ";
    q.pop();
}
cout << endl;
36 25 14

文章作者: kunpeng
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 kunpeng !
  目录