STL中stack和queue的用法

來源:互聯網
上載者:User

發現在平常的編碼中,stack和queue用到的頻率都非常高,自己寫一個stack和queue又顯得麻煩,今天特地研究如何使用STL中的stack和queue。研究的並不輸入,只是一些簡單用法而已。

下面附上我的stack和queue使用代碼

#include<iostream><br />#include<stack><br />#include<queue><br />#include<stdlib.h>//system("pause");需要用到<br />using namespace std;<br />int main()<br />{<br />stack<int> s;<br />queue<int> q;<br />int arry[4]={1,2,3,4};<br />for(int i=0;i<4;i++)<br />{<br />s.push(arry[i]);<br />q.push(arry[i]);<br />}<br />//輸出棧中元素<br />cout<<"棧長度為:"<<s.size()<<endl;<br />for(int i=0;i<4;i++)<br />{<br />cout<<s.top()<<" ";<br />s.pop();<br />}<br />cout<<endl;<br />//輸出隊列中元素<br />cout<<"隊列長度為:"<<q.size()<<endl;<br />for(int i=0;i<4;i++)<br />{<br />cout<<q.front()<<" ";<br />q.pop();<br />}<br />cout<<endl;<br />/*int i;<br />cin>>i;*/<br />system("pause");<br />return 0;<br />} 

ps:

在VS2010中按CTRL+F5的時候命令列一閃而過,並沒有以前“請按任意鍵繼續. . .”這樣的提示了。

在網上查詢,最後得出的解決方案是加入stdlib.h的標頭檔,如下所示:

#include<stdlib.h>//system("pause");需要用到 

然後在程式最後加上system("pause");,如果有return的話要寫在return之前。如下所示:

system("pause");<br />return 0; 

還有一種解決命令列一閃而沒 方法,請參考

http://hi.baidu.com/liuguoyuhot/blog/item/f58f64edffae4a2826979108.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.