Thread: iterators

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    33

    iterators

    Could someone explain to me the purpose of the STL iterators and generally when they should be used? Thanks.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Could someone explain to me the purpose of the STL iterators
    Glorified pointers.

    >and generally when they should be used?
    When you use anything in the standard library that requires them.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    33
    Obviously. Exatly what requires them? I'm sure there are several reasons, one or two would help.
    “Focused, hard work is the real key to success. Keep your eyes on the goal, and just keep taking the next step towards completing it. " -John Carmack

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    They are useful when using the STL containers themselves. Any time you want to go through the contents of a container, or search the contents of a container, or basically access any data in the container, the container interface uses iterators. There are some exceptions, for example string and vector provide array like interfaces that let you access elements without using iterators. For the most part, though, iterators are usedto gain access to the data stored in a container.

    They are also useful when using STL containers with STL algorithms. Technically, any object that allows iteration over data and follows certain rules can be passed to most algorithms, but the iterators used in the STL containers are generally setup to work with the algorithms easily (or maybe the algorithms were set up to work with the container iterators ).

    Here's a random example of accessing and printing the elements of a map:
    Code:
    #include <iostream>
    #include <map>
    #include <cstdlib>
    #include <ctime>
    
    int main()
    {
        srand(time(0));
        std::map<int, int> m;
        for (int i = 0; i < 20; i++)
        {
            m.insert(std::make_pair(rand() % 100, rand() % 1000));
        }
    
        std::map<int, int>::const_iterator iterPair = m.begin();
        std::map<int, int>::const_iterator iterEnd = m.end();
        for (; iterPair != iterEnd; ++iterPair)
        {
            std::cout << "FirstNum: " << iterPair->first;
            std::cout << "\tSecondNum: " << iterPair->second << std::endl;
        }
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>Could someone explain to me the purpose of the STL iterators.....
    >>Exatly what requires them?

    Maybe the questions can be rephrased as "why are iterators used in preference to routine pointers in the development of the Standard Template Library?"

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    And the answer is... *dadadada

    Ask elad!

    Actually, you CAN (at least in the case of a vector, I'm not sure about other containers) substitute a pointer for an iterator, in MSVC - although I believe that would depend on the implementation.
    Last edited by Hunter2; 06-10-2004 at 02:05 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >is the answer because you can't move through the elements of the containers, like lists, with pointers?
    You can't move through an std::list with pointers unless you know something about the implementation. The elements of a list aren't required to be contiguous, so without something that knows how to move to the next element, a pointer isn't meaningful.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>You can't move through an std::list with pointers unless you know something about the implementation. The elements of a list aren't required to be contiguous, so without something that knows how to move to the next element, a pointer isn't meaningful.

    Is there another way of aligning elements of a list (or other container) besides contiguous and non-contiguous? Can't routine pointers deal with both contiguous and non-contiguous alignments? Is it possible to implement iterators that don't include a pointer as member of an iterator class? Aren't all user defined types (STL containers, iterators, whatever) made up of aggregates of primitive types, like int, char, pointers, etc.?

    So why not use a plain pointer instead of iterators with STL classes?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there another way of aligning elements of a list (or other container) besides contiguous and non-contiguous?
    Well, with the proper hardware you could store the elements of a list in the same location with each element in a parallel dimension. Then a pointer could easily be used to traverse the dimensions and you don't lose the benefit of constant time insertion and removal. You also get the benefit of random access too if you believe the theory of quantum mechanics. It's the best of both worlds! ...or all worlds, as it were.

    >Can't routine pointers deal with both contiguous and non-contiguous alignments?
    Yes, but not the same way. With contiguous storage all you have to do is p++ or p--, but with non-contiguous storage you would have to chase links by saying something like p.next or p.prev. Built-in pointer types don't allow you to do both with the same operation.

    >Is it possible to implement iterators that don't include a pointer as member of an iterator class?
    Yes. The iterator for a container class can be implemented any way as long as the proper behavior and interface is satisfied.

    >Aren't all user defined types (STL containers, iterators, whatever) made up of aggregates of primitive types, like int, char, pointers, etc.?
    Yes.

    >So why not use a plain pointer instead of iterators with STL classes?
    Because STL classes may not be amenable to being traversed by a pointer. A good example is a list. With iterators the defined behavior is to allow ++it and --it, but if the container is implemented as a double linked list then ++p and --p won't work if you use a plain pointer.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    To paraphrase, to make sure I understand the answer: among other considerations, with iterators the desired behavior is to allow both ++ and --, irrespective of memory allocation pattern of the elements (contiguous vs non-contiguous), whereas with pointers you can only use ++ and -- if memory allocation is contiguous.

    Thanks.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >To paraphrase, <snip>
    Yes.
    My best code is written with the delete key.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The answer to the first part of your question is that the two are equivalent. It's a matter of style as to which you use. The answer to the second part is that it's convenient to have a single object represent a related pair of items, such as in an asociative array.
    My best code is written with the delete key.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>i don't understand why they say this
    I believe it's for the sake of language portability; that's how it has to be declared in C, but in C++ it makes no difference.

    >>and why do we need to have a strucrure called Pair
    I don't know why there's a typedef for a Pair<int, float> but I do know that std::map's use Pair's a lot (the first in the pair is the key, and the second is the value).
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I believe it's for the sake of language portability
    Sure, using the struct keyword goes toward compatibility, but the instant you get to the template arguments, compatibility with C goes out the window.

    >I don't know why there's a typedef for a Pair<int, float>
    For the same reason one would do this:
    Code:
    typedef std::map< string, pair<int, float> > point_map;
    So we can do things like this:
    Code:
    point_map::iterator it;
    Instead of
    Code:
    std::map< string, pair<int, float> >::iterator it;
    >but why did they make a function "make_pair() when you have a constructor that does the same job?
    For convenience.
    My best code is written with the delete key.

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Yes.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  2. Using reverse iterators in algorithms
    By 0rion in forum C++ Programming
    Replies: 1
    Last Post: 02-27-2006, 03:19 AM
  3. Writing Iterators...
    By Geolingo in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  4. accessing vector elements: iterators are faster?
    By Captain Penguin in forum C++ Programming
    Replies: 1
    Last Post: 11-28-2002, 02:27 PM
  5. Generic Progpammimg Iterators
    By rip1968 in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2002, 10:20 AM