stl源码剖析 详细学习笔记deque(2)

//---------------------------15/3/13----------------------------

                self&operator++()

                {

                    ++cur;

                   if(cur==last)

                    {

                        set_node(node+1);

                        cur=first;

                    }

                   return *this;

                }

                

                self operator++(int)   //这里不能返回引用,因为 tmp是临时变量,作用域结束tmp就消失了

                {

                    self tmp=*this;

                    ++*this;

                   return tmp;

                }

                

                

                self&operator--()

                {

                   if(cur=first)

                    {

                        set_node(node-1);

                        cur=last;

                    }

                    --cur;

                   return *this;

                }

                

                selfoperator--(int)

                {

                    self tmp=*this;

                    --*this;

                   return temp;

                }

                

                

                

               /*

                    1.看能否在不改变node的情况下进行指针位置的移动

                 

                    2.调整node

                        offset==(n+x) -->> 最后的位置==first + (n + x)

                        x代表cur 距离first的距离,也就是cur当前的下标

                        

                        (offset>0): offset/diffenrence_type(buffer_size())

                        -->>    y==(n + x)/(sz) -->>移动的node节点个数为y

                        (offset<0)   -difference_type((-offset -1) / buffer_size()) - 1

                        -->>    y==-(-n - x -1)/(sz) -1 -->> y==(n + x +1)/(sz) -1

                        不同于(offset<0)的情况,因为当前处于first位置,移动的距离在

                            -1到-(sz)是都应该移动一个node节点所以才取上式子;

                 

                    3.调整cur

                        (offset>0) :offset==(n + x)

                        node_offset*difference_type(buffer_size())==n + x -r)

                        (r代表余数)

                        -->>z(位移量)==r

                        (offset<0)  :offset==(n + x)

                        node_offset*difference_type(buffer_size())==(n + x +r-sz)

                        -->>z(位移量)==-sz + r

                        cur =z

             

                 */

                self&operator+=(difference_type n)

                {

                    difference_type offset =n+(cur - first);

                   if(offset >= 0 && offset < difference_type(buffer_size()))

                        cur+=n;

                   else

                    {

                        difference_type node_offset=

                          offset >0 ? offset/difference_type(buffer_size())

                            : -difference_type((-offset -1) / buffer_size()) -1 ;

                        

                        set_node(node + node_offset);

                        

                        cur = first +(offset - node_offset * difference_type(buffer_size()));

                    

                    }

                   return *this;

                }

                

                selfoperator+(difference_type n) const

                {

                    self tmp=*this;

                   return tmp+=n;

                }

                

                self &operator -=(difference_type n){return *this +=-n;}

                

                selfoperator-(difference_type n) const

                {

                    self tmp =*this;

                   return tmp-=n;

                }

                

                referenceoperator[](difference_type)const{return *(*this +n);}

                

               bool operator==(const  self& x)const{return cur==x.cur;}

               bool operator!=(const self& x)const {return !(*this==x);}

               bool operator<(const self& x)const

                {

                   return (node==x.node)?(cur < x.cur) :(node < x.node);

                }

                

                

               template<class T,class Alloc = alloc,size_t BufSiz=0>

               class deque

                {

               public:

                   typedef T value_type;

                   typedef value_type* pointer;

                   typedef size_t size_type;

                   typedef __deque_iterator<T,T&,T*,BufSiz> iteratoer;

                    

               protected:

                   typedef pointer* map_pointer;

                    

                    iteratoer start;

                    iteratoer finish;

                    map_pointer map;

                    

                    size_type map_size;

                    

               public:

                    iteratoer begin(){return start;}

                    iteratoer end() {return finish;}

                    

                    referenceoperator[](size_type n)

                    {

                       return start[difference_type(n)];

                    }

                    

                    reference front(){return *start;}

                    reference back()

                    {

                        iteratoer tmp=finish;

                        --tmp;

                       return *tmp;

                        

                        //上面三行不改为 return *(finish-1)是因为operator -(difference_type n)

                       //    的操作比--复杂很多

                    }

                    

                    size_type size()const {return finish - start;;}//两个;是手误??

                   }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值