0

I'm using std::list to create my lists. Each item in the linked list contains a linked list of char. Each list can have a different number of chars.

I want to iterate through 3 elements of the list at a time using an iterator for each list. When I'm iterating through it I want to be moving along the full length of the middle line and checking the char in the line above and below at the same time.

e.g. if the following are the contents of my linked list of linked list of char,

  1. abcdef
  2. the quick brown fox
  3. zxy

I want to be iterating through the 2nd list (middle) and checking the above and below characters. e.g. t - a is upper and z is lower, h - b is upper and x is lower, etc. The problem is when list 3 and 1 stop and I still want to iterate through to the end of list 2 without getting errors for lists 1 and 3 finishing.

I'm not too sure how to go about this as I originally had a for loop for my middle list and was manually incrementing the upper and lower lists but ran into some issues when the upper or lower lists had a smaller number of elements than my middle list did.

e.g. I had something like this:

list<char>::const_iterator i, u, l
u = upper_row.begin();
l = lower_row.begin();
for (i = middle_row.begin(); i != middle_row.end(); i++) 
{
  // code to do something with upper, middle and lower lists
  u++;
  l++
}
Jade L
  • 167
  • 2
  • 9
  • 1
    What do you want to do when past the end of the outer lists? – Yakk - Adam Nevraumont Apr 28 '14 at 10:28
  • @Yakk When I past the end of either the upper or lower lists, I want to stop iterating through whichever has stopped and continue iterating through the middle and any list that hasn't ended. – Jade L Apr 28 '14 at 10:31
  • What does your phrase "t - a is upper" mean? I did not find character t in the upper string. Could you elaborate what do you want? – Vlad from Moscow Apr 28 '14 at 10:39
  • @VladfromMoscow I meant 't' from the middle string has 'a' from the upper string and 'z' from the lower string at that position. – Jade L Apr 28 '14 at 10:41
  • @AnonUser And what? What are you going to do with that? – Vlad from Moscow Apr 28 '14 at 10:44
  • @VladfromMoscow I mainly want to iterate through the middle row and check what the char above and below are (and perform more processing later but I've written the code for that). The main problem I was having was with when the upper and/or lower strings were shorter than the middle string and I still wanted to check the contents of the middle row without running into errors with rows that had finished. – Jade L Apr 28 '14 at 10:47

1 Answers1

1

Check if the list iterators for upper/lower have reached the end. Then change your logic accordingly such that it can work for the cases when lower or upper are not present.

list<char>::const_iterator i, u, l
u = upper_row.begin();
l = lower_row.begin();
for (i = middle_row.begin(); i != middle_row.end(); ++i) 
{
  bool has_upper = (u != upper_row.end());
  bool has_lower = (l != lower_row.end());
  // code to do something with upper, middle and lower lists
  // >> only do something with upper/lower if has_upper/has_lower is true
  if(has_upper) ++u;
  if(has_lower) ++l;
}

Additional note: always use ++i for iterators, not i++!

Danvil
  • 22,240
  • 19
  • 65
  • 88
  • Thanks for the response! It makes sense to me however could you please elaborate on using `++i` for iterators instead of `i++`? – Jade L Apr 28 '14 at 10:36
  • 1
    @AnonUser: The main reason is to avoid arguments with people who insist on always using `++i`. There's also a chance that `++i` might be more efficient in some circumstances, so it's probably a good idea anyway. – Mike Seymour Apr 28 '14 at 10:55