<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ### 前言 ? stack是一種“先進后出”的數據結構,它只能在棧頂對數據進行操作,即只能在棧頂進行新增元素、移除元素、取得最頂端元素。不能進行遍歷行為,所以不需要設計自己的迭代器。在SGI STL的源碼<stl_stack.h>的設計中,它是基于某種容器作為底部結構的,默認容器是deque容器,用戶也可以自己指定容器的類型。 ### stack容器配接器 ? 由于源碼比較短,同時是基于其他容器進行操作的,這里只給出源碼的剖析: ~~~ #ifndef __SGI_STL_INTERNAL_STACK_H #define __SGI_STL_INTERNAL_STACK_H #include <sequence_concepts.h> __STL_BEGIN_NAMESPACE // Forward declarations of operators == and <, needed for friend declaration. //這里默認的底層容器類型是deque容器 template <class _Tp, class _Sequence __STL_DEPENDENT_DEFAULT_TMPL(deque<_Tp>) > class stack; template <class _Tp, class _Seq> bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y); template <class _Tp, class _Seq> bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y); template <class _Tp, class _Sequence> class stack { // requirements: __STL_CLASS_REQUIRES(_Tp, _Assignable); __STL_CLASS_REQUIRES(_Sequence, _BackInsertionSequence); typedef typename _Sequence::value_type _Sequence_value_type; __STL_CLASS_REQUIRES_SAME_TYPE(_Tp, _Sequence_value_type); #ifdef __STL_MEMBER_TEMPLATES template <class _Tp1, class _Seq1> friend bool operator== (const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); template <class _Tp1, class _Seq1> friend bool operator< (const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&); #else /* __STL_MEMBER_TEMPLATES */ friend bool __STD_QUALIFIER operator== __STL_NULL_TMPL_ARGS (const stack&, const stack&); friend bool __STD_QUALIFIER operator< __STL_NULL_TMPL_ARGS (const stack&, const stack&); #endif /* __STL_MEMBER_TEMPLATES */ public: // 由于stack僅支持對棧頂元素的操作, 所以不定義STL要求的 // pointer, iterator, difference_type typedef typename _Sequence::value_type value_type; typedef typename _Sequence::size_type size_type; typedef _Sequence container_type; typedef typename _Sequence::reference reference; typedef typename _Sequence::const_reference const_reference; protected: _Sequence c;//底層容器類型,默認為deque容器 public: //下面對stack的維護完全依賴于底層容器的操作 stack() : c() {} explicit stack(const _Sequence& __s) : c(__s) {} //判斷容器是否為空 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(); } //在棧頂追加元素 void push(const value_type& __x) { c.push_back(__x); } //彈出棧頂的元素,但不返回任何內容 void pop() { c.pop_back(); } }; //下面是依賴于底層容器的操作運算符 template <class _Tp, class _Seq> bool operator==(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) { return __x.c == __y.c; } template <class _Tp, class _Seq> bool operator<(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) { return __x.c < __y.c; } #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER template <class _Tp, class _Seq> bool operator!=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) { return !(__x == __y); } template <class _Tp, class _Seq> bool operator>(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) { return __y < __x; } template <class _Tp, class _Seq> bool operator<=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) { return !(__y < __x); } template <class _Tp, class _Seq> bool operator>=(const stack<_Tp,_Seq>& __x, const stack<_Tp,_Seq>& __y) { return !(__x < __y); } #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */ __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_STACK_H */ // Local Variables: // mode:C++ // End: ~~~ 這里給出例子: ~~~ // constructing stacks #include <iostream> // std::cout #include <stack> // std::stack #include <vector> // std::vector #include <deque> // std::deque int main () { std::deque<int> mydeque (3,100); // deque with 3 elements std::vector<int> myvector (2,200); // vector with 2 elements std::stack<int> first; // empty stack std::stack<int> second (mydeque); // stack initialized to copy of deque std::stack<int,std::vector<int> > third; // empty stack using vector std::stack<int,std::vector<int> > fourth (myvector); std::cout << "size of first: " << first.size() << '\n'; std::cout << "size of second: " << second.size() << '\n'; std::cout << "size of third: " << third.size() << '\n'; std::cout << "size of fourth: " << fourth.size() << '\n'; second.push(2); std::cout << "The element at the top of stack second is: " << second.top( ) << "." << std::endl; std::cout << "size of second: " << second.size() << '\n'; return 0; } Output: size of first: 0 size of second: 3 size of third: 0 size of fourth: 2 The element at the top of stack second is:2 . size of second: 4 ~~~ 參考資料: 《STL源碼剖析》侯捷
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看