PDA

View Full Version : How to iterate a QStringList from a specific position its end()



mireiner
24th April 2016, 16:01
Hi there,

how to iterate QStringList from a specific posistion to its end. Both examples don't work:


QStringList stringlist;

for (int i = 4; !stringlist[i].isNull(); i++){}

for (QStringList::iterator it = stringlist.at(4); it != stringlist.end(); ++it) {}
How its down right?

Hi there,

how to iterate QStringList from a specific posistion to its end. Both examples don't work:


QStringList stringlist;

for (int i = 4; !stringlist[i].isNull(); i++){}

for (QStringList::iterator it = stringlist.at(4); it != stringlist.end(); ++it) {}
How its down right?

Hi there,

how to iterate QStringList from a specific posistion to its end. Both examples don't work:


QStringList stringlist;

for (int i = 4; !stringlist[i].isNull(); i++){}

for (QStringList::iterator it = stringlist.at(4); it != stringlist.end(); ++it) {}
How its down right?

Added after 25 minutes:

Just found a solution for myself:


QStringList stringlist;

for (QStringList::iterator it = stringlist.begin()+4; it != stringlist.end(); ++it)
{
do something;
}
Is this the easiest way to do this?

d_stranz
24th April 2016, 17:58
Is this the easiest way to do this?

It is one way to do it. In your examples, "stringlist.at()" does not return an iterator, it returns the QString at the location you give as an argument and won't compile. "stringlist[i]" also returns the QString at "i", but in your code there is nothing to prevent the index from going off the end of the list. It will compile but will probably blow up at runtime.

anda_skoa
24th April 2016, 19:09
It will compile but will probably blow up at runtime.
Exactly, unless come other code guarantees that the last element in the list is a null string.

Why not check if the current value of "i" is still valid?

Cheers,
_

Lesiok
25th April 2016, 07:15
Something like this :
QStringList stringlist;

for (int i = 4; i < stringlist.size(); i++){}