• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Design&Art

            C++博客 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
              26 Posts :: 0 Stories :: 38 Comments :: 0 Trackbacks
            書上已經(jīng)講得很清楚了,這里給出一個(gè)編譯通過的例子。
             3mylist.h
            // file: 3mylist.h
            #include <iostream>

            template 
            <typename T>
            class ListItem
            {
            public:
                ListItem(T value, ListItem
            <T>* next)
                
            {
                    _value 
            = value;
                    _next 
            = next;
                }

                T value() 
            const return _value; }
                
            void value(T value) { _value = value; }
                ListItem
            * next() const return _next; }
                
            void next(ListItem* next) { _next = next; }
                
            //
            private:
                T _value;
                ListItem
            * _next;  // †ÎÏò´®ÁУ¨single linked list£©
            }
            ;

            template 
            <typename T>
            class List
            {
            public:
                
            ~List()
                
            {
                    
            if(_front == _end) return;
                    ListItem
            <T>* item = _front;
                    
            while(item != _end)
                    
            {
                        ListItem
            <T>* iter = item;
                        item 
            = item->next();
                        delete iter;
                    }

                
                }

                
            void insert_front(T value)
                
            {
                    _front 
            = new ListItem<T>(value, _front);
                }

                
            void insert_end(T value)
                
            {
                    
            if(_front == _end)
                    
            {
                        _front 
            = new ListItem<T>(value, _front);
                    }

                    ListItem
            <T>* item = _front;
                    
            while(item->next() != _end)
                    
            {
                        item 
            = item->next();
                    }

                    item
            ->next(new ListItem<T>(value, _end));
                }

                
            void display(std::ostream &os = std::cout) const
                
            {
                    ListItem
            <T>* item = _front;
                    
            while(item != _end)
                    
            {
                        os
            <<item->value()<<" ";
                        item 
            = item->next();
                    }

                    os
            <<endl;
                }

                ListItem
            <T>* front()return _front;}
                ListItem
            <T>* end()return _end;}
                
            // 
            private:
                ListItem
            <T>* _end;
                ListItem
            <T>* _front;
                
            long _size;
            }
            ;
            3mylist-iter.h
            // file : 3mylist-iter.h
            #include "3mylist.h"
            template 
            <class Item> // Item可以是單向列表節(jié)點(diǎn)或雙向列表節(jié)點(diǎn)。
            struct ListIter       // 此處這個(gè)迭代器特定只為列表服務(wù),因?yàn)槠?/span>
            {                     // 獨(dú)特的 operator++之故。   
                Item* ptr; // 保持與容器之間的一個(gè)聯(lián)系
                
                ListIter(Item
            * p = 0)  // default ctor
                    :  ptr(p) { }
                
                
            // 不必實(shí)作 copy ctor,因?yàn)榫幾g器提供的預(yù)設(shè)行為已足夠。
                
            // 不必實(shí)作 operator=,因?yàn)榫幾g器提供的預(yù)設(shè)行為已足夠。

                Item
            & operator*()  const return *ptr; }
                Item
            * operator->() const return  ptr; }
                
                
            // 以下兩個(gè)operator++遵循標(biāo)準(zhǔn)作法,參見[Meyers96]條款6      
                
            // (1) pre-increment operator
                ListIter& operator++()
                
            { ptr =  ptr->next(); return *this; }
                
                
            // (2) post-increment operator
                ListIter  operator++(int)
                
            { ListIter tmp = *this++*thisreturn tmp; }
                
                
            bool operator==(const ListIter& i) const
                
            return ptr == i.ptr; }
                
                
            bool operator!=(const ListIter& i) const
                
            return ptr != i.ptr; }
            }
            ;
            3mylist-iter.cpp
            // file : 3mylist-iter.cpp

            #include 
            "stdafx.h"
            #include 
            "3mylist-iter.h"
            #include 
            <iostream>

            using namespace std;

            //  摘自 SGI <stl_algo.h>
            template <class InputIterator, class T>
            InputIterator find(InputIterator first,
                               InputIterator last,
                               
            const T& value) 
            {
                
            while (first != last && (*first).value() != value)
                    
            ++first;
                
            return first;
            }


            // 3mylist-iter-test.cpp
            void main()
            {
                List
            <int> mylist;
                
            for(int i=0; i<5++i) {
                    mylist.insert_front(i);
                    mylist.insert_end(i
            +2);
                }

                mylist.display();     
            // 10 ( 4 3 2 1 0 2 3 4 5 6 )
                ListIter<ListItem<int> > begin(mylist.front());
                ListIter
            <ListItem<int> > end(mylist.end());  // default 0, null
                ListIter<ListItem<int> > iter; // default 0, null

                
            // 執(zhí)行結(jié)果:found. 3
                iter = find(begin, end, 3);
                
            if (iter == end)
                    cout 
            << "not found" << endl;
                
            else
                    cout 
            << "found.  " << iter->value() << endl;

                
            // 執(zhí)行結(jié)果:not found
                iter = find(begin, end, 7);
                
            if (iter == end)
                    cout 
            << "not found" << endl;
                
            else
                    cout 
            << "found. " << iter->value() << endl;

                
            return;
            }
            posted on 2007-04-16 19:54 安帛偉 閱讀(856) 評(píng)論(0)  編輯 收藏 引用 所屬分類: STL
            伊人久久大香线蕉精品不卡| 日本久久久久久久久久| 久久精品人人槡人妻人人玩AV| 婷婷久久综合九色综合九七| 久久久久亚洲av综合波多野结衣| 大香伊人久久精品一区二区| 亚洲伊人久久精品影院| 久久美女人爽女人爽| 久久国产成人精品国产成人亚洲| 亚洲国产小视频精品久久久三级 | 久久99精品久久久久久野外| 久久精品国产亚洲Aⅴ香蕉 | 爱做久久久久久| 久久免费视频1| 97精品国产97久久久久久免费| 久久国产欧美日韩精品免费| 久久se精品一区精品二区| 久久婷婷五月综合97色直播| 精品久久久久久无码中文字幕一区| 日本久久久久久中文字幕| 亚洲精品无码久久久久久| 久久国产三级无码一区二区| 久久婷婷五月综合97色 | 亚洲国产精品无码久久98| 国产激情久久久久影院老熟女免费| 7777久久久国产精品消防器材| 88久久精品无码一区二区毛片| 无码人妻久久久一区二区三区| 久久99精品九九九久久婷婷 | 久久涩综合| 色噜噜狠狠先锋影音久久| 午夜天堂av天堂久久久| 色妞色综合久久夜夜| 久久久久久亚洲精品不卡| 国产—久久香蕉国产线看观看 | 2021精品国产综合久久| 日韩欧美亚洲综合久久| 婷婷久久精品国产| 亚洲国产成人久久综合野外| 老司机午夜网站国内精品久久久久久久久| 97超级碰碰碰碰久久久久|