# Thread: iterators & lists

1. ## 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. Have two iterators?

3. 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).

4. Originally Posted by lawrenced
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. 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.

6. Originally Posted by laserlight
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

7. 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.

8. Originally Posted by laserlight
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. Originally Posted by EVOEx
Which brings me a question of my own: are random access iterators required to have operator+?
Yes.

10. >> 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.

11. 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;
}```

Popular pages Recent additions