What is the purpose of iterators?

This is a discussion on What is the purpose of iterators? within the C++ Programming forums, part of the General Programming Boards category; This question has had me for a while: Why use iterators when you can access the elements directly. For example, ...

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up What is the purpose of iterators?

    This question has had me for a while: Why use iterators when you can access the elements directly. For example, suppose we want to fill an empty vector with a for loop. Why would we do it like this:

    Code:
    vector< int > v( 100 );
    for ( vector< int >::iterator it = v.begin(); it != v.end(); ++it )
    {
    	*it = 0;
    }
    ...when you can do it like this:

    Code:
    vector< int > v( 100 );
    for ( int i = 0; i < v.size(); ++i )
    {
    	v[i] = 0;
    }
    To me, the latter is much easier to read, much easier to type, just as optimised and just as safe. Another example is using a function that requires an iterator. For the erase() function, you can simply do this:

    Code:
    v.erase( b.begin() + i );
    Obviously I'm missing something here, so can someone point it out to me?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    In your first example, it would be enough to write:
    Code:
    vector<int> v(100);
    Or to be more explicitly:
    Code:
    vector<int> v(100, 0);
    But now, suppose later you want to "reset" the elements to say, 123. You can then #include <algorithm> and write:
    Code:
    std::fill(v.begin(), v.end(), 123);
    This is even "easier to read, much easier to type, just as optimised and just as safe". It also brings us to one answer to your question: you can use iterators to write function templates that implement generic algorithms.

    Quote Originally Posted by yaya
    For the erase() function, you can simply do this:
    In which case you are using an iterator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    Yeah, I understand that there are better ways to do this, but I just wanted a bare-bones example. I am assuming you want to go through all of the elements of the vector to do some sort of processing.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote Originally Posted by yaya
    Yeah, I understand that there are better ways to do this, but I just wanted a bare-bones example.
    My point is that iterators allow for the better ways to do this, and not only "to go through all of the elements of the vector", but to go through a range of elements via suitable iterators. This range could be a sub-range from a vector or some other container, or sometimes container objects are not even involved.

    Quote Originally Posted by yaya
    I am assuming you want to go through all of the elements of the vector to do some sort of processing.
    If you prefer to use array indexing to solve your particular problem, go ahead. It is not wrong.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    All containers do not support indexing. Take a set, for example. Or a map. You can't use indexes there.
    So how do you iterate through such containers? You do it through iterators.
    Iterator is a concept that defines a way to iterate over a range. So it makes sense to have it apply to all containers, not just those where index operators do not work.
    Some containers also support indices. They, too, have their uses.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,422
    Some of the vector functions require an iterator or you can't use them, insert, erase for example. So without them you cant use the full functionality of the type.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    While they do require iterators, they could just as well have accepted indices. For a vector, there is no advantage in using iterators.
    But for other containers, such as linked lists, there is a need. Sure, it could theoretically support indices, but then most operators would be O(N) instead of O(1).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    That was the information I was looking for. Thanks.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If all containers have a very different interface, this would make it impossible to write generic code (templates) to work with many container types.

    std::string supports both indices and iterators, though, for member functions (not that the abundance of overloads doesn't get confusing at times).

    For example, suppose we want to fill an empty vector with a for loop. Why would we do it like this:
    Yeah, iterators make for loops verbose (disregarding the fact that here there are alternative and better ways to fill containers with a value).

    The new standard will give you tools to make it look better:

    Code:
    //automatic variable type deduction
    for (auto it = vec.begin(); it != vec.end(); ++it) {
         *it = 0;
    }
    
    //range-based for loops
    for (int& i: vec) {
        i = 0;
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. State Manager
    By Dark_Phoenix in forum Game Programming
    Replies: 1
    Last Post: 03-25-2007, 09:18 AM
  2. Purpose of Operator Overloading
    By xmltorrent in forum C++ Programming
    Replies: 11
    Last Post: 08-09-2006, 06:23 PM
  3. vector of strings with iterators.
    By Mario F. in forum C++ Programming
    Replies: 6
    Last Post: 05-31-2006, 12:12 PM
  4. Writing Iterators...
    By DeepBlackMagic in forum C++ Programming
    Replies: 6
    Last Post: 05-29-2003, 09:31 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21