CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2003
    Posts
    615

    STL algorithm adapter

    Hello,

    An external library has the following header file;
    Code:
    template <typename T> 
    class MyCollection
    {  
    public:
      MyCollection();
    
      const T& operator[](unsigned long index) const;
      unsigned long length() const;
      
    private:
      std::vector<void*> v;
    };
    To iterate this collection I can obviously use a loop. However, would it be possible to use STL algorithms functions on this collection? A lot of those methods use an iterator as input parameter, would be possible to make an adapter class to make this work?
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: STL algorithm adapter

    Well, it may be easiest if you can convince the library author to augment the class template with iterator support. If that cannot be done, then one way out is to write your own iterator class template, e.g.,
    Code:
    template <typename T>
    class MyCollectionIterator : public std::iterator<std::input_iterator_tag, T>
    {
    public:
        explicit MyCollectionIterator(MyCollection<T>& collection_, unsigned long index_=0) :
            collection(&collection_), index(index_) {}
    
        const T& operator*() const
        {
            return (*collection)[index];
        }
    
        const T* operator->() const
        {
            return &(operator*());
        }
    
        MyCollectionIterator<T>& operator++()
        {
            ++index;
            return *this;
        }
    
        bool operator==(const MyCollectionIterator<T>& other) const
        {
            return index == other.index && collection == other.collection;
        }
    private:
        MyCollection<T>* collection;
        unsigned long index;
    };
    
    template <typename T>
    MyCollectionIterator<T> operator++(MyCollectionIterator<T>& iterator, int)
    {
        MyCollectionIterator<T> temp(iterator);
        ++iterator;
        return temp;
    }
    
    template <typename T>
    bool operator!=(const MyCollectionIterator<T>& lhs, const MyCollectionIterator<T>& rhs)
    {
        return !(lhs == rhs);
    }
    
    template <typename T>
    MyCollectionIterator<T> begin(MyCollection<T>& collection)
    {
        return MyCollectionIterator<T>(collection);
    }
    
    template <typename T>
    MyCollectionIterator<T> end(MyCollection<T>& collection)
    {
        return MyCollectionIterator<T>(collection, collection.length());
    }
    
    // Example usage:
    int main()
    {
        MyCollection<int> collection;
        // ...
        std::copy(begin(collection), end(collection),
                  std::ostream_iterator<int>(std::cout, "\n"));
    }
    Given the interface of MyCollection, you probably want a random access iterator, not an input iterator, but I simplified for this example. Furthermore, it looks as if the MyCollection class template actually is an immutable type, in which case perhaps it would be simpler to just provide a const_iterator version of the iterator instead of both versions.
    Last edited by laserlight; October 13th, 2013 at 05:59 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jan 2003
    Posts
    615

    Re: STL algorithm adapter

    Thank you.

    That looks very interesting.

    Will study it in more detail the coming week.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  4. #4
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: STL algorithm adapter

    Have a look at boost::iterator_facade.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured