Thread: C++ STL: Why use member iterators

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    C++ STL: Why use member iterators

    I just started C++ last week (but have been goofing around with other programming languages). I just started looking around the STL (vectors in specific) and why use the iterators? It seems like less typing to just make your own 'for loop' if you are going to step through an array. You can use the size function to check that you don't go out of bounds.

    Are iterators quicker because the processor doesn't have to jump around as much or something (me totally guessing)?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's fine to use array syntax to loop through a vector.

    There are more advanced things you can do with iterators, and they are a more generic concept. If you have a function (like sort for example) that works on a container, then you can have it take iterators so that it works with any container that supports that kind of iteration.

    Also, in some cases you may want to use the iterator version to loop through a vector because it can be easier to update the code if you switch to a different container type later.

  3. #3
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Also, keep in mind that some containers only support iterators, such as std::list.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Daved View Post
    It's fine to use array syntax to loop through a vector.

    There are more advanced things you can do with iterators, and they are a more generic concept. If you have a function (like sort for example) that works on a container, then you can have it take iterators so that it works with any container that supports that kind of iteration.

    Also, in some cases you may want to use the iterator version to loop through a vector because it can be easier to update the code if you switch to a different container type later.
    Also, you can pass an iterator to a function and it can use it. Try doing that with just an index...

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    9
    Thank you for answering. I went ahead and played around with them last night. Orignally it just looked like extra typing. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM