• <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>
            Lyt
            posts - 16,comments - 61,trackbacks - 0
            最近由于需要頻繁使用Dictionary,于是就動(dòng)手實(shí)現(xiàn)了一個(gè),希望和List可以隨意地?fù)Q來換去。
            //IList.h
            template<typename _Type>
            class IIList : public ICollection<_Type>{};

            //List.h
            template<typename _Type>
            class List : public IList<_Type>{};

            //IDictionary.h
            template<typename TKey, typename TValue>
            class IDictionary : public ICollection<Pair<TKey, TValue>>{};
            我希望IDictionary.h提供一個(gè)ToList() GetKeys() GetValues() 的操作,如果返回IList:

            error C2682: cannot use 'dynamic_cast' to convert from 'const Lyt::Collection::List<_Type> *' to 'Lyt::Collection::IList<_Type> *' e:\c++\library\library\library\collection\dictionary.h 171

            為啥會(huì)有編譯錯(cuò)誤,至今不明,明明List繼承了IList怎么還會(huì)轉(zhuǎn)換失敗=  =

            還有另外一個(gè)問題,在實(shí)現(xiàn)GetKeys()時(shí),我希望是返回IList而非List,這樣如果我以后有其他關(guān)于List的實(shí)現(xiàn)就不需要修改代碼了,考慮到內(nèi)存泄漏的問題,我只想到兩種解決方法,不知道到底怎么折騰才合適,求解答。

            //方法一
            #include "List.h"
            template
            <typename TKey, typename TValue>
            class Dictionary : public IDictionary<TKey, TValue>
            {
            protected:
              List
            <Pair<TKey, TValue>> Data;
            public:
              AutoPtr
            <IList<Pair<TKey, TValue>>> GetKeys()const
              {
                AutoPtr
            <IList<Pair<TKey, TValue>>> Result=new List<TKey>//用引用計(jì)數(shù)實(shí)現(xiàn)的智能指針
                /**/
              }
            };

            //方法二
            #include "List.h"
            template
            <typename TKey, typename TValue>
            class Dictionary : public IDictionary<TKey, TValue>
            {
            protected:
              List
            <Pair<TKey, TValue>> Data;
              List
            <TKey> Keys; //本來我只需要一個(gè)List來存放東西就夠了,這樣我在其他的Add、Remove等操作中都要多折騰一個(gè)Keys,是不是沒有必要?
            public:
              
            const IList<TKey>* GetKeys()const
              {
                
            return dynamic_cast<IList<TKey>*>(&Keys);
              }
            };
            posted on 2009-10-15 09:44 Lyt 閱讀(1454) 評(píng)論(5)  編輯 收藏 引用 所屬分類: 數(shù)據(jù)結(jié)構(gòu)

            FeedBack:
            # re: Dictionary的囧狀
            2009-10-15 13:43 | OwnWaterloo
            1.
            error C2682: cannot use 'dynamic_cast' to convert from 'const Lyt::Collection::List<_Type> *' to 'Lyt::Collection::IList<_Type>
             
            明白了嗎?
             
            2.
            IDictionary.h  include  List.h
            Dictionary.h include IDictionary.h
            相互包含在哪?
             
             
             
            C++不是這么用的……  被C#熏昏頭的同學(xué)……
              回復(fù)  更多評(píng)論
              
            # re: Dictionary的囧狀
            2009-10-15 15:39 | Lyt
            @OwnWaterloo
            沒有相互包含的狀況,抱歉…
            已經(jīng)解決const的問題了,謝。

            數(shù)據(jù)結(jié)構(gòu)的確是模仿.NET的,呃,不太明白為什么你說C++不是這么用的?  回復(fù)  更多評(píng)論
              
            # re: Dictionary的囧狀
            2009-10-15 22:30 | OwnWaterloo
            @Lyt
            你也發(fā)現(xiàn)問題了不是? 內(nèi)存不好管理。

            如果你用GP的思想去實(shí)現(xiàn)Dictionary, GetKeys可以很簡單,并且很高效的實(shí)現(xiàn)。
            用OOP? 就等著承受虛函數(shù)開銷以及內(nèi)存管理的困擾吧。。。
            然后,你又會(huì)為此去寫智能指針, 寫內(nèi)存池, 寫鎖 ……

            你可以把這些當(dāng)作練習(xí)…… 但這不是C++的style ……  回復(fù)  更多評(píng)論
              
            # re: Dictionary的囧狀
            2009-10-15 22:52 | OwnWaterloo
            我說具體一點(diǎn)吧……
            如果用GP(代碼一切從簡):
             
            template<typename T>
            class /*array*/list {
                T *first,*last_;
            public:
                typedef T* iterator;
                typedef const T* const_iterator;
             
                iterator begin() { return first_; }
                iterator end() { return last_; }
                const_iterator begin() const { return first_; }
                const_iterator end() const { return last_; } 
                // ...
            };
             
            要迭代一個(gè)容器:
            list<int> l;
            for ( list<int>::iterator first = l.begin(), last = l.end(), first!=last; ++first)
                visit_element( *first );
             
            而你遇到的問題就是:
            list<pair<key,value> > d;
            如何得到一個(gè)迭代器, 僅僅訪問key部分, 而不訪問value部分。
             
            template<class It>
            class project_first {
                It it_;
            public:
                project_first( It it ) : it_(it) {}
                typename std::iterator_traits<It>::value_type::first_type&
                    operator*() const { return it->first; }
                // 再實(shí)現(xiàn) ++, -- +n 等操作...
            };
             
            for ( project_first first = d.begin(), last = d.end(); first!=last; ++first)
                visit_key( *first );
             
             
            如果d是 list<tuple<key,value> > 類型, project_iterator還可以做得更范化一些。
             
            沒有虛函數(shù)調(diào)用,沒有動(dòng)態(tài)內(nèi)存分配。
            并且,和stl的算法,boost的算法,以及其他C++組件合作良好。
              回復(fù)  更多評(píng)論
              
            # re: Dictionary的囧狀
            2009-10-15 23:14 | OwnWaterloo
            C#的template能做什么我不太清楚。

            C++支持編譯時(shí)的ducking type機(jī)制。
            拋棄這種強(qiáng)大的抽象機(jī)制不用, 轉(zhuǎn)而在C++這種暫不提供GC的語言中使用接口來作為數(shù)據(jù)結(jié)構(gòu)之間的紐帶 ……
            所以…… 我說這不是C++的style ……

            還有一些小地方。 比如main 的返回類型是int, 而不是C#中的void。
            以單下劃線接大寫字母開頭,以及以雙下劃線開頭的標(biāo)識(shí)符在C++中是被保留的。
            最好不要將C#中的那些習(xí)慣帶到C++中來……
            用Type, Type_, 別用_Type。

            這些被保留的標(biāo)識(shí)符不會(huì)被永遠(yuǎn)保留。 _Bool, _LongLong, _Complex已經(jīng)出現(xiàn)。



            http://www.shnenglu.com/Streamlet/
            這位同學(xué), 和你在做類似的事情, 也遇到了類似的問題。
            你可以參考參考……  回復(fù)  更多評(píng)論
              
            91精品国产色综久久| 久久99精品国产| 精品水蜜桃久久久久久久| 伊人色综合久久天天人手人婷| 天天综合久久一二三区| 欧美午夜精品久久久久久浪潮| 久久本道久久综合伊人| 国产精品嫩草影院久久| 久久久久久毛片免费看| 美女久久久久久| 国产69精品久久久久9999APGF| 狠狠精品久久久无码中文字幕 | 77777亚洲午夜久久多喷| 一97日本道伊人久久综合影院| 亚洲精品国产第一综合99久久| 97香蕉久久夜色精品国产 | 久久天天躁狠狠躁夜夜2020一| 欧美伊人久久大香线蕉综合| 日韩精品无码久久久久久| 91久久精品91久久性色| 亚洲国产精品婷婷久久| 久久久久久久久久免免费精品| 亚洲性久久久影院| 久久婷婷国产综合精品| 热久久这里只有精品| 亚洲人成无码www久久久| 久久男人Av资源网站无码软件 | 一本久久a久久精品综合夜夜| 国产精品伦理久久久久久| 久久久www免费人成精品| 精品乱码久久久久久久| 人妻无码久久精品| 久久不见久久见免费视频7| 久久99精品久久久久久野外| 久久精品卫校国产小美女| 久久精品男人影院| 亚洲色欲久久久综合网东京热| 久久99久久成人免费播放| 久久亚洲精品成人av无码网站| 99久久免费国产精品| 久久精品国产亚洲av水果派|