QSet::const_iterator Class
class QSet::const_iteratorQSet::const_iterator 类为QSet 提供了 STL 风格的常量迭代器。更多
公共类型
公共函数
const_iterator() | |
const_iterator(const QSet<T>::iterator &other) | |
const_iterator(const QSet<T>::const_iterator &other) | |
bool | operator!=(const QSet<T>::const_iterator &other) const |
const T & | operator*() const |
QSet<T>::const_iterator & | operator++() |
QSet<T>::const_iterator | operator++(int) |
const T * | operator->() const |
QSet<T>::const_iterator & | operator=(const QSet<T>::const_iterator &other) |
bool | operator==(const QSet<T>::const_iterator &other) const |
详细说明
QSet 同时具有STL 风格的迭代器和Java 风格的迭代器。STL 风格的迭代器更低级,使用起来也更麻烦;但另一方面,它们的速度稍快,对于已经了解 STL 的开发人员来说,具有熟悉的优势。
QSet<Key,T>::const_iterator 允许你遍历QSet 。如果你想在遍历过程中修改QSet ,就必须使用QSet::iterator 。一般来说,除非需要通过迭代器修改QSet ,否则在非常数QSet 上使用QSet::const_iterator 也是不错的做法。常量迭代器的速度稍快,而且可以提高代码的可读性。
默认的QSet::const_iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QSet::begin(),QSet::end() 或QSet::insert() 等函数对其进行初始化。下面是一个典型的循环,用于打印存储在集合中的所有项目:
QSet<QString>set={"一月"、 "二月"、... "December"};// i is QSet<QString>::const_iteratorfor(autoi=set.cbegin(),end=set.cend(); i!=end;++i) qDebug() << *i;
STL 样式的迭代器可用作generic algorithms 的参数。例如,下面是如何使用 qFind() 算法在集合中查找一个项目:
QSet<QString> set; ... const auto predicate = [](const QString &s) { return s.compare("Jeanette", Qt::CaseInsensitive) == 0; }; QSet<QString>::const_iterator it = std::find_if(set.cbegin(), set.cend(), predicate); if (it != set.constEnd()) cout << "Found Jeanette" << endl;
警告: 隐式共享容器上的迭代器与 STL-迭代器的工作方式并不完全相同。当容器上的迭代器处于活动状态时,应避免复制该容器。更多信息,请阅读隐式共享迭代器问题。
另请参阅 QSet::iterator 和QSetIterator 。
成员函数文档
const_iterator::const_iterator()
构造一个未初始化的迭代器。
不应在未初始化的迭代器上调用 operator*() 和 operator++() 等函数。在使用迭代器之前,请使用 operator=() 为其赋值。
另请参阅 QSet::begin() 和QSet::end()。
const_iterator::const_iterator(const QSet<T>::const_iterator &other)
构造other 的副本。
QSet<T>::const_iterator &const_iterator::operator=(const QSet<T>::const_iterator &other)
为该迭代器指定other 。
const T &const_iterator::operator*() const
返回当前项目的引用。
另请参阅 operator->()。
const T *const_iterator::operator->() const
返回指向当前项目的指针。
另请参阅 operator*()。
bool const_iterator::operator==(const QSet<T>::const_iterator &other) const
如果other 指向与此迭代器相同的项目,则返回true
;否则返回false
。
另请参阅 operator!=() 。
bool const_iterator::operator!=(const QSet<T>::const_iterator &other) const
如果other 指向与此迭代器不同的项目,则返回true
;否则返回false
。
另请参阅 operator==() 。
QSet<T>::const_iterator &const_iterator::operator++()
前缀 ++ 运算符 (++it
) 将迭代器前进到集合中的下一项,并返回一个指向新的当前项的迭代器。
在QSet<T>::constEnd() 上调用此函数会导致未定义的结果。
QSet<T>::const_iterator const_iterator::operator++(int)
这是一个重载函数。
后缀 ++ 运算符 (it++
) 将迭代器前进到集合中的下一个项目,并返回一个指向先前当前项目的迭代器。
const_iterator::const_iterator(const QSet<T>::iterator &other)
这是一个重载函数。
构造other 的副本。
© 2025 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.