本页内容

QSet::const_iterator Class

class QSet::const_iterator

QSet::const_iterator 类为QSet 提供了STL风格的const迭代器。更多内容...

公共类型

公共函数

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 。 通常,即使针对非const的QSet ,也建议使用QSet::const_iterator ,除非您需要通过迭代器修改QSet 。const迭代器速度稍快,且能提高代码的可读性。

默认的QSet::const_iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QSet::begin()、QSet::end()或QSet::insert()等函数对其进行初始化。以下是一个典型的循环,用于打印集合中存储的所有项:

QSet<QString>set={"January", "February", /*...*/ "December"};

// i 是 QSet<QString>::const_iterator
for(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::iterator_category

std::bidirectional_iterator_tag的同义词,用于表示这些迭代器是双向迭代器。

成员函数文档

const_iterator::const_iterator()

构建一个未初始化的迭代器。

不应在未初始化的迭代器上调用 operator*() 和 operator++() 等函数。使用前请先通过 operator=() 向其赋值。

另请参阅 QSet::begin() 和QSet::end()。

const_iterator::const_iterator(const QSet<T>::iterator &other)

创建other 的副本。

这是一个重载函数。

const_iterator::const_iterator(const QSet<T>::const_iterator &other)

创建other 的副本。

bool const_iterator::operator!=(const QSet<T>::const_iterator &other) const

如果 `other ` 指向的项与该迭代器所指向的项不同,则返回 `true `;否则返回 `false`。

另请参阅 operator==()。

const T &const_iterator::operator*() const

返回对当前项的引用。

另请参阅 operator->()。

QSet<T>::const_iterator &const_iterator::operator++()

前缀 ++ 运算符(++it )将迭代器向前移动到集合中的下一个元素,并返回指向新当前元素的迭代器。

在QSet<T>::constEnd() 上调用此函数将导致未定义的结果。

QSet<T>::const_iterator const_iterator::operator++(int)

后缀 ++ 运算符(it++ )将迭代器向前移动到集合中的下一个元素,并返回指向先前当前元素的迭代器。

const T *const_iterator::operator->() const

返回指向当前项的指针。

另请参阅 ` operator*()`。

QSet<T>::const_iterator &const_iterator::operator=(const QSet<T>::const_iterator &other)

将other 分配给该迭代器。

bool const_iterator::operator==(const QSet<T>::const_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.