STL源碼剖析 容器 stl_stack.h

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   

本文為senlie原創,轉載請保留此地址:http://blog.csdn.net/zhengsenlie


stack

-------------------------------------------------------------
stack 是一種配接器(adapter),以某種容器作為底部結構,改變其介面,使之符合"先進後出"的特性。
SGI STL 預設以 deque 為 stack 底部結構
沒有遍曆行為,沒有遍曆器
樣本:
#include <stack>#include <list>#include <iostream>#include <algorithm>using namespace std;int main(){stack<int, list<int> >istack;istack.push(1);istack.push(2);cout << istack.size() << endl << istack.top() << endl;}

源碼:
#ifndef __SGI_STL_INTERNAL_STACK_H#define __SGI_STL_INTERNAL_STACK_H__STL_BEGIN_NAMESPACE#ifndef __STL_LIMITED_DEFAULT_TEMPLATEStemplate <class T, class Sequence = deque<T> > //預設以 deque 為底層容器#elsetemplate <class T, class Sequence>#endifclass stack {  friend bool operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&);  friend bool operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&);public:  typedef typename Sequence::value_type value_type;  typedef typename Sequence::size_type size_type;  typedef typename Sequence::reference reference;  typedef typename Sequence::const_reference const_reference;protected:  Sequence c; //底層容器public:  //以下完全利用 Sequence c 的操作完成 stack 的操作  bool empty() const { return c.empty(); }  size_type size() const { return c.size(); }  reference top() { return c.back(); }  const_reference top() const { return c.back(); }  //修改介面使符合 stack "前進後出"的特性  void push(const value_type& x) { c.push_back(x); }  void pop() { c.pop_back(); }};template <class T, class Sequence>bool operator==(const stack<T, Sequence>& x, const stack<T, Sequence>& y) {  return x.c == y.c;}template <class T, class Sequence>bool operator<(const stack<T, Sequence>& x, const stack<T, Sequence>& y) {  return x.c < y.c;}__STL_END_NAMESPACE#endif /* __SGI_STL_INTERNAL_STACK_H */// Local Variables:// mode:C++// End:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.