Wrapping linked lists in iterators

后端 未结 5 725
青春惊慌失措
青春惊慌失措 2021-01-20 01:21

A set of APIs that I commonly use follow a linked-list pattern:

struct SomeObject
{
    const char* some_value;
    const char* some_other_value;
    SomeObj         


        
5条回答
  •  Happy的楠姐
    2021-01-20 01:41

    My advice: Trash this and use an existing slist<> implementation. IIRC, it will be in C++1x, so your compiler(s) might already support it. Or it might be in boost. Or take it from someplace else.

    Wherever you get it from, what you get has all these problems already solved, is likely very well tested, therefore bug-free and fast, and the code using it is easily recognizable (looking at it many of us would immediately see what it does, because it's been around for a while and it's going to to be part of the next standard).

    The last time I wrote my own list class was before the STL became part of the C++ standard library.

    Ok, since you're stuck with the API you have, here's something that might get you started:

    class MyObjectList
    {
    public:
        typedef SomeObject value_type;
        // more typedefs
    
        class iterator {
        public:
            typedef SomeObject value_type;
            // more typedefs
    
            iterator(SomeObject* pObj = NULL)
                                        : pObj_(pObj) {}
            iterator& operator++()        {if(pObj_)pObj_ = pObj_->next;}
            iterator  operator++(int)     {iterator tmp(*this);
                                          operator++();
                                          return tmp;}
            bool operator==(const iterator& rhs) const
                                          {return pObj_ == rhs.pObj_;}
            bool operator!=(const iterator& rhs) const
                                          {return !operator==(rhs);}
                  value_type& operator*() {return pObj_;}
        private:
            SomeObject* pObj_;
        };
    
        class const_iterator {
        public:
            typedef SomeObject value_type;
            // more typedefs
            const_iterator(const SomeObject* pObj = NULL)
                                        : pObj_(pObj) {}
            iterator& operator++()        {if(pObj_)pObj_ = pObj_->next;}
            iterator  operator++(int)     {iterator tmp(*this);
                                          operator++();
                                          return tmp;}
            bool operator==(const iterator& rhs) const
                                          {return pObj_ == rhs.pObj_;}
            bool operator!=(const iterator& rhs) const
                                          {return !operator==(rhs);}
            const value_type& operator*() {return pObj_;}
        private:
            const SomeObject* pObj_;
        };
    
        MyObjectList()                   : list_() {GetObjectList(&list_;);}
        ~MyObjectList()                    {FreeObjectList(list_);}
              iterator begin()             {return list_ ?       iterator(list_)
                                                         :       iterator();}
        const_iterator begin() const       {return list_ ? const_iterator(list_)
                                                         : const_iterator();}
              iterator end  ()             {return       iterator(getEnd_());}
        const_iterator end  () const       {return const_iterator(getEnd_());}
    
    private:
        SomeObject* list_;
    
        SomeObject* getEnd_()
        {
            SomeObject* end = list_;
            if(list_)
                while(end->next)
                    end = end->next;
            return end;
        }
    };
    

    Obviously, there's more to this (for example, I believe const and non-const iterators should be comparable, too), but that shouldn't be hard to add to this.

提交回复
热议问题