QJsonObject::iterator Class

class QJsonObject::iterator

QJsonObject::iterator 类为QJsonObject 提供了 STL 风格的非常数迭代器。更多

该类具有很强的可比性

该类与QJsonObject::const_iterator 具有很强的可比性

公共类型

公共函数

iterator()
QString key() const
QJsonValueRef value() const
QJsonValueRef operator*() const
QJsonObject::iterator operator+(qsizetype j) const
QJsonObject::iterator &operator++()
QJsonObject::iterator operator++(int)
QJsonObject::iterator &operator+=(qsizetype j)
qsizetype operator-(QJsonObject::iterator other) const
QJsonObject::iterator operator-(qsizetype j) const
QJsonObject::iterator &operator--()
QJsonObject::iterator operator--(int)
QJsonObject::iterator &operator-=(qsizetype j)
QJsonValueRef *operator->()
const QJsonValueConstRef *operator->() const
QJsonValueRef operator[](qsizetype j) const
bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)
bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)
bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

详细描述

QJsonObject::iterator 允许您遍历 ,并修改特定键下存储的值(而不是键)。如果要遍历常量 ,则应使用 。一般来说,除非需要通过迭代器修改 ,否则在非常量 上使用 也是不错的做法。常量迭代器速度稍快,代码可读性也更好。QJsonObject QJsonObject QJsonObject::const_iterator QJsonObject QJsonObject QJsonObject::const_iterator

默认的QJsonObject::iterator 构造函数会创建一个未初始化的迭代器。在开始迭代之前,必须使用QJsonObject 函数(如QJsonObject::begin()、QJsonObject::end() 或QJsonObject::find() )对其进行初始化。

可以在同一对象上使用多个迭代器。不过,一旦对象被修改,现有的迭代器就会变成悬空的。

另请参阅 QJsonObject::const_iteratorQt 中的 JSON 支持以及保存和加载游戏

成员类型文档

iterator::iterator_category

std::random_access_iterator_tag的同义词,表示该迭代器是随机访问迭代器。

注意: 在 5.6 之前的 Qt 版本中,该标记被错误地设置为std::bidirectional_iterator_tag

成员函数文档

iterator::iterator()

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

不得在未初始化的迭代器上调用key(),value() 和 operator++() 等函数。在使用迭代器之前,请使用 operator=() 为其赋值。

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

QString iterator::key() const

返回当前项目的键值。

虽然可以通过调用QJsonObject::erase() 后再调用QJsonObject::insert() 来改变项的键值,但没有通过迭代器改变项键值的直接方法。

另请参阅 value()。

QJsonValueRef iterator::value() const

返回当前项目值的可修改引用。

您可以在赋值的左侧使用 value() 来更改项的值。

返回值的类型是QJsonValueRef ,它是QJsonArrayQJsonObject 的辅助类。当您获得QJsonValueRef 类型的对象时,可以将其作为QJsonValue 的引用来使用。如果对其赋值,赋值将应用于您从中获得引用的QJsonArrayQJsonObject 中的元素。

另请参阅 key() 和operator*()。

QJsonValueRef iterator::operator*() const

返回当前项目值的可修改引用。

value() 相同。

返回值的类型是QJsonValueRef ,它是QJsonArrayQJsonObject 的辅助类。当您获得QJsonValueRef 类型的对象时,可以像使用QJsonValue 的引用一样使用它。如果对它进行赋值,赋值将应用于您从中获得引用的QJsonArrayQJsonObject 中的元素。

另请参阅 key() 。

QJsonObject::iterator iterator::operator+(qsizetype j) const

返回一个迭代器,指向从该迭代器向前j 位置的项目。如果j 为负数,则迭代器会向后退。

另请参见 operator-()。

QJsonObject::iterator &iterator::operator++()

++ 前缀操作符++i 将迭代器前进到对象中的下一个项,并返回一个指向新的当前项的迭代器。

QJsonObject::end() 上调用此函数会导致未定义的结果。

另请参见 operator--()。

QJsonObject::iterator iterator::operator++(int)

这是一个重载函数。

后缀++ 运算符i++ 将迭代器前进到对象中的下一个项目,并返回一个指向先前当前项目的迭代器。

QJsonObject::iterator &iterator::operator+=(qsizetype j)

通过j 将迭代器向前推进。如果j 为负数,迭代器会向后退。

另请参阅 operator-=() 和operator+()。

qsizetype iterator::operator-(QJsonObject::iterator other) const

返回other 指向的项目与此迭代器指向的项目之间的项目个数。

QJsonObject::iterator iterator::operator-(qsizetype j) const

返回一个迭代器,指向从该迭代器向后位于j 位置的项目。如果j 为负数,则迭代器向前移动。

另请参见 operator+()。

QJsonObject::iterator &iterator::operator--()

-- 前缀运算符--i 使前一个项目成为当前项目,并返回一个指向新的当前项目的迭代器。

QJsonObject::begin() 上调用此函数会导致未定义的结果。

另请参见 operator++()。

QJsonObject::iterator iterator::operator--(int)

这是一个重载函数。

后缀-- 运算符i-- 使前一个项目成为当前项目,并返回一个指向前一个当前项目的迭代器。

QJsonObject::iterator &iterator::operator-=(qsizetype j)

使迭代器返回j 项。如果j 为负数,则迭代器向前移动。

另请参阅 operator+=() 和operator-()。

QJsonValueRef *iterator::operator->()

返回指向当前项目可修改引用的指针。

const QJsonValueConstRef *iterator::operator->() const

返回指向当前项目常量引用的指针。

QJsonValueRef iterator::operator[](qsizetype j) const

返回该迭代器指向的项目(位置*this + j 上的项目)与j 偏移量处的项目的可修改引用。

提供该函数是为了使QJsonObject 迭代器的行为类似于 C++ 指针。

返回值的类型是QJsonValueRef ,它是QJsonArrayQJsonObject 的辅助类。当您获得QJsonValueRef 类型的对象时,可以将其作为QJsonValue 的引用来使用。如果对其赋值,赋值将应用于您获得引用的QJsonArrayQJsonObject 中的元素。

另请参阅 operator+()。

相关非成员

[noexcept] bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator==(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果lhs 指向与rhs 迭代器相同的项目,则返回true ;否则返回false

另请参阅 operator!=() 。

[noexcept] bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator!=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果lhs 指向与rhs 迭代器不同的项目,则返回true ;否则返回false

另请参阅 operator==() 。

[noexcept] bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator<(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果lhs iterator 指向的项目小于rhs iterator 指向的项目,则返回true

[noexcept] bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator<=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果lhs iterator 指向的项目小于或等于rhs iterator 指向的项目,则返回true

[noexcept] bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator>(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果lhs 迭代器指向的项目大于rhs 迭代器指向的项目,则返回true

[noexcept] bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::const_iterator &rhs)

[noexcept] bool operator>=(const QJsonObject::iterator &lhs, const QJsonObject::iterator &rhs)

如果lhs iterator 指向的项目大于或等于rhs iterator 指向的项目,则返回true

© 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.