2

I'm working on a QStringList where the program has a main iterator to go through each word. Now I want to implement a sub-iterator where I want the sub-iterator to start at a certain position.

Here is a simple visualization of my code:

for(QStringList::iterator pkg_header(inputline.begin()); pkg_header != inputline.end(); ++pkg_header){ 
   ...
   if(!QString::compare(*pkg_header,Computing)){
      for(QStringList::iterator pkg_section(pkg_header+1; pkg_section != pkg_header.end(); pkg_section++){
         ...
   }
}

In other words, I need help to make the sub-iterator start from pkg_header+1 position instead of doing pkg_header.begin().

Thanks.

Mrchacha
  • 197
  • 1
  • 17
  • 1
    Can you use [`std::next`](http://en.cppreference.com/w/cpp/iterator/next)? – Tas Aug 28 '16 at 22:57
  • 1
    This is simpler than you think. See here: http://stackoverflow.com/questions/1033089/can-i-increment-an-iterator-by-just-adding-a-number – Chris Aug 28 '16 at 23:03

1 Answers1

1

I think you can just use operator + (int) with QStringList iterators (somebody correct me if I'm wrong):

for (QStringList::iterator pkg_section = pkg_header + 1;
     pkg_section != inputline.end(); 
     pkg_section ++) 
    ...

But even if not, you can always do:

QStringList::iterator pkg_section = pkg_header;
for (pkg_section ++; pkg_section != inputline.end(); pkg_section ++) 
    ...

Apologies if you can't do + 1, I'm just not in a position to conveniently check for QStringList right now.

Also you seem to have some confusion about the termination condition for your loops. I am assuming you meant inputline.end(), as pkg_header.end() isn't valid... pkg_header is an iterator, it doesn't have an end().

Jason C
  • 38,729
  • 14
  • 126
  • 182