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,708
    >> 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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    24,885
    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 08:25 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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-24-2007, 12:05 AM
  2. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 03:37 PM
  3. Replies: 4
    Last Post: 01-16-2006, 05:58 PM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  5. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 09:04 PM

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