Qt: is removing QList elements while iterating using foreach macro possible?

前端 未结 3 396
有刺的猬
有刺的猬 2020-12-30 20:26

I\'m new to Qt and trying to learn the idioms.

The foreach documentation says:

Qt automatically takes a copy of the container when it enters a

3条回答
  •  囚心锁ツ
    2020-12-30 21:14

    You should better use iterators for that:

    // Remove all odd numbers from a QList 
    QMutableListIterator i(list);
    while (i.hasNext()) {
        if (i.next() % 2 != 0)
            i.remove();
    }
    

提交回复
热议问题