Thread: iterators & lists

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    iterators & lists

    if I am running through a list

    list<Class A*> lclassA;
    list<Class A*>::iterator = iter;

    for(iter = lclassA.begin(); iter!=lclassA.end(); ++iter)
    {
    var1 = (*iter)->Getabba();
    var2 = (*iter+1)->Getabba()

    }

    how can I increment iter to look at iter+1 but maintain the position of iter. as in if my list has 4 vars, I want to compare 1 to 2, the 2 to 3, the 3 to 4, then stop.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have two iterators?

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I have also used these functions:

    Code:
    template <class ListIterator>
    ListIterator previous(ListIterator it)
    {
        return --it;
    }
    
    template <class ListIterator>
    ListIterator next(ListIterator it)
    {
        return ++it;
    }
    
    ...
    if (*it == sth && *next(it) == sth)
    In your case having two might be simpler (and you would be checking the wrong one for the end).
    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).

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by lawrenced View Post
    if I am running through a list

    list<Class A*> lclassA;
    list<Class A*>::iterator = iter;

    for(iter = lclassA.begin(); iter!=lclassA.end(); ++iter)
    {
    var1 = (*iter)->Getabba();
    var2 = (*iter+1)->Getabba()

    }

    how can I increment iter to look at iter+1 but maintain the position of iter. as in if my list has 4 vars, I want to compare 1 to 2, the 2 to 3, the 3 to 4, then stop.
    try this:

    Code:
    list<Class A*> lclassA;
    list<Class A*>::iterator = iter;
    
    for(iter = lclassA.begin(); iter!=lclassA.end(); ++iter)
    {
    var1 = (*iter)->Getabba();
    var2 = (*(iter+1))->Getabba()
    
    }

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Elkvis' proposed code will not work (aside from the errors repeated from the original code snippet) because std::list iterators do not have operator+ overloaded since they are not random access iterators.
    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

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    Quote Originally Posted by laserlight View Post
    Elkvis' proposed code will not work (aside from the errors repeated from the original code snippet) because std::list iterators do not have operator+ overloaded since they are not random access iterators.
    yep.... i don't think many people know that. I have seen a lot of topics started in some forum with the same problem
    you can't use iteratros on lists
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Hussain Hani
    you can't use iteratros on lists
    That is false, as tabstop implied and anon outlined in a code example.
    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

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by laserlight View Post
    Elkvis' proposed code will not work (aside from the errors repeated from the original code snippet) because std::list iterators do not have operator+ overloaded since they are not random access iterators.
    Which brings me a question of my own: are random access iterators required to have operator+? Anywhere I tried they were present, but I couldn't find anything about them being required in the standard. Or maybe I just didn't look properly.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by EVOEx
    Which brings me a question of my own: are random access iterators required to have operator+?
    Yes.
    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

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Which brings me a question of my own: are random access iterators required to have operator+? Anywhere I tried they were present, but I couldn't find anything about them being required in the standard. Or maybe I just didn't look properly.

    I believe so.
    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;
    }

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    Table 7--Random access iterator requirements (in addition to bidirectional iterator)
    
      +------------------------------------------------------------------------------------+
      |expression       return type         operational             assertion/note         |
      |                                      semantics            pre/post-condition       |
      +------------------------------------------------------------------------------------+
      |r += n       X&                    { Distance m =                                   |
      |                                   n;                                               |
      |                                     if (m >= 0)                                    |
      |                                       while (m--)                                  |
      |                                   ++r;                                             |
      |                                     else                                           |
      |                                       while (m++)                                  |
      |                                   --r;                                             |
      |                                     return r; }                                    |
      +------------------------------------------------------------------------------------+
      |a + n                              { X tmp = a;                                     |
      |             X                       return tmp +=   a + n == n + a.                |
      |                                   n; }                                             |
      |n + a                                                                               |
      +------------------------------------------------------------------------------------+
      |r -= n       X&                    return r += -n;                                  |
      +------------------------------------------------------------------------------------+
      |a - n        X                     { X tmp = a;                                     |
      |                                     return tmp -=                                  |
      |                                   n; }                                             |
      +------------------------------------------------------------------------------------+
      |b - a        Distance              (a<b)? dis-       pre: there exists a value n of |
      |                                   tance(a,b):       Distance such that a + n == b. |
      |                                   -distance(b,a)    b == a + (b - a).              |
      +------------------------------------------------------------------------------------+
      |a[n]         convertible to T      *(a + n)                                         |
      +------------------------------------------------------------------------------------+
      |a < b        convertible to bool   b - a > 0         < is a total ordering relation |
      +------------------------------------------------------------------------------------+
      |a > b        convertible to bool   b < a             > is a total ordering relation |
      |                                                     opposite to <.                 |
      +------------------------------------------------------------------------------------+
      |a >= b       convertible to bool   !(a < b)                                         |
      +------------------------------------------------------------------------------------+
      |a <= b       convertible to bool   !(a > b)                                         |
      +------------------------------------------------------------------------------------+
    Hey, there are things I didn't know:

    Code:
    #include <vector>
    
    int main()
    {
        std::vector<int> vec(10);
        std::vector<int>::iterator it = vec.begin();
        it[1] = 3;
    }
    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. Dynamic Lists? Is it possible? Is it a logical solution?
    By arcaine01 in forum C++ Programming
    Replies: 10
    Last Post: 07-23-2009, 01:08 AM
  2. intersection of two STL lists
    By misterMatt in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2009, 12:25 AM
  3. Question about Linked lists of lists
    By hear_no_evil in forum C Programming
    Replies: 2
    Last Post: 11-08-2004, 02:49 AM
  4. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM