Mrchacha Mrchacha - 3 months ago 17
C++ Question

How to make a QStringList iterator start from a specific index

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.

Answer

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