2010-01-21

Foreach in Qt Container

Great! Qt container supports foreach iteration, which simplies largely the code. See the example for comparison:


========= java-like iterator===============
QLinkedList<> list;
...
QLinkedListIterator<> i(list);
while (i.hasNext())
qDebug()<< i.next()

========== STL-like iterator=============
QLinkedList<> list;
...
QListedList<>::const_iterator it = list.begin();
while( it!= list.end())
{
qDebug()<< (*it); ++it; }

=========foreach iteration===============

QLinkedList<>> list;
...
foreach (QString str, list)
qDebug() << str

===================================

Reference:

1 comment:

  1. The Foreach loop may be deprecated quite soon: https://www.kdab.com/goodbye-q_foreach/

    ReplyDelete