• <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>
            隨筆 - 25  文章 - 29  trackbacks - 0
            <2006年5月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            常用鏈接

            留言簿(4)

            隨筆分類(22)

            隨筆檔案(25)

            文章分類(2)

            文章檔案(2)

            相冊

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 56396
            • 排名 - 404

            最新評論

            閱讀排行榜

            評論排行榜

            ?1 template? < typename?T1,typename?T2 >
            ?2 class ?mypair?: public ?pair < T1,T2 >
            ?3 {
            ?4 public :
            ?5 ????inline?mypair?():pair() {} ;
            ?6 ????inline?mypair( const ?T1 & ?a, const ?T2? & ?b):pair < T1,T2 > (a,b) {} ;
            ?7 ????inline?mypair( const ?mypair < T1,T2 > ? & ?a):pair < T1,T2 > (a) {} ;
            ?8 ????friend?ostream & ? operator << ? < T1,T2 > (ostream & ?os,mypair < T1,T2 > ? & ?p);
            ?9 protected :
            10 private :
            11 }
            ;
            12 template? < typename?T1,typename?T2 >
            13 ostream & ? operator << (ostream & ?os,mypair < T1,T2 >& ?p)
            14 {
            15 ????os << " \n?cout?pair? " ;
            16 ????os << endl << " ? " << p.first;
            17 ????os << endl << " ? " << p.second;
            18 ???? return ?os;
            19 }

            20 ?
            21
            22 int ?_tmain( int ?argc,?_TCHAR * ?argv[])
            23 {
            24 ????f();
            25 ????mypair < int , float > ?a( 1 , 2.0333 );
            26 ????mypair < int , float > ?b( 2 , 6.04343 );
            27 ????mypair < int , float > ?c(b);
            28 ????cout << a;
            29 ????cout << b;
            30 ????cout << c;
            31 ????a.swap(b);
            32 ????cout << a;
            33 ????cout << b;
            34
            35 ????cout << (a == b);
            36
            37 ???? return ? 0 ;
            38 }

            39
            #include <utility>
            template <typename T1,typename T2>
            class pair
            {
            ??? 1.空構造
            ??? 2.雙參數構造
            ???? 3.拷貝構造
            ????? 4.swap交換
            5.內含2個T1,T2型 變量(first ,second);
            }
            pair 是struct ,所有成員公有
            template <typename T1,typename T2>

            ??? pair<T1,T2> make_pair(const T1&,const T2&)

            ?? 操作符重載模版
            make_pair(a,b);//按值返回個pair< >對象;
            make_pair<int,float >(a,b);

            auto_ptr<T> ptr(new int);//智能指針類???

            輔助函數模板
            max ,min, swap
            template<class Type>   void swap(      Type& _Left,       Type& _Right   );   #include <algorithm>


            4個比較算子
            #include <utility>
            using namespace rel_ops;
            就重載了這幾個算子

            template<Class Type>   bool operator!=(      const Type& _Left,      const Type& _Right);template<class Type1, class Type2>   bool operator!=(      const pair<Type1, Type2>& _Left,      const pair<Type1, Type2>& _Right   );
            非pair<> 的在 std::rel_ops中



            ?1以下是迭代器基類
            ?2template<class?_Category,
            ?3????class?_Ty,
            ?4????class?_Diff?=?ptrdiff_t,
            ?5????class?_Pointer?=?_Ty?*,
            ?6????class?_Reference?=?_Ty&>
            ?7????????struct?iterator
            ?8????{????//?base?type?for?all?iterator?classes
            ?9????typedef?_Category?iterator_category;
            10????typedef?_Ty?value_type;
            11????typedef?_Diff?difference_type;
            12????typedef?_Diff?distance_type;????//?retained
            13????typedef?_Pointer?pointer;
            14????typedef?_Reference?reference;
            15????}
            ;
            16

            ?1template<class?_Ty>
            ?2????class?allocator
            ?3//基類,是個空結構
            ?4????????:?public?_Allocator_base<_Ty>
            ?5????{????//?generic?allocator?for?objects?of?class?_Ty
            ?6public:
            ?7????typedef?_Allocator_base<_Ty>?_Mybase;
            ?8????typedef?typename?_Mybase::value_type?value_type;
            ?9
            10
            11????typedef?value_type?_FARQ?*pointer;
            12????typedef?value_type?_FARQ&?reference;
            13????typedef?const?value_type?_FARQ?*const_pointer;
            14????typedef?const?value_type?_FARQ&?const_reference;
            15
            16????typedef?_SIZT?size_type;
            17????typedef?_PDFT?difference_type;
            18
            19????template<class?_Other>
            20????????struct?rebind
            21????????{????//?convert?an?allocator<_Ty>?to?an?allocator?<_Other>
            22????????typedef?allocator<_Other>?other;
            23????????}
            ;
            24
            25????pointer?address(reference?_Val)?const
            26????????{????//?return?address?of?mutable?_Val
            27????????return?(&_Val);
            28????????}

            29
            30????const_pointer?address(const_reference?_Val)?const
            31????????{????//?return?address?of?nonmutable?_Val
            32????????return?(&_Val);
            33????????}

            34
            35????allocator()
            36????????{????//?construct?default?allocator?(do?nothing)
            37????????}

            38
            39????allocator(const?allocator<_Ty>&)
            40????????{????//?construct?by?copying?(do?nothing)
            41????????}

            42
            43????template<class?_Other>
            44????????allocator(const?allocator<_Other>&)
            45????????{????//?construct?from?a?related?allocator?(do?nothing)
            46????????}

            47
            48????template<class?_Other>
            49????????allocator<_Ty>&?operator=(const?allocator<_Other>&)
            50????????{????//?assign?from?a?related?allocator?(do?nothing)
            51????????return?(*this);
            52????????}

            53
            54????void?deallocate(pointer?_Ptr,?size_type)
            55????????{????//?deallocate?object?at?_Ptr,?ignore?size
            56????????operator?delete(_Ptr);
            57????????}

            58
            59????pointer?allocate(size_type?_Count)
            60????????{????//?allocate?array?of?_Count?elements
            61????????return?(_Allocate(_Count,?(pointer)0));
            62????????}

            63
            64????pointer?allocate(size_type?_Count,?const?void?_FARQ?*)
            65????????{????//?allocate?array?of?_Count?elements,?ignore?hint
            66????????return?(allocate(_Count));
            67????????}

            68
            69????void?construct(pointer?_Ptr,?const?_Ty&?_Val)
            70????????{????//?construct?object?at?_Ptr?with?value?_Val
            71????????_Construct(_Ptr,?_Val);
            72????????}

            73
            74????void?destroy(pointer?_Ptr)
            75????????{????//?destroy?object?at?_Ptr
            76????????_Destroy(_Ptr);
            77????????}

            78
            79????_SIZT?max_size()?const
            80????????{????//?estimate?maximum?array?size
            81????????_SIZT?_Count?=?(_SIZT)(-1)?/?sizeof?(_Ty);
            82????????return?(0?<?_Count???_Count?:?1);
            83????????}

            84????}
            ;
            以上是.net 2003 中 iterator 和 allocator 的代碼
            可以看出 stl是借助allocate 模板來分配元素 空間的?


            一個自定義迭代器,和自定義容器

            ??1template<typename?item>
            ??2class?maniter;
            ??3
            ??4template?<typename?T>
            ??5class?man
            ??6{
            ??7public:
            ??8????typedef?maniter<T>?iterator?;
            ??9?????man()
            ?10????{
            ?11????????array=NULL;
            ?12????????size=0;
            ?13????}

            ?14????iterator?begin()
            ?15????{
            ?16????????iterator?*pt?=new?iterator;
            ?17????????pt->p=array;
            ?18????????return?*pt;
            ?19????}

            ?20????iterator?end()
            ?21????{
            ?22?????????iterator?*pt=new?iterator;
            ?23?????????pt->p=array+sizeof(T)*size;
            ?24?????????return?*pt;
            ?25????}

            ?26???????man(int?count)
            ?27????{
            ?28????????
            ?29????????array=new?T?[count];
            ?30
            ?31????????size=count;
            ?32????????for?(int?i=0;i<size;i++)
            ?33????????{
            ?34
            ?35????????????*(array+i)=i;
            ?36????????}

            ?37????
            ?38????}

            ?39????man(const?man&?a)
            ?40????{
            ?41
            ?42????????
            ?43????????if(this!=&a)
            ?44????????{
            ?45??????????????????????size=a.size;
            ?46????????
            ?47????????????try{
            ?48????????array=new?T[a.size];?
            ?49????????????}

            ?50????????????catch?(bad_alloc?e)?{
            ?51????????????????
            ?52????????????}

            ?53????????}

            ?54????????for?(int?i=0;i<a.size;i++)
            ?55????????{
            ?56????????????
            ?57????????????*(array+i)=a.array[i];
            ?58????????}

            ?59????????
            ?60????????
            ?61????}

            ?62????void?operator?=(const?man&?a)
            ?63????{
            ?64????????
            ?65????????
            ?66????????delete?[]?array;
            ?67????????array=new?T?[a.size];?
            ?68????????size=a.size;
            ?69????????for?(int?i=0;i<a.size;i++)
            ?70????????{
            ?71????????????*(array+i)=a.array[i];
            ?72????????}

            ?73????????
            ?74????????????//return?*this;
            ?75????}

            ?76????~man()
            ?77????{
            ?78????????delete?[]?array;
            ?79????????size=0;
            ?80????}

            ?81????man<T>&?operator++()
            ?82????{
            ?83???????????this->array++;
            ?84???????????return?*this;
            ?85????}

            ?86????const?man<T>?operator++(int?)
            ?87????{
            ?88????????man<T>?temp=*this;
            ?89????????????(this->array)++;
            ?90????????????????return?temp;
            ?91????}

            ?92???
            ?93protected:
            ?94????friend?ostream&?operator<<?<T>(ostream&?os,const?man<T>?&?a);
            ?95private:
            ?96????T?*?array;
            ?97????int?size;
            ?98}
            ;
            ?99template?<typename?T>
            100ostream&?operator<<(ostream&?os,?const?man<T>&?a)
            101{
            102????cout<<"\n?in?printing?man"<<endl;
            103????if?(!a.size)
            104????{
            105????????cout<<"\n"<<"?array?is?null";
            106????}

            107???????for?(int?i=0;i<a.size;i++)
            108???????{
            109???????????
            110??????????os<<"?"<<a.array[i];
            111???????}

            112???????cout<<endl;
            113???????return?os;
            114?}

            115
            116template?<typename?it,typename?T>
            117it?myfind(it?begin,?it?end?,?const?T&?a)
            118{
            119????for?(;begin!=end&&*begin!=a;++begin)
            120????{}
            121????return?begin;
            122}

            123?
            124template<typename?item>
            125class?maniter
            126{
            127public:
            128????item*?p;
            129????maniter():p(0)
            130????{
            131
            132????}

            133????item&?operator*(){return?*p;};
            134????item*?operator->(){return?p;};
            135????item&?operator++(){p++;return?*p;};
            136????item?operator++(int){item?temp=*this;this++;return?temp;}
            137????bool?operator==(const?maniter?&?ele)
            138????{
            139????????return?*p==*(ele.p);
            140????}

            141????bool?operator!=(const?maniter?&?ele)
            142????{
            143????????return?*p!=*(ele.p);
            144????}

            145
            146protected:
            147private:
            148????
            149}
            ;
            150int?_tmain(int?argc,?_TCHAR*?argv[])
            151{
            152/*
            153????vector<man<int>?>?a;
            154for?(int?i=1;i<10;++i)
            155????{
            156????????cout<<"\n?"<<i;
            157?????????man<int>*?manb=new?man<int>?(i);
            158????????a.push_back(*manb);
            159?????????delete?manb;
            160????????
            161????}
            162??*/

            163????man<int?>?b(5);
            164????cout<<(*myfind(b.begin(),b.end(),3));//very?perfect,自定義迭代器成功實現
            165?????//迭代器就是包含容器節點指針的類模板對象,提供?*,->,++,--,==,!=?(essential)操作的,這些操作需要迭代器
            166????//對容器結構了解,如本列中maniter?就清楚知道man?類中的節點結構是個1維數組
            167return?0;
            168}

            169
            170
            類型萃取技術(迭代器中廣泛使用的技術)

            ? template <typename T>
            ?? struct iterator
            {
            ??? typedef T? value_type;
            };
            template <typename T>
            ?struct iterator_trait
            {
            ?????typedef? T ?typename? T::iterator::value_type?? value_type;
            };

            template <typename T>
            typename iterator_trait<T>::value_type
            ?f(T)
            {};

            ???萃取機 iterator_trait<迭代器〉??快速的從迭代器中 取出 迭代器實際類型
            (very important)
            ???????


            posted on 2006-05-31 16:53 黃大仙 閱讀(1232) 評論(0)  編輯 收藏 引用 所屬分類: c++
            久久夜色精品国产欧美乱| 久久精品国产第一区二区| 久久国产精品-久久精品| 天堂久久天堂AV色综合| 精品久久久久久久久免费影院| 免费国产99久久久香蕉| 国产91色综合久久免费| WWW婷婷AV久久久影片| 国产亚洲综合久久系列| 久久亚洲国产成人影院网站| 久久久精品久久久久特色影视| 精品伊人久久大线蕉色首页| 色欲综合久久中文字幕网| 国产精品免费看久久久香蕉| 久久国产精品免费一区二区三区| 精品久久人人妻人人做精品 | www久久久天天com| 久久久无码精品亚洲日韩软件| 91精品国产91久久久久福利| 精品久久久中文字幕人妻| 久久人搡人人玩人妻精品首页| 东京热TOKYO综合久久精品| 久久亚洲精品无码aⅴ大香| 精品无码久久久久久国产| 91视频国产91久久久| 国产成人精品久久| 久久se精品一区精品二区| 久久亚洲AV成人无码电影| 久久精品国产亚洲αv忘忧草| 久久综合一区二区无码| 精品久久久久一区二区三区| 日本免费一区二区久久人人澡| 97久久天天综合色天天综合色hd| 亚洲AV无码1区2区久久| 一本色道久久综合狠狠躁| 久久无码中文字幕东京热| 久久久久精品国产亚洲AV无码| 久久精品国产欧美日韩99热| 91精品国产9l久久久久| 久久久无码精品亚洲日韩蜜臀浪潮| 久久狠狠色狠狠色综合|