1. 程式人生 > >STL中stack與queue庫函式 的使用方法

STL中stack與queue庫函式 的使用方法

 1、stack
stack 模板類的定義在<stack>標頭檔案中。
stack 模板類需要兩個模板引數,一個是元素型別,一個容器型別,但只有元素型別是必要
的,在不指定容器型別時,預設的容器型別為deque。
定義stack 物件的示例程式碼如下:
stack<int> s1;
stack<string> s2;
stack 的基本操作有:
入棧,如例:s.push(x);
出棧,如例:s.pop();注意,出棧操作只是刪除棧頂元素,並不返回該元素。
訪問棧頂,如例:s.top()
判斷棧空,如例:s.empty(),當棧空時,返回true。
訪問棧中的元素個數,如例:s.size()。

成員函式

stack::stack

explicit stack ( const Container& ctnr = Container() );

用於構造一個棧介面卡物件。

ctnr
Container object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class description).
  1. // test_stack.cpp : 定義控制檯應用程式的入口點。
  2. //
  3. #include "stdafx.h"
  4. #include <stack>
  5. #include <vector>
  6. #include <deque>
  7. #include <iostream>
  8. usingnamespace std;  
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     deque<int> mydeque(2,100);  
  12.     vector<int> myvector(2,200);  
  13.     stack<int> first;  
  14.     stack<int
    > second(mydeque);  
  15.     stack<int,vector<int> > third;  
  16.     stack<int,vector<int> > fourth(myvector);  
  17.     cout << "size of first: " << (int) first.size() << endl;  
  18.     cout << "size of second: " << (int) second.size() << endl;  
  19.     cout << "size of third: " << (int) third.size() << endl;  
  20.     cout << "size of fourth: " << (int) fourth.size() << endl;  
  21.     return 0;  
  22. }  

output:

size of first: 0
size of second: 3
size of third: 0
size of fourth: 2

stack::empty

bool empty ( ) const;

判斷是否為空。

Return Value

true if the container size is 0false otherwise.

  1. // stack::empty
  2. #include <iostream>
  3. #include <stack>
  4. usingnamespace std;  
  5. int main ()  
  6. {  
  7.   stack<int> mystack;  
  8.   int sum (0);  
  9.   for (int i=1;i<=10;i++) mystack.push(i);  
  10.   while (!mystack.empty())  
  11.   {  
  12.      sum += mystack.top();  
  13.      mystack.pop();  
  14.   }  
  15.   cout << "total: " << sum << endl;  
  16.   return 0;  
  17. }  

Output:

total: 55

stack::pop

void pop ( );

在棧的頂部移除元素。

  1. // stack::push/pop
  2. #include <iostream>
  3. #include <stack>
  4. usingnamespace std;  
  5. int main ()  
  6. {  
  7.   stack<int> mystack;  
  8.   for (int i=0; i<5; ++i) mystack.push(i);  
  9.   cout << "Popping out elements...";  
  10.   while (!mystack.empty())  
  11.   {  
  12.      cout << " " << mystack.top();  
  13.      mystack.pop();  
  14.   }  
  15.   cout << endl;  
  16.   return 0;  
  17. }  

Output:

Popping out elements... 4 3 2 1 0

stack::push

void push ( const T& x );

在棧頂新增元素

  1. // stack::push/pop
  2. #include <iostream>
  3. #include <stack>
  4. usingnamespace std;  
  5. int main ()  
  6. {  
  7.   stack<int> mystack;  
  8.   for (int i=0; i<5; ++i) mystack.push(i);  
  9.   cout << "Popping out elements...";  
  10.   while (!mystack.empty())  
  11.   {  
  12.      cout << " " << mystack.top();  
  13.      mystack.pop();  
  14.   }  
  15.   cout << endl;  
  16.   return 0;  
  17. }  

Output:

Popping out elements... 4 3 2 1 0

stack::size

size_type size ( ) const;

計算棧物件元素個數

  1. // stack::size
  2. #include <iostream>
  3. #include <stack>
  4. usingnamespace std;  
  5. int main ()  
  6. {  
  7.   stack<int> myints;  
  8.   cout << "0. size: " << (int) myints.size() << endl;  
  9.   for (int i=0; i<5; i++) myints.push(i);  
  10.   cout << "1. size: " << (int) myints.size() << endl;  
  11.   myints.pop();  
  12.   cout << "2. size: " << (int) myints.size() << endl;  
  13.   return 0;  
  14. }  



Output:

0. size: 0
1. size: 5
2. size: 4

stack::top

      value_type& top ( );
const value_type& top ( ) const;

返回棧頂元素

  1. // test_stack.cpp : 定義控制檯應用程式的入口點。
  2. //
  3. #include "stdafx.h"
  4. #include <stack>
  5. #include <vector>
  6. #include <deque>
  7. #include <iostream>
  8. usingnamespace std;  
  9. int _tmain(int argc, _TCHAR* argv[])  
  10. {  
  11.     stack<int> mystack;  
  12.     mystack.push(10);  
  13.     mystack.push(20);  
  14.     mystack.top()-=5;  
  15.     cout << "mystack.top() is now " << mystack.top() << endl;  
  16.     return 0;  
  17. }  

Output:

mystack.top() is now 15

2、queue
queue 模板類的定義在<queue>標頭檔案中。
與stack 模板類很相似,queue 模板類也需要兩個模板引數,一個是元素型別,一個容器類
型,元素型別是必要的,容器型別是可選的,預設為deque 型別。
定義queue 物件的示例程式碼如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
入隊,如例:q.push(x); 將x 接到佇列的末端。
出隊,如例:q.pop(); 彈出佇列的第一個元素,注意,並不會返回被彈出元素的值。
訪問隊首元素,如例:q.front(),即最早被壓入佇列的元素。
訪問隊尾元素,如例:q.back(),即最後被壓入佇列的元素。
判斷佇列空,如例:q.empty(),當佇列空時,返回true。
訪問佇列中的元素個數,如例:q.size()

#include <cstdlib>
#include <iostream>
#include <queue>

using namespace std;

int main()
{
    int e,n,m;
    queue<int> q1;
    for(int i=0;i<10;i++)
       q1.push(i);
    if(!q1.empty())
    cout<<"dui lie  bu kong\n";
    n=q1.size();
    cout<<n<<endl;
    m=q1.back();
    cout<<m<<endl;
    for(int j=0;j<n;j++)
    {
       e=q1.front();
       cout<<e<<" ";
       q1.pop();
    }
    cout<<endl;
    if(q1.empty())
    cout<<"dui lie  bu kong\n";
    system("PAUSE");
    return 0;
}

3、priority_queue
在<queue>標頭檔案中,還定義了另一個非常有用的模板類priority_queue(優先佇列)。優先隊
列與佇列的差別在於優先佇列不是按照入隊的順序出隊,而是按照佇列中元素的優先權順序
出隊(預設為大者優先,也可以通過指定運算元來指定自己的優先順序)。
priority_queue 模板類有三個模板引數,第一個是元素型別,第二個容器型別,第三個是比
較運算元。其中後兩個都可以省略,預設容器為vector,預設運算元為less,即小的往前排,大
的往後排(出隊時序列尾的元素出隊)。
定義priority_queue 物件的示例程式碼如下:
priority_queue<int> q1;
priority_queue< pair<int, int> > q2; // 注意在兩個尖括號之間一定要留空格。
priority_queue<int, vector<int>, greater<int> > q3; // 定義小的先出隊
priority_queue 的基本操作與queue 相同。
初學者在使用priority_queue 時,最困難的可能就是如何定義比較運算元了。
如果是基本資料型別,或已定義了比較運算子的類,可以直接用STL 的less 運算元和greater
運算元——預設為使用less 運算元,即小的往前排,大的先出隊。
如果要定義自己的比較運算元,方法有多種,這裡介紹其中的一種:過載比較運算子。優先隊
列試圖將兩個元素x 和y 代入比較運算子(對less 運算元,呼叫x<y,對greater 運算元,呼叫x>y),
若結果為真,則x 排在y 前面,y 將先於x 出隊,反之,則將y 排在x 前面,x 將先出隊。
看下面這個簡單的示例:
#include <iostream>

#include <queue>
using namespace std;
class T
{
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator < (const T &t1, const T &t2)
{
return t1.z < t2.z; // 按照z 的順序來決定t1 和t2 的順序
}
main()
{
priority_queue<T> q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top(); q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}
輸出結果為(注意是按照z 的順序從大到小出隊的):
3 3 6
2 2 5
1 5 4
4 4 3
再看一個按照z 的順序從小到大出隊的例子:
#include <iostream>
#include <queue>
using namespace std;
class T
{
public:
int x, y, z;
T(int a, int b, int c):x(a), y(b), z(c)
{
}
};
bool operator > (const T &t1, const T &t2)
{
return t1.z > t2.z;
}
main()
{
priority_queue<T, vector<T>, greater<T> > q;
q.push(T(4,4,3));
q.push(T(2,2,5));
q.push(T(1,5,4));
q.push(T(3,3,6));
while (!q.empty())
{
T t = q.top(); q.pop();
cout << t.x << " " << t.y << " " << t.z << endl;
}
return 1;
}
輸出結果為:
4 4 3
1 5 4
2 2 5
3 3 6
如果我們把第一個例子中的比較運算子過載為:
bool operator < (const T &t1, const T &t2)
{
return t1.z > t2.z; // 按照z 的順序來決定t1 和t2 的順序
}
則第一個例子的程式會得到和第二個例子的程式相同的輸出結果。

相關推薦

STLstackqueue函式 的使用方法

 1、stack stack 模板類的定義在<stack>標頭檔案中。 stack 模板類需要兩個模板引數,一個是元素型別,一個容器型別,但只有元素型別是必要 的,在不指定容器型別時,預設的容器型別為deque。 定義stack 物件的示例程式碼如下: st

C++STL學習——stackqueue容器

stack容器 簡介 stack是一種堆疊容器,是一種"先進後出"的容器。 stack是簡單地裝飾deque容器而成為另外的一種容器 標頭檔案#include<stack> stack物件的預設構造 stack採用模板類實現, stack

c++ stackqueue的使用方法

轉自 https://wenku.baidu.com/view/7d66ae3a580216fc700afded.html stack(棧)和queue(佇列)也是在程式設計中經常會用到的資料容器,STL為我們提供了方便的stack(棧)的queue(佇列)的實現。 準確地

Mysql的字串拆分方法 (類似javasplit()PaAdmin的split_part()函式)

【原理】://fSELECT  SUBSTRING_INDEX('a,b,c,d,e,f',',',-1) ; -- f != 'a,b,c,d,e,f'//eSELECT  SUBSTRING_INDEX('a,b,c,d,e,f',',',-2) ; -- e,f !=

C++ STL佇列(queue)的使用方法

原文地址 基本操作: push(x) 將x壓入佇列的末端 pop() 彈出佇列的第一個元素(隊頂元素),注意此函式並不返回任何值 front() 返回第一個元素(隊頂元素) back() 返回最後被壓入的元素(隊尾元素) empty() 當佇列為空時,返回true

STL佇列(queue)的使用方法

STL 中優先佇列的使用方法(priority_queu) 優先佇列容器與佇列一樣,只能從隊尾插入元素,從隊首刪除元素。但是它有一個特性,就是佇列中最大的元素總是位於隊首,所以出隊時,並非按照先進先出的原則進行,而是將當前佇列中最大的元素出隊。這點類似於給佇列裡的元素進行了由大互小的順序排序。元素的比較規則

STL(七):stack queue

好久沒有寫過新的內容了。主要是最近真的沒有時間。 好吧,這次介紹一下棧與佇列的內容。 stack 先理一下之前寫過的容器: vector deque list 這些都是序列式容器,但是使用了不同的資料結構來實現。 棧的特性,就是先進後出(或者

StackQueue

Stack(棧) 堆疊(Stack)代表了一個後進先出的物件集合。當您需要對各項進行後進先出的訪問時,則使用堆疊。當您在列表中新增一項,稱為推入元素,當您從列表中移除一項時,稱為彈出元素。 方法 Clear(); 從 Stack 中移除所有的元素。 bool Contains( o

zcmu-4930: 堆疊的使用(stlstack的基本用法)

  4930: 堆疊的使用 Time Limit: 1 Sec  Memory Limit: 32 MB Submit: 63  Solved: 27 [Submit][Status][

Java的輸入和輸出、if...else if...else判斷、Java列印陣列、Java陣列排序、檢視函式方法的原始碼、命令列引數

Java的輸入和輸出: 輸入: import java.util.Scanner Scanner s = new Scanner(System.in); //通過new Scanner(System.in)建立一個Scanner物件,控制檯會一直等待輸入,直到敲回車鍵

一個程式包含C++ STLstack常見用法

下面是程式:  #include <iostream> #include <stack> using namespace std; int main() { //建立一個空

tf.stack()tf.unstack()函式

tf.stack()是一個矩陣拼接函式,即將秩為 R 的張量列表堆疊成一個秩為 (R+1) 的張量。  import tensorflow as tf a = tf.constant([[1,2,3],[4,5,6]]) b = tf.constant([[7,8,9],[

關於JavaStackQueue的一些api

關於Java中Queue的offer和add方法的區別 API中這樣說到: add():Inserts the specified element at the tail of this queu

面試常見的字串函式程式設計

下面對一些常見的關於字元的庫函式進行實現,這些也是通常面試中所問的一些問題,需要注意的是有些看起來很簡單,但是一定要考慮一些邊界條件,否則很容易出錯. strcpy實現 char* strcpy(char* dst,const char* src){

在CentOS 7安裝配置Tomcat-8方法

命令主要參考http://www.jb51.NET/os/RedHat/73032.html 安裝說明 安裝環境:CentOS-7安裝方式:原始碼安裝 軟體:apache-tomcat-8.0.14.tar.gz下載地址:http://tomcat.apache

ionic3引入第三方js方法總結

以jquery為例: 建立ionic3專案:ionic start ionic3-test tabs 一、在使用的頁面匯入js檔案路徑;         下載jquery.min.js檔案,找到assets下面,建立js資料夾,將下載好的jquer

STLstack詳解

stack Stacks are a type of container adaptor, specifically designed to operate in a LIFO context

CCF考試常用到的函式的操作

作為一個程式設計的初學者,在學校能夠參加的認證考試也就是隻有CCF認證考試了,而對於這個認證考試,一般套路固定,第三題多為字串操作的題目,第四題多為圖的相關演算法,基本廣搜和深搜都可解決一些問題,為了在考試時能更高效地編碼,在此對一些常用的庫函式做一些總結: 1.C++字

帶你深入理解STLStackQueue

上一篇部落格,帶你深入理解STL之Deque容器中詳細介紹了deque容器的原始碼實現方式。結合前面介紹的兩個容器vector和list,在使用的過程中,我們確實要知道在什麼情況下需要選擇恰當的容器來滿足需求和提升效率。一般選擇的準則有如下幾條: 如果需要隨

Linux CentOS 6.5安裝配置Tomcat-8方法

2. 下載jdk-8u20-linux-x64.rpm,執行rpm -ivh jdk-8u20-linux-x64.rpm安裝;第二步 安裝 tomcat  將apache-tomcat-8.0.0.RC3.tar.gz檔案上傳到/usr/local中執行以下操作:  複製程式碼程式碼如下: [[email&