Iterator for second to last element in a list

Posted by BSchlinker on Stack Overflow See other posts from Stack Overflow or by BSchlinker
Published on 2011-06-21T19:34:05Z Indexed on 2011/06/22 0:23 UTC
Read the original article Hit count: 267

Filed under:
|
|

I currently have the following for loop:

for(list<string>::iterator jt=it->begin(); jt!=it->end()-1; jt++)

I have a list of strings which is in a larger list (list<list<string> >). I want to loop through the contents of the innerlist until I get to the 2nd to last element. This is because I have already processed the contents of the final element, and have no reason to process them again.

However, using it->end()-1 is invalid -- I cannot use the - operator here. While I could use the -- operator, this would decrement this final iterator on each cycle.

I believe a STL list is a doubly linked list, so from my perspective, it should be possible to do this.

Advice? Thanks in advance

© Stack Overflow or respective owner

Related posts about c++

Related posts about stl