本文介绍了Qt的foreach的第二个术语只评估一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这个代码:

foreach (QListWidgetItem *ii, selectedItems()) {
    urls.push_back(ii->data(Qt::ToolTip).toString());
}

selectedItems()只能调用一次?

推荐答案

Yup。它将创建一个返回的容器的副本,并使用它。 (请参阅文档)

Yup. It will create a copy of the returned container, and use that. (See Qt foreach keyword documentation)

相关:


  • 由于Qt foreach总是复制容器,它最适用于QT容器(即写时复制)或小型STL容器。

  • 更好地处理这个问题,避免复制,除非必要。

  • Since Qt foreach always copies the container, it's best used with either QT-containers (which are copy-on-write) or small STL containers.
  • Boost's foreach handles this better, and avoids copies unless necessary.

这篇关于Qt的foreach的第二个术语只评估一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:55