QSet::iterator Class
class QSet::iteratorQSet::iterator 类为QSet 提供了一个STL风格的非const迭代器。更多内容...
公共类型
公共函数
| iterator() | |
| iterator(const QSet<T>::iterator &other) | |
| bool | operator!=(const QSet<T>::iterator &other) const |
| bool | operator!=(const QSet<T>::const_iterator &other) const |
| const T & | operator*() const |
| QSet<T>::iterator & | operator++() |
| QSet<T>::iterator | operator++(int) |
| const T * | operator->() const |
| QSet<T>::iterator & | operator=(const QSet<T>::iterator &other) |
| bool | operator==(const QSet<T>::iterator &other) const |
| bool | operator==(const QSet<T>::const_iterator &other) const |
详细说明
QSet 同时支持STL 风格的迭代器和 Java 风格的迭代器。STL 风格的迭代器更接近底层,使用起来也更为繁琐;但另一方面,它们的速度稍快一些,对于已经熟悉 STL 的开发者来说,还具有使用起来更得心应手的优势。
QSet<T>::iterator 允许您遍历QSet ,并在遍历过程中移除元素(使用QSet::erase())。(QSet 不允许您通过迭代器修改值,因为这可能会导致需要移动QSet 所使用的内部哈希表中的值。) 若要遍历一个 constQSet ,应使用QSet::const_iterator 。通常,即使对非 constQSet ,也建议使用QSet::const_iterator ,除非你需要通过迭代器修改QSet 。const 迭代器速度稍快,且能提高代码可读性。
默认的QSet::iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用诸如QSet::begin()、QSet::end()或QSet::insert()之类的函数对其进行初始化。以下是一个典型的循环,用于打印集合中存储的所有项:
QSet<QString>set={"January", "February", /*...*/ "December"};
// i 是一个 QSet<QString>::iterator
for(autoi=set.begin(),end=set.end(); i!=end;++i)
qDebug() << *i;以下是一个在迭代过程中从集合中移除某些元素(所有以“J”开头的元素)的循环:
QSet<QString> set = {"January", "February", /*...*/ "December"};
auto i = set.begin();
while (i != set.end()) {
if ((*i).startsWith('J')) {
i = set.erase(i);
} else {
++i;
}
}STL 风格的迭代器可作为 `generic algorithms` 的参数。例如,以下是使用 `qFind()` 算法在集合中查找元素的方法:
QSet<QString> set;
//...
const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; };
QSet<QString>::iterator it = std::find_if(set.begin(), set.end(), predicate);
if (it != set.end())
cout << "Found Jeanette" << endl;同一个集合上可以使用多个迭代器。
警告: 在隐式共享容器上的迭代器其 行为与 STL 迭代器并不完全一致。当迭代器正在访问某个容器时,应避免复制该容器。更多信息请参阅《隐式共享迭代器问题》。
另请参阅 QSet::const_iterator 和QMutableSetIterator 。
成员函数文档
iterator::iterator()
构建一个未初始化的迭代器。
不应在未初始化的迭代器上调用 operator*() 和 operator++() 等函数。使用前请先通过 operator=() 向其赋值。
另请参阅 QSet::begin() 和QSet::end()。
iterator::iterator(const QSet<T>::iterator &other)
创建other 的副本。
bool iterator::operator!=(const QSet<T>::iterator &other) const
如果 `other ` 指向的项与该迭代器指向的项不同,则返回 `true `;否则返回 `false`。
另请参阅 operator==()。
bool iterator::operator!=(const QSet<T>::const_iterator &other) const
bool iterator::operator==(const QSet<T>::const_iterator &other) const
const T &iterator::operator*() const
返回对当前项的引用。
另请参阅 operator->()。
QSet<T>::iterator &iterator::operator++()
前缀 ++ 运算符(++it )将迭代器向前移动到集合中的下一个元素,并返回指向新当前元素的迭代器。
在QSet<T>::constEnd() 上调用此函数将导致未定义的结果。
QSet<T>::iterator iterator::operator++(int)
后缀 ++ 运算符(it++ )将迭代器向前移动到集合中的下一个元素,并返回指向先前当前元素的迭代器。
const T *iterator::operator->() const
返回指向当前项目的指针。
另请参阅 operator*()。
QSet<T>::iterator &iterator::operator=(const QSet<T>::iterator &other)
将other 分配给该迭代器。
bool iterator::operator==(const QSet<T>::iterator &other) const
如果 `other ` 指向与该迭代器相同的项,则返回 `true `;否则返回 `false`。
另请参阅 operator!=()。
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.