CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Threaded View

  1. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: creating an iterator to search a vector of list<T>

    Quote Originally Posted by yellowMonkeyxx View Post
    I have a template class defined as the following:

    Code:
    template <class T, class HF>
    class HashTable
    {
       private:
    
       vector<list<T> > bucket;
    
      public:
    
      //public methods
      bool find(const T&) const;
      
    };
    What I'm trying to do is create an iterator to traverse the contents of bucket.

    I've tried a few variations of defining a list iterator, such as
    Code:
    list<T>::const_iterator it;
    but haven't found anything that is correct? Any ideas?
    Bucket is a vector, so your iterator has to be a vector iterator:

    Code:
    vector<list<T> >::const_iterator it
    Each iterator will point to a list. If you want to iterate on the items in that list, create a list iterator:

    Code:
    vector<list<T> >::const_iterator itVect = bucket.begin();
    vector<list<T> >::const_iterator itVectEnd = bucket.end();
    for ( ; itVect != itVectEnd ; ++itVect)
    {
         list<T>const_iterator itList = itVect->begin();
         list<T>const_iterator itListEnd = itVect->end();
         for ( ; itList != itListEnd ; ++itList )
        {
            //Do things with itListEnd
        }
    
    }
    If what you wanted was an actual new iterator type that traverses both, with HashTable::begin() defined as bucket.begin()->begin(), and HashTable::end() defined as (bucket.end()-1)->end(), well I'm afraid you'll have to write your entire own iterator class. I know there are some things in boost that can speed up the process (for example, if you write operator+=, there is a boost class which will automatically generate operator+), I wouldn't their names, and I don't know if they are worth the trouble.

    Does that answer your question?
    Last edited by monarch_dodra; April 21st, 2010 at 07:29 PM.

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