<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                《C++ primer》中的一個例子,感覺對模板編程很有幫助,便從頭到尾實現了一遍。內部機制實際上是一個鏈表,但對外呈現隊列的特性。代碼如下: ~~~ #ifndef __MYQUEUE_H__ #define __MYQUEUE_H__ #include <iostream> // 這里必須提前聲明 template <typename Type> class Queue; template<typename Type> std::ostream& operator << (std::ostream &, const Queue<Type> &); template <typename Type> class QueueItem { // 聲明友元關系 friend class Queue<Type>; friend std::ostream& operator << <Type> (std::ostream &, const Queue<Type> &); QueueItem(const Type &t) : item(t), next(NULL) {} Type item; QueueItem *next; }; template <typename Type> class Queue { public: // 聲明友元關系 friend std::ostream& operator << <Type> (std::ostream &, const Queue<Type> &); Queue() : head(NULL), tail(NULL) {} Queue(const Queue &q) : head(NULL), tail(NULL) { copy_elems(q); } Queue& operator= (const Queue &); ~Queue() { destroy(); } Type& front() { if (head == NULL) throw exception("Queue is empty."); return head->item; } const Type& front() const { if (head == NULL) throw exception("Queue is empty."); return head->item; } void push(const Type&); void pop(); bool empty() const { return head == NULL; } private: QueueItem<Type> *head; // 指向Queue頭節點 QueueItem<Type> *tail; // 指向Queue尾節點 void copy_elems(const Queue &); void destroy(); }; template<typename Type> Queue<Type>& Queue<Type>::operator= (const Queue &q) { if (&q == this) return *this; destroy(); copy_elems(q); return *this; } // 從尾添加一個節點 template<typename Type> void Queue<Type>::push(const Type &t) { QueueItem<Type> *tmp = new QueueItem<Type>(t); if (empty()) { head = tail = tmp; } else { tail->next = tmp; tail = tmp; } } // 從頭刪除一個節點 template<typename Type> void Queue<Type>::pop() { if (head != NULL) { QueueItem<Type> *tmp = head; head = tmp->next; delete tmp; } } template<typename Type> void Queue<Type>::destroy() { while (!empty()) pop(); } template<typename Type> void Queue<Type>::copy_elems(const Queue &q) { QueueItem<Type> *tmp = q.head; while (tmp != NULL) { push(tmp->item); tmp = tmp->next; } } template<typename Type> std::ostream& operator << (std::ostream &os, const Queue<Type> &q) { QueueItem<Type> *tmp = q.head; while (tmp != NULL) { os << tmp->item << ' '; tmp = tmp->next; } return os; } // Queue的特化版本 template <> class Queue<const char *> { public: // 聲明特化版本的 operator<< 為友元 friend std::ostream& operator << <const char *> (std::ostream &os, const Queue<const char *> &q); void push(const char *); void pop() { real_queue.pop(); } bool empty() const { return real_queue.empty(); } std::string& front() { return real_queue.front(); } const std::string& front() const // 基于const的成員函數重載 { return real_queue.front(); } private: Queue<std::string> real_queue; }; void Queue<const char *>::push(const char *val) { real_queue.push(val); } template <> std::ostream& operator << <const char *> (std::ostream &os, const Queue<const char *> &q) { os << q.real_queue; return os; } #endif ~~~ 參考: 《C++ primer》
                  <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>

                              哎呀哎呀视频在线观看