<QtAlgorithms> - Generic Algorithms
<QtAlgorithms> 标头包含基于模板的通用算法。更多
Header: | #include <QtAlgorithms> |
函数
uint | qCountLeadingZeroBits(quint16 v) |
uint | qCountLeadingZeroBits(quint32 v) |
uint | qCountLeadingZeroBits(quint64 v) |
uint | qCountLeadingZeroBits(quint8 v) |
uint | qCountTrailingZeroBits(quint8 v) |
uint | qCountTrailingZeroBits(quint16 v) |
uint | qCountTrailingZeroBits(quint32 v) |
uint | qCountTrailingZeroBits(quint64 v) |
void | qDeleteAll(ForwardIterator begin, ForwardIterator end) |
void | qDeleteAll(const Container &c) |
uint | qPopulationCount(quint8 v) |
uint | qPopulationCount(quint16 v) |
uint | qPopulationCount(quint32 v) |
uint | qPopulationCount(quint64 v) |
详细说明
Qt 在<QtAlgorithms>
中提供了许多全局模板函数,这些函数可用于容器并执行一些小任务,从而使生活更轻松,例如qDeleteAll() 可对给定容器中或给定范围内的所有项目调用operator delete
。您可以在任何提供 STL 风格迭代器的容器类中使用这些算法,包括 Qt XML 的QList 、QMap 和QHash 类。
大多数算法都将STL 样式的迭代器作为参数。这些算法是通用的,因为它们并不与特定的迭代器类绑定;你可以将它们与任何符合特定要求的迭代器一起使用。
不同的算法可能对其接受的迭代器有不同的要求。每种算法都指定了所需的迭代器类型。如果传递了错误类型的迭代器(例如,将QList::ConstIterator 作为output iterator 传递),编译器总是会出错,但不一定是信息量很大的错误。
有些算法对存储在容器中的值类型有特殊要求。例如,qDeleteAll() 要求值类型是非const 指针类型(例如,QWidget *)。每种算法都指定了值类型要求,如果不符合要求,编译器会产生错误。
除 Qt 和 STL 提供的容器类外,通用算法还可用于其他容器类。STL 风格迭代器的语法仿照 C++ 指针,因此可以使用普通数组作为容器,使用普通指针作为迭代器。
迭代器类型
算法对其接受的迭代器类型有一定的要求,这些要求是为每个函数单独指定的。如果不符合要求,编译器会产生错误。
输入迭代器
输入迭代器是一种可用于从容器中按顺序读取数据的迭代器。它必须提供以下操作符:用于比较两个迭代器的==
和!=
,用于检索存储在项中的值的单引号*
,以及用于前进到下一个项的前缀++
。
Qt 容器的迭代器类型(常量和非常量)都是输入迭代器。
输出迭代器
输出迭代器是一种迭代器,可用于将数据按顺序写入容器或某些输出流。它必须提供以下操作符:用于写入值的一元*
(即*it = val
)和用于前进到下一项的前缀++
。
Qt 容器的非常数迭代器类型都是输出迭代器。
前向迭代器
前向迭代器是一种同时满足输入迭代器和输出迭代器要求的迭代器。
Qt 容器的非const 迭代器类型都是前向迭代器。
双向迭代器
双向迭代器是一种满足正向迭代器要求的迭代器,但它还支持用于反向迭代的前缀--
。
Qt 容器的非const迭代器类型都是双向迭代器。
随机访问迭代器
最后一类随机访问迭代器是功能最强大的迭代器类型。它支持双向迭代器的所有要求,并支持以下操作:
i += n | 将迭代器i 提前n 个位置 |
i -= n | 将迭代器i 向后移动n 个位置 |
i + n 或n + i | 返回迭代器n 位置前的项目的迭代器i |
i - n | 返回位于迭代器后面n 位置的项目的迭代器i |
i - j | 返回迭代器i 与j |
i[n] | 相同*(i + n) |
i < j | 如果迭代器j 在迭代器之后,则返回true i |
QList的非const迭代器类型是随机访问迭代器。
另请参阅 容器类和<QtGlobal> 。
函数文档
[constexpr noexcept]
uint qCountLeadingZeroBits(quint16 v)
从 MSB 开始搜索时,返回v 中连续零位的个数。例如,qCountLeadingZeroBits(quint16(1)) 返回 15,qCountLeadingZeroBits(quint16(8)) 返回 12。
[constexpr noexcept]
uint qCountLeadingZeroBits(quint32 v)
从 MSB 开始搜索时,返回v 中连续零位的个数。例如,qCountLeadingZeroBits(quint32(1)) 返回 31,qCountLeadingZeroBits(quint32(8)) 返回 28。
[constexpr noexcept]
uint qCountLeadingZeroBits(quint64 v)
从 MSB 开始搜索时,返回v 中连续零位的个数。例如,qCountLeadingZeroBits(quint64(1)) 返回 63,qCountLeadingZeroBits(quint64(8)) 返回 60。
[constexpr noexcept]
uint qCountLeadingZeroBits(quint8 v)
从 MSB 开始搜索时,返回v 中连续零位的个数。例如,qCountLeadingZeroBits(quint8(1)) 返回 7,qCountLeadingZeroBits(quint8(8)) 返回 4。
[constexpr noexcept]
uint qCountTrailingZeroBits(quint8 v)
从 LSB 开始搜索时,返回v 中连续零位的个数。例如,qCountTrailingZeroBits(1) 返回 0,qCountTrailingZeroBits(8) 返回 3。
[constexpr noexcept]
uint qCountTrailingZeroBits(quint16 v)
这是一个重载函数。
[constexpr noexcept]
uint qCountTrailingZeroBits(quint32 v)
这是一个重载函数。
[constexpr noexcept]
uint qCountTrailingZeroBits(quint64 v)
这是一个重载函数。
template <typename ForwardIterator> void qDeleteAll(ForwardIterator begin, ForwardIterator end)
使用 C++delete
操作符删除范围 [begin,end) 中的所有项目。项目类型必须是指针类型(例如QWidget *
)。
示例
QList<Employee *> list; list.append(new Employee("Blackpool", "Stephen")); list.append(new Employee("Twist", "Oliver")); qDeleteAll(list.begin(), list.end()); list.clear();
请注意,qDeleteAll() 并没有从容器中删除项目,而只是调用了delete
。在上面的示例中,我们在容器上调用 clear() 来删除项目。
此函数也可用于删除存储在关联容器中的项目,如QMap 和QHash 。此函数只删除存储在每个容器中的对象,而不会删除用作键的对象。
另请参见 forward iterators 。
template <typename Container> void qDeleteAll(const Container &c)
这是一个重载函数。
它与 qDeleteAll(c.begin(),c.end()) 相同。
[constexpr noexcept]
uint qPopulationCount(quint8 v)
返回v 中设置的比特数。这个数字也称为v 的汉明权重。
[constexpr noexcept]
uint qPopulationCount(quint16 v)
这是一个重载函数。
[constexpr noexcept]
uint qPopulationCount(quint32 v)
这是一个重载函数。
[constexpr noexcept]
uint qPopulationCount(quint64 v)
这是一个重载函数。
© 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.