[Qgis-developer] STL vs. Qt containers

Mateusz Loskot mateusz at loskot.net
Sat Oct 28 21:25:16 EDT 2006


Martin Dobias wrote:
> Qt containers also have java-like iterators which are simpler to use.
> Besides this, they have generally the same functionality.

Martin,

Even if Trolls are warning you these iterators are slower than any other
way to access container elements, are you going to use them?
Just because of difference between (*it) and it.next() ?

Second, I've pointed it in my previous point, iterators are not a lonely
feature but they are part of whole system of types and algorithms
that *is supposed* to be used together to provide best performance
and simplified usage.

Honestly, Java-like iterators are not so easy.
What about the side effect in it.next(), is this still clear for you?
This operation results in 2:
- move iterator to next element
- return current element

Here are two examples:

QListIterator<QString> i(list);
while (i.hasNext())
    qDebug() << i.next();

std::list<std::string>::const_iterator it = mylist.begin();
while (it != mylist.end())
     qDebug() << *it++;
-----------------^^^^^^

here you can observe explicitly two these operations which
are hidden in i.next() above.
In C++, concept of iterators is strictly based on concept of pointers,
together with pointers arithmetic and dereferencing:
- increase or decrease pointer
- dereference pointer

Now, Java iterator dereferencing does 2 things together and I strongly
believe that C++ users may have problems with understanding it well.
For example C++ coders may try to do something like this:

int res = 0;
QListIterator<int> i(list);
while (i.hasNext())
{
     res = i.next() / (2 * i.next()) ...
}

where in C++ it's

res = (*i) / (2 * (*i));

without any temp values etc.

Next, construction of Java-like iterators is error prone.
Consider this:

QList<QString> list;

...

// 100 lines of code later
QListIterator<int> i(list);


Where with STL, you *can not* do this bug because well-designed
container API. Container iterator is closely tight with container type,
and this type is not std::list but std::list<T>.
This obvious rule in C++ is broken with QT Java-like iterators.

That's why I call Troll's talk as a FUD in this case.

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Qgis-developer mailing list