国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++stack模板類

2019-11-06 07:48:44
字體:
來源:轉載
供稿:網友

stack 介紹

棧是一種容器適配器,特別為后入先出而設計的一種(LIFO ),那種數據被插入,然后再容器末端取出棧實現了容器適配器,這是用了一個封裝了的類作為他的特定容器,提供了一組成員函數去訪問他的元素,元素從特定的容器,也就是堆棧的頭取出袁術。

這個基礎的容器可能是任何標準的容器類,和一些其他特殊設計的模板類,唯一的要求就是要支持一下的操作

[cpp] view plain copy<span style="font-size:16px;"><strong>?</strong>back()   ?push_back()   ?pop_back()</span>   

 

因此,標準的容器類模板vector, deque 和list可以使用,默認情況下,如果沒有容器類被指定成為一個提別的stack 類,標準的容器類模板就是deque 隊列。

//常用模型stackr<int,vector<int>>;stack<int,deque<int>>;stack<int,list<int>>;//默認stack<int,deque<int>>;

實現C++  STL,棧有兩個參數。

template < class T, class Container = deque<T> > class stack;

參數示意:

T: 元素類型Container: 被用于存儲和訪問元素的的類型

成員函數

stack::stack

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

用于構造一個棧適配器對象。

ctnrContainer objectContainer is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class description).[cpp] view plain copy// test_stack.cpp : 定義控制臺應用程序的入口點。  //    #include "stdafx.h"  #include <stack>  #include <vector>  #include <deque>  #include <iostream>    using namespace std;    int _tmain(int argc, _TCHAR* argv[])  {      deque<int> mydeque(2,100);      vector<int> myvector(2,200);        stack<int> first;      stack<int> second(mydeque);        stack<int,vector<int> > third;      stack<int,vector<int> > fourth(myvector);        cout << "size of first: " << (int) first.size() << endl;      cout << "size of second: " << (int) second.size() << endl;      cout << "size of third: " << (int) third.size() << endl;      cout << "size of fourth: " << (int) fourth.size() << endl;          return 0;  }  

output:

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

stack::empty

bool empty ( ) const;

判斷是否為空。

Return Value

true if the container size is 0false otherwise.

[cpp] view plain copy// stack::empty  #include <iostream>  #include <stack>  using namespace std;    int main ()  {    stack<int> mystack;    int sum (0);      for (int i=1;i<=10;i++) mystack.push(i);      while (!mystack.empty())    {       sum += mystack.top();       mystack.pop();    }      cout << "total: " << sum << endl;        return 0;  }  

Output:

total: 55

stack::pop

void pop ( );

在棧的頂部移除元素。

 

[cpp] view plain copy // stack::push/pop  #include <iostream>  #include <stack>  using namespace std;    int main ()  {    stack<int> mystack;      for (int i=0; i<5; ++i) mystack.push(i);      cout << "Popping out elements...";    while (!mystack.empty())    {       cout << " " << mystack.top();       mystack.pop();    }    cout << endl;      return 0;  }  

 

Output:

Popping out elements... 4 3 2 1 0

 

stack::push

void push ( const T& x );

在棧頂添加元素

[cpp] view plain copy // stack::push/pop  #include <iostream>  #include <stack>  using namespace std;    int main ()  {    stack<int> mystack;      for (int i=0; i<5; ++i) mystack.push(i);      cout << "Popping out elements...";    while (!mystack.empty())    {       cout << " " << mystack.top();       mystack.pop();    }    cout << endl;      return 0;  }  

Output:

Popping out elements... 4 3 2 1 0

stack::size

 
size_type size ( ) const;

計算棧對象元素個數

 

[cpp] view plain copy // stack::size  #include <iostream>  #include <stack>  using namespace std;    int main ()  {    stack<int> myints;    cout << "0. size: " << (int) myints.size() << endl;      for (int i=0; i<5; i++) myints.push(i);    cout << "1. size: " << (int) myints.size() << endl;      myints.pop();    cout << "2. size: " << (int) myints.size() << endl;      return 0;  }  

Output:

0. size: 01. size: 52. size: 4

stack::top

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

返回棧頂元素

[cpp] view plain copy// test_stack.cpp : 定義控制臺應用程序的入口點。  //    #include "stdafx.h"  #include <stack>  #include <vector>  #include <deque>  #include <iostream>    using namespace std;    int _tmain(int argc, _TCHAR* argv[])  {      stack<int> mystack;      mystack.push(10);      mystack.push(20);      mystack.top()-=5;      cout << "mystack.top() is now " << mystack.top() << endl;        return 0;  }  

Output:

mystack.top() is now 15

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 永顺县| 松阳县| 六枝特区| 双辽市| 翼城县| 阜宁县| 额济纳旗| 克拉玛依市| 江安县| 遂昌县| 蓬莱市| 北安市| 济宁市| 滕州市| 神农架林区| 绥芬河市| 沙河市| 海淀区| 古蔺县| 涞源县| 合肥市| 永靖县| 南木林县| 连南| 石棉县| 永胜县| 柳江县| 黄龙县| 三台县| 朝阳市| 化州市| 呼和浩特市| 彭山县| 海安县| 湄潭县| 特克斯县| 上虞市| 富民县| 昭平县| 浠水县| 平顶山市|