Thread: Do we really need iterator?

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216

    Do we really need iterator types?

    Have a look at the following code first
    Code:
    #include <vector>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main () {
        vector<string> text(5, "Hi~");    // empty vector
         
        /* for (vector<string>::iterator iter = text.begin();
                   iter != text.end(); ++iter) { */
        /* by defining a string pointer, this program works the same as defining an iterator */
        for (string *iter = text.begin(); iter != text.end(); ++iter) {
            cout<<*iter<<endl;
        }
    
        return 0;
    }
    By defining a string pointer, this program works the same as defining an iterator. As defining an iterator is more complex than defining a string pointer, why do we need iterator types? I really can't understand. Could somebody be kind enough to tell me? Thanks in advance.
    Last edited by Antigloss; 06-13-2005 at 04:36 AM.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    You're assuming that the STL vector is storing all of the strings in a single segment of continuous memory. I don't think that is a requirement of the vector class. It's safer to use the iterator. With the vector, you could also use normal array indexing from 0 to vector.length().

    My main use of iterators is in serializing maps for I/O. If you change your vector to a map, the code that uses iterators will still hit every element. The code that uses a string pointer will probably fail. In my opinion, it's easier to use iterators because you can change your container with relative ease.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The vector class is the only STL container that guarantees contiguous memory*. Iterators are an abstraction that is used through all STL containers, and can be used with other types as well. There are many algorithms, for example, that take iterators that might be for a vector, or a list, or a deque, or a set, that will work regardless of which container they are from. The pointer technique will only work for a vector, and then only if you're lucky.

    Since the iterator is not guaranteed to be implemented as a basic pointer, when you switch platforms, your code might not compile. Some implementations might use a wrapper around the pointer to keep track of other information, and in those cases the assignment of the return value of begin() to the string pointer will not compile.

    So in the end, the iterator provides an extra layer of abstraction that is useful for many reasons. And besides, looking at your code, I don't see how the iterator version is any more complex than the string pointer version, except maybe for a few extra letters. If they bother you then you can always use typedefs to make the line shorter.

    * Note: I'm not positive that this is explicitly guaranteed by the standard yet, but for all intents and purposes you can use that information.
    Last edited by Daved; 06-13-2005 at 10:17 AM.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The first technical amendment to the C++ standard guarantees the continuous storage for vector, and so will C++0x. C++98 doesn't, but there is AFAIK no existing STL that doesn't use continuous storage, because the way C++98 formulates vector requirements, you'd actually have to go out of your way to come up with a different solution.

    However, Daved's objections still apply. In particular, many STLs use a non-pointer iterator for vector in debug mode, one that actually does bounds checking.

    Typedefs are the way to go.

    Anyway, try the same with a std::list. One of the nice things about iterators are the uniform interface.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Iterator
    By MarkZWEERS in forum C++ Programming
    Replies: 19
    Last Post: 05-19-2008, 11:16 PM
  3. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  4. Link list library
    By Brighteyes in forum C Programming
    Replies: 4
    Last Post: 05-12-2003, 08:49 PM
  5. Search and Build Tree
    By 1999grandamse in forum C++ Programming
    Replies: 17
    Last Post: 11-14-2002, 01:36 PM