青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Lyt
posts - 16,comments - 61,trackbacks - 0

發現這里被我荒廢了很久,自動腸粉--

最近在湊數據結構,發現要湊個稍微好看點的代碼難度好大,模仿了.NET改了自己以前的結構,憋了一天終于把可擴展的List折騰出來了

比較怪異的是迭代器iterator,怎么看怎么別扭,暫時先湊合著吧

IIterator.h

        template<typename _Type>
        
class IIterator    //迭代器
        {
        
protected:
            _Type Current;

        
public:
            
virtual ~IIterator()
            
{
            }


            
virtual _Type CurrentItem()const=0;        //Current是否有效
            virtual void First()=0;                    //Current移到首位
            virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
            virtual bool IsFirst()const=0;            //Current是否在首位
            virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
            virtual bool IsAvailable()const=0;        //Current是否有效
            virtual void MoveNext()=0;                //Current移到下一位置
        }
;

        template
<typename _Type>
        
class IForwordIterator : public IIterator<_Type>    //單向迭代器
        {
        
public:
            
virtual ~IForwordIterator()
            
{
            }


            
virtual _Type CurrentItem()const=0;        //Current是否有效
            virtual void First()=0;                    //Current移到首位
            virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
            virtual bool IsFirst()const=0;            //Current是否在首位
            virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
            virtual bool IsAvailable()const=0;        //Current是否有效
            virtual void MoveNext()=0;                //Current移到下一位置
        }
;

        template
<typename _Type>
        
class IBidirectionalIterator : public IIterator<_Type>    //雙向迭代器
        {
        
public:
            
virtual ~IBidirectionalIterator()
            
{
            }


            
virtual _Type CurrentItem()const=0;        //Current是否有效
            virtual void First()=0;                    //Current移到首位
            virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
            virtual bool IsFirst()const=0;            //Current是否在首位
            virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
            virtual bool IsAvailable()const=0;        //Current是否有效
            virtual void MoveNext()=0;                //Current移到下一位置

            
virtual void MovePrev()=0;                //Current移到前一位置
        }
;

        template
<typename _Type>
        
class IRandomIterator : public IIterator<_Type>    //隨機迭代器
        {
        
public:
            
virtual ~IRandomIterator()
            
{
            }


            
virtual _Type CurrentItem()const=0;        //Current是否有效
            virtual void First()=0;                    //Current移到首位
            virtual bool IsDone()const=0;            //Current是否到達末尾,true的時候CurrentItem()失效
            virtual bool IsFirst()const=0;            //Current是否在首位
            virtual bool IsLast()const=0;            //Current是否到達末尾,ture的時候CurrentItem()有效
            virtual bool IsAvailable()const=0;        //Current是否有效
            virtual void MoveNext()=0;                //Current移到下一位置
            
            
virtual void MovePrev()=0;                //Current移到前一位置
            virtual void SkipPrev(const int n)=0;    //Current前移n位
            virtual void SkipNext(const int n)=0;    //Current后移n位
        }
;

        template
<typename _Type>
        
class IIterative    //可迭代的
        {
        
public:
            
virtual ~IIterative()
            
{
            }


            
virtual AutoPtr<IIterator<_Type>> CreateIterator()const=0;    //返回迭代器
        }
;

ListIterator

        template<typename _Type>
        
class ListIterator : public IRandomIterator<_Type>        //List迭代器
        {
        
public:
            
const List<_Type>* Data;
            
int Index;

            ListIterator(
const List<_Type>* Object)
            
{
                
if (!Object || Object->IsEmpty()) throw L"List迭代器初始化出錯";
                
else
                
{
                    Data
=Object;
                    Index
=0;
                }

            }


            ListIterator(
const ListIterator<_Type>& Object)
            
{
                Data
=Object.Data;
                Index
=Object.Index;
                Current
=Object.Current;
            }


            
virtual ~ListIterator()
            
{
            }


            
virtual _Type CurrentItem()const        //Current是否有效
            {
                
if (IsAvailable()) return Current;
                
else throw L"非法訪問當前元素";
            }


            
virtual void First()                    //Current移到首位
            {
                
if (Data->IsEmpty()) throw L"List為空";
                
else
                
{
                    Index
=0;
                    Current
=Data->operator [](0);
                }

            }


            
virtual bool IsDone()const                //Current是否到達末尾,true的時候CurrentItem()失效
            {
                
return Index>=Data->GetCount();
            }


            
virtual bool IsFirst()const                //Current是否在首位
            {
                
return Index==0;
            }


            
virtual bool IsLast()const                //Current是否到達末尾,ture的時候CurrentItem()有效
            {
                
return Index==Data->GetCount()-1;
            }


            
virtual bool IsAvailable()const            //Current是否有效
            {
                
return (Index>=0 && Index<=Data->GetCount()-1);
            }


            
virtual void MoveNext()                    //Current移到下一位置
            {
                
++Index;
                
if (IsAvailable()) Current=Data->operator [](Index);
            }

            
            
virtual void MovePrev()                    //Current移到前一位置
            {
                
--Index;
                
if (IsAvailable()) Current=Data->operator [](Index);
            }


            
virtual void SkipPrev(const int n)        //Current前移n位
            {
                Index
-=n;
                
if (IsAvailable()) Current=Data->operator [](Index);
            }


            
virtual void SkipNext(const int n)        //Current后移n位
            {
                Index
+=n;
                
if (IsAvailable()) Current=Data->operator [](Index);
            }

        }
;

 

ICollecion.h

        template<typename _Type>
        
class ICollection : public IIterative<_Type>
        
{
        
protected:
            
int Count;            //元素個數

        
public:
            
virtual ~ICollection()
            
{
            }


            
virtual AutoPtr<IIterator<_Type>> CreateIterator()const=0;    //返回迭代器,繼承自IIterative

            
virtual const int GetCount()const=0;                    //返回元素個數
            virtual bool IsEmpty()const=0;                            //是否為空
            virtual bool Add(const _Type& Object)=0;                        //從尾部添加元素
            virtual void Clear()=0;                                    //刪除所有元素
            virtual bool Contains(const _Type& Object)const=0;                //是否包含指定元素
            virtual bool Remove(const _Type& Object)=0;                    //刪除指定元素,若有多個,則刪除第一個
        }
;

IList.h

        template<typename _Type>
        
class IList : public ICollection<_Type>
        
{
        
public:
            
virtual ~IList()
            
{
            }


            
virtual AutoPtr<IIterator<_Type>> CreateIterator()const=0;        //返回迭代器,繼承自IIterative
            virtual const int GetCount()const=0;                            //返回元素個數,繼承自ICollection
            virtual bool IsEmpty()const=0;                                    //是否為空,繼承自ICollection
            virtual bool Add(const _Type& Object)=0;                        //從尾部添加元素,繼承自ICollection
            virtual void Clear()=0;                                            //刪除所有元素,繼承自ICollection
            virtual bool Contains(const _Type& Object)const=0;                //是否包含指定元素,繼承自ICollection
            virtual bool Remove(const _Type& Object)=0;                        //刪除指定元素,若有多個,則刪除第一個,繼承自ICollection

            
virtual int IndexOf(const _Type& Object)const=0;                //返回指定元素所在位置,若不存在返回-1,若有多個,返回第一個
            virtual bool Insert(const int Index, const _Type& Object)=0;    //在指定位置插入指定元素
            virtual bool RemoveAt(const int Index)=0;                        //在指定位置刪除元素
        }
;
List.h
        template<typename _Type>
        
class List : public IList<_Type>
        
{
        
private:
            
bool Grow();                                                        //如果Capacity有增大,則返回真
            void Update();                                                        //Capacity增大導致Items需要更新
    
        
protected:
            _Type
* Items;
            
int Capacity;                                                        //List可容納元素個數
            static const int GRAW_CAPACITY;                                        //List每次增大的容量

        
public:
            
bool IsFixedSize;                                                    //List大小是否固定

            List(
const bool Fixed=false);
            List(
const int ObjectCount, const bool Fixed=false);
            List(
const List<_Type>& Object);

            
virtual ~List();

            
virtual AutoPtr<IIterator<_Type>> CreateIterator()const;            //返回迭代器,繼承自IIterative
            virtual void Clear();                                                //刪除所有元素,繼承自ICollection
            virtual const int GetCount()const;                                    //返回元素個數,繼承自ICollection
            virtual int IndexOf(const _Type& Object)const;                        //返回指定元素所在位置,若不存在返回-1,繼承自IList
            virtual int LastIndexOf(const _Type& Object)const                    //返回指定元素所在位置,若不存在返回-1,若有多個,返回最后一個
            virtual bool IsEmpty()const;                                        //是否為空,繼承自ICollection
            virtual bool Add(const _Type& Object);                                //從List尾部添加元素,繼承自ICollection
            virtual bool Add(const List<_Type>& Object);                        //在List末尾添加列表
            virtual bool Contains(const _Type& Object)const;                    //是否包含指定元素,繼承自ICollection
            virtual bool Remove(const _Type& Object);                            //刪除指定元素,若有多個,則刪除第一個,繼承自ICollection
            virtual bool Remove(const int Index, const int n);                    //從Index開始刪除n個元素
            virtual bool RemoveAt(const int Index);                                //在指定位置刪除元素,繼承自IList
            virtual bool Insert(const int Index, const _Type& Object);            //在指定位置插入指定元素,繼承自IList
            virtual bool Insert(const int Index, const List<_Type>& Object);    //從Index開始插入Object
            virtual List<_Type> Sub(const int Index, const int n)const;            //從Index開始取n個元素
            virtual List<_Type> Left(const int n)const;                            //取左邊n個元素
            virtual List<_Type> Right(const int n)const;                        //取右邊n個元素
            _Type operator[](const int Index)const;
            _Type
& operator[](const int Index);
            List
<_Type> operator=(const List<_Type>& Object);
            
bool operator==(const List<_Type>& Object)const;
            
bool operator!=(const List<_Type>& Object)const;
            List
<_Type> operator+(const _Type& Object)const;
            List
<_Type> operator+(const List<_Type>& Object)const;
        }
;

例子:

void Print(AutoPtr<ListIterator<int>> Iterator)
{
    
for (Iterator->First(); !Iterator->IsDone(); Iterator->MoveNext()) cout<<Iterator->CurrentItem()<<" ";
    cout
<<endl;
}


void main(int argc , char* argv[])
{
    setlocale(LC_ALL,
"chs");
    List
<int> A(1);
    A[
0]=1;
    AutoPtr
<ListIterator<int>> Iterator=A.CreateIterator();
    Print(Iterator);
    A.Add(
2);
    cout
<<"After A.Add(2) : ";
    Print(Iterator);
    
if (A.Contains(1)) cout<<"A contains 1"<<endl;
    
if (!A.Contains(3)) cout<<"A doesn't contain 3"<<endl;
    cout
<<"index of 1 is "<<A.IndexOf(1)<<endl;
    cout
<<"index of 3 is "<<A.IndexOf(3)<<endl;
    A.Insert(
0,0);
    cout
<<"After A.Insert(0,0) : ";
    Print(Iterator);
    A.Insert(
1,3);
    cout
<<"After A.Insert(1,3) : ";
    Print(Iterator);
    A.RemoveAt(
1);
    cout
<<"After A.RemoveAt(1) : ";
    Print(Iterator);
    List
<int> B=A.Right(2);
    A.Insert(
1, B);
    cout
<<"After B=A.Right(2); A.Insert(1, B) : ";
    Print(Iterator);
    A
=A.Sub(1,3);
    cout
<<"After A=A.Sub(1,3) : ";
    Print(Iterator);
    B
=A.ToArray();
    A
=A+B;
    cout
<<"After B=A.ToArray(); A=A+B : ";
    Print(Iterator);
    cout
<<"last index of 2 is "<<A.LastIndexOf(2)<<endl;
    _getch();
}

結果如下:

1
After A.Add(2) : 1 2
A contains 1
A doesn't contain 3
index of 1 is 0
index of 3 is -1
After A.Insert(0,0) : 0 1 2
After A.Insert(1,3) : 0 3 1 2
After A.RemoveAt(1) : 0 1 2
After B=A.Right(2); A.Insert(1, B) : 0 1 2 1 2
After A=A.Sub(1,3) : 1 2 1
After B=A.ToArray(); A=A+B : 1 2 1 1 2 1
last index of 2 is 4

 

 

C#在用delegate的確很爽,無奈C++沒得爽,本來很天真地想實現一下功能

template<typename _Type>
class List : public IList<_Type>
{
  template
<typename T>
  typedef 
bool Predicate(T& Object);

  
bool Exists(Predicate<_Type> Match)
  {
    
if (Match(*this)) return true;
    
else return false;
  }
};

 網上搜了下,才發現C++沒有這個特性,delegate暫時湊不出來,于是折騰了個難看的辦法,沒想到還是不行,至今不知道為啥

template<typename _Type>
class List : public IList<_Type>
{
  
bool Exists ((bool(*Predicate)(List<_Type>& Object)) Match)
  {
    
if (Match(*this)) return true;
    
else return false;
  }
};

 

歡迎大家來噴哈~

posted on 2009-09-23 11:20 Lyt 閱讀(2007) 評論(5)  編輯 收藏 引用 所屬分類: 數據結構

FeedBack:
# re: Collection::List
2009-09-23 14:56 | OwnWaterloo
如果以GP而非OOP的方式構建集合,你會發現C++的template即使沒有delegate也會很爽。C#沒得這么爽。   回復  更多評論
  
# re: Collection::List
2009-09-23 18:30 | 陳梓瀚(vczh)
@OwnWaterloo
我的VL++2.0推掉重寫,最近再寫3.0,實踐了一下同時GP+OOP,最后發現可行,而且我也實現了delegate,將linq搬過來了。  回復  更多評論
  
# re: Collection::List
2009-09-23 18:35 | OwnWaterloo
@陳梓瀚(vczh)
支持推倒……  回復  更多評論
  
# re: Collection::List
2009-09-24 12:15 | 陳梓瀚(vczh)
@OwnWaterloo
原來同是宅人……  回復  更多評論
  
# re: Collection::List[未登錄]
2009-09-28 11:21 | a
不用GP, 很多情況下都可以忽略Iterator,很多人用it無非是rbtree。我就喜歡直接用int遍歷hash(directory), vector, list之類的。。  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            最新亚洲激情| 一本大道久久a久久综合婷婷 | 国产日韩欧美在线播放| 一本久久综合亚洲鲁鲁| 亚洲日本在线观看| 欧美理论在线| 亚洲一区二区高清| 亚洲国产欧美日韩精品| 欧美不卡在线视频| 欧美大片免费观看| 宅男精品视频| 亚洲一区二区视频在线观看| 国产伦精品一区二区三区照片91| 久久久久五月天| 久久久无码精品亚洲日韩按摩| 亚洲成色最大综合在线| 亚洲自拍偷拍麻豆| 亚洲欧美综合国产精品一区| 国产一区清纯| 亚洲国产精选| 国产精品久久九九| 久久资源在线| 欧美日韩成人在线播放| 欧美一区国产在线| 麻豆国产精品777777在线| 99re这里只有精品6| 亚洲欧美日本国产专区一区| 激情综合在线| 一本久道久久久| 国产一区二区成人久久免费影院| 欧美激情黄色片| 国产精品久久| 欧美国产在线电影| 国产精品乱码一区二三区小蝌蚪| 免费在线看一区| 国产精品电影网站| 亚洲第一页在线| 久久精品免费电影| 亚洲视频图片小说| 久久夜色精品国产| 亚洲伊人伊色伊影伊综合网| 久久精品国产综合精品| 亚洲视频二区| 蜜桃av一区| 久久久久久伊人| 欧美日韩影院| 欧美激情第4页| 国产在线欧美日韩| 亚洲一区二区精品| 99精品黄色片免费大全| 久久免费国产精品1| 性久久久久久久久| 欧美日韩午夜在线| 欧美激情五月| 在线观看国产成人av片| 午夜综合激情| 羞羞答答国产精品www一本| 欧美激情综合五月色丁香| 美女成人午夜| 精品不卡一区| 欧美一级免费视频| 欧美伊人久久| 国产亚洲精品一区二555| 在线综合视频| 亚洲一区二区三区免费在线观看| 欧美大片在线观看一区| 欧美二区在线播放| 好吊日精品视频| 欧美综合77777色婷婷| 欧美在线免费视频| 美女日韩在线中文字幕| 久久综合99re88久久爱| 国产一区二区成人| 欧美在线观看视频一区二区| 欧美在线短视频| 国产午夜精品视频| 欧美在线观看天堂一区二区三区| 久久精品二区亚洲w码| 国产欧美精品日韩| 欧美在线免费播放| 久久综合亚洲社区| 亚洲国产日韩一级| 欧美国产日本韩| 亚洲人成艺术| 亚洲一区成人| 国产人成一区二区三区影院| 午夜久久福利| 欧美xart系列高清| 99成人在线| 国产精品高潮呻吟| 欧美一区二区三区在| 蜜臀av一级做a爰片久久| 亚洲激情校园春色| 欧美日韩一区二区三区免费| 亚洲视频axxx| 免费观看国产成人| 99re热这里只有精品视频| 欧美午夜精品久久久| 午夜在线精品| 亚洲国产成人精品久久| 一区二区三区视频在线| 国产欧美不卡| 欧美国产第二页| 亚洲视频欧美视频| 女人色偷偷aa久久天堂| 一级成人国产| 国产一区二区三区在线观看免费视频| 久热精品在线| 亚洲一区999| 欧美二区乱c少妇| 性高湖久久久久久久久| 亚洲国产毛片完整版 | 欧美成ee人免费视频| 99综合电影在线视频| 久久久久久电影| 亚洲视频一起| 亚洲人成网站精品片在线观看| 国产精品免费电影| 欧美www在线| 久久99伊人| 亚洲一区bb| 亚洲美女视频网| 免费观看成人网| 久久精品国产欧美激情| 在线亚洲免费| 亚洲国产天堂久久综合| 国产午夜精品视频| 国产精品理论片| 欧美日韩国产一中文字不卡| 久久久久久久久岛国免费| 亚洲一级二级| 日韩一级欧洲| 亚洲国产欧美日韩另类综合| 久久一区二区精品| 久久国产精品亚洲va麻豆| 亚洲视频免费看| 99成人免费视频| 亚洲品质自拍| 亚洲高清资源| 在线观看国产精品淫| 国产一区二区三区无遮挡| 国产精品伦理| 国产精品无码永久免费888| 欧美人与性动交a欧美精品| 美女国产精品| 快she精品国产999| 久久美女艺术照精彩视频福利播放| 欧美一区二区视频在线观看| 亚洲欧美日韩第一区| 亚洲欧美日本在线| 午夜在线播放视频欧美| 午夜视频一区二区| 久久av免费一区| 久久高清一区| 久久综合色播五月| 免费欧美电影| 欧美日韩第一页| 欧美三级乱码| 国产精品男女猛烈高潮激情| 国产精品久久毛片a| 国产乱肥老妇国产一区二| 国产亚洲精品久久久| 韩国三级在线一区| 亚洲欧洲美洲综合色网| 亚洲另类视频| 亚洲欧美999| 久久久久久亚洲精品中文字幕| 久久亚洲国产精品一区二区| 欧美成人精品不卡视频在线观看| 亚洲大片免费看| 亚洲精品在线看| 亚洲综合视频在线| 久久久久国产精品麻豆ai换脸| 久久综合狠狠| 欧美吻胸吃奶大尺度电影| 国产女人精品视频| 亚洲成人在线视频播放| 99视频精品在线| 欧美一级专区免费大片| 久久一二三国产| 日韩一级黄色片| 久久高清一区| 欧美片在线观看| 国产一区二区三区久久久久久久久| 亚洲国产精品第一区二区| 亚洲婷婷综合久久一本伊一区| 欧美在线综合视频| 亚洲丰满在线| 午夜伦理片一区| 欧美精品麻豆| 国内成人精品视频| 一本大道久久a久久综合婷婷| 久久精品国产99精品国产亚洲性色| 欧美大尺度在线观看| 亚洲特级毛片| 欧美成人69av| 国外成人性视频| 亚洲女同在线| 亚洲人成艺术| 久久米奇亚洲|