Moving an iterator forward

This is a discussion on Moving an iterator forward within the C++ Programming forums, part of the General Programming Boards category; Hey all, I have a list of classes, and two iterators, it_cur and it_nxt. It_cur points to some element, and ...

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

    Moving an iterator forward

    Hey all,

    I have a list of classes, and two iterators, it_cur and it_nxt. It_cur points to some element, and I need it_nxt to point to the element after it_cur. Is there any simple built in way to do this? If not, would using the following function be alright?

    Code:
    list<TheClass>::iterator getNext(list<TheClass>::iterator it_cur)
    {
         return ++it_cur;
    }
    Or maybe just it_nxt = it_cur + sizeof(TheClass)?

    Cheers,

    Gabe

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    >> If not, would using the following function be alright?

    Yes, that would work fine (though you may want to check that it_cur != end( )).

    >> Or maybe just it_nxt = it_cur + sizeof(TheClass)?

    Definitly *not*. That would increment the iterator sizeof(TheClass) times. It works just like normal pointer arithematic, actually.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,310
    Quote Originally Posted by G4B3
    Is there any simple built in way to do this?
    One way would be:
    Code:
    it_nxt = it_cur;
    ++it_nxt;
    But once you have such an iterator pair, you could also write:
    Code:
    it_cur = it_nxt++;
    Quote Originally Posted by G4B3
    Or maybe just it_nxt = it_cur + sizeof(TheClass)?
    No. If the iterators are random access iterators, that would likely set it_nxt to a far more advanced position than desired. If not, that would likely result in a compile error since operator+ is not usually defined for say, the bidirectional iterators of a std::list (you would use std::advance() instead).
    Last edited by laserlight; 05-27-2009 at 07:25 AM.
    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

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    71
    advance() was the function I was looking for. Thanks, I completely missed it in the reference.

    Cheers,

    Gabe

    PS.: Sorry about the + sizeof(TheClass) thing, I mixed things up in my head and treated a list as an array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  3. Replies: 4
    Last Post: 01-16-2006, 04:58 PM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 04:46 AM
  5. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:04 PM

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