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

            相冊

            最新隨筆

            搜索

            •  

            積分與排名

            • 積分 - 56388
            • 排名 - 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电影 | 久久er热视频在这里精品| 97久久国产亚洲精品超碰热| 国产福利电影一区二区三区,免费久久久久久久精 | 久久精品国产精品亚洲精品| 国产精品久久久久影院嫩草| 亚洲精品视频久久久| 国产精品久久亚洲不卡动漫| 少妇被又大又粗又爽毛片久久黑人| 无码专区久久综合久中文字幕 | 99久久99这里只有免费的精品| 久久精品成人一区二区三区| 久久偷看各类wc女厕嘘嘘| 久久久久亚洲AV成人网人人网站| 精品蜜臀久久久久99网站| 女同久久| 久久久久久国产a免费观看不卡| 久久免费的精品国产V∧| 精品久久久久久久久免费影院| 久久狠狠色狠狠色综合| 久久男人Av资源网站无码软件| 久久亚洲精品国产精品婷婷| 国内精品免费久久影院| 丁香狠狠色婷婷久久综合| 久久影院综合精品| 欧美日韩精品久久免费| 人妻丰满?V无码久久不卡| 久久精品国产只有精品66 | 亚洲AV无码一区东京热久久 | 国産精品久久久久久久| 久久精品人人槡人妻人人玩AV| 久久人与动人物a级毛片| 色青青草原桃花久久综合| 久久久久亚洲AV综合波多野结衣 | 国产免费久久精品99re丫y| 欧美一级久久久久久久大| 久久久国产精品| 久久久久综合中文字幕| 日韩久久无码免费毛片软件| 久久精品免费网站网| 久久久久久亚洲精品影院|