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

Thread: C++ iterator

  1. #1
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    C++ iterator

    I was meaning to understand in C++ what are iterator , I have gone through whats on the web , however still did not get it. could someone please provide explanation , with some example ,
    I have always found , seeing an example helps me better than just plain words.

  2. #2
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: C++ iterator

    An iterator object is associated to a container so that it points to one element of that container or to an invalid pseudo element which signals the end of the iteration. The iterator provides a function 'next' - mostly via the ++ operator - which makes the iterator object pointing to the next element.

    Code:
    // the container
    std::vector<int> cont;
    cont.push_back(1);
    cont.push_back(2);
    // the iterator (pointing to nowhere and not yet associated to the container)
    std::vector<int>::iterator iter;
    
    for (iter = cont.begin();  // now iter is associated to the container and points to first element
          iter !=cont.end();     // and runs until the iter is end iterator of the container 
          ++iter)                     // points to next element after each cycle
    {
         int i = *iter;    // get value of element from iterator by dereferencing (it is 1 or 2)
         std::cout << i << std::endl;
    }

  3. #3
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: C++ iterator

    thank you , except for the obvious , as looping through the container , what are there practical uses.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C++ iterator

    Quote Originally Posted by aamir121a View Post
    thank you , except for the obvious , as looping through the container , what are there practical uses.
    Well, looping through a container seems to be a very practical use to me, especially for a container such as a std::list or std::map that can't be simply indexed like a vector can.

    Due to their ability to make any kind of container "look the same", iterators are also used in almost every STL algorithm such as std::transform() as a generic way to specify data without caring where it's stored.

  5. #5
    Join Date
    Mar 2010
    Location
    Melbourne Australia
    Posts
    454

    Re: C++ iterator

    thanks again much appreciated

  6. #6
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C++ iterator

    Custom iterators can also be useful when you want to iterate from something other than 'first to last'. Many standard containers have a reverse iterator, where incrementing will step backwards through the container. This means that only one algorithm need be written which assumes the availability of a ++ operator in the iterator. It's up to the iterator to define what ++ actually does.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

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