Chapter 3 | Stacks and Queues

stack 、queue、priority_queue(stl)

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
using namespace std;
stack<int>s;

void _stack(){
    for(int i = 0 ; i <= 4; i++)
        s.push(i);
    cout <<s.top()<<endl;
    s.emplace(233);//對于在容器中添加類的對象時, 相比于push_back,emplace_back可以避免額外類的復(fù)制和移動操作.
    cout <<s.top()<<endl;

}
void _queue(){
    queue<int>q;
    q.push(1);
    if(!q.empty()){
        cout << q.front() <<endl;
        q.pop();
    }
}
void p_q(){
    priority_queue<int> bigtosmall;
    priority_queue<int,vector<int>,greater<int> >smalltobig;
    for(int i = 1; i <= 10 ; i++){
        bigtosmall.push(i);
        smalltobig.push(i);
    }
    while(!bigtosmall.empty()){
        cout << bigtosmall.top() <<"   ";

        bigtosmall.pop();
    }
    cout <<endl;
    while(!smalltobig.empty()){
        cout << smalltobig.top() <<"   ";
        smalltobig.pop();
    }
    cout <<endl;
}
int main(){
    _stack();
    _queue();
    p_q();
}

單調(diào)棧

實現(xiàn)一個棧,除了push和pop操作,還要實現(xiàn)min函數(shù)以返回棧中的最小值。 push,pop和min函數(shù)的時間復(fù)雜度都為O(1)。

維護一個單調(diào)遞減的棧,所棧首就是當(dāng)前的最小值,pop的時候如果單調(diào)棧首的序號是這個,那也要pop這個單調(diào)棧,數(shù)值可能重復(fù),所以單調(diào)棧維護的是同一個值的最小序號。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,665評論 1 32
  • 1.設(shè)計模式是什么? 你知道哪些設(shè)計模式,并簡要敘述?設(shè)計模式是一種編碼經(jīng)驗,就是用比較成熟的邏輯去處理某一種類型...
    龍飝閱讀 2,302評論 0 12
  • 作者:同夢奇緣鏈接:https://segmentfault.com/a/1190000017905515 一、認(rèn)...
    grain先森閱讀 14,526評論 0 14
  • 昨天由于春節(jié)臨近,家家都在做衛(wèi)生,我也不另外,白天在店里邊干活邊做衛(wèi)生,晚上8:30回婆婆住的地方,我也在想完成功...
    王澤華wzh閱讀 147評論 0 0
  • 夢想在什么地方 總是那么令人向往 我不顧一切走在路上 就是為了來到你的身旁 夢想,有多大多遠(yuǎn),它在哪里 遠(yuǎn)方,長路...
    浩宇小丸子閱讀 384評論 2 4

友情鏈接更多精彩內(nèi)容