<QtAlgorithms> - Generic Algorithms | Qt Core | Qt 6.11.1
Description: The <QtAlgorithms> header includes the generic, template-based algorithms.

- Qt 6.11
- Qt Core
- <QtAlgorithms> - Generic Algorithms
- Qt 6.11.1 Reference Documentation

# <QtAlgorithms> - Generic Algorithms

The <QtAlgorithms> header includes the generic, template-based algorithms. More...

Header: | `#include <QtAlgorithms>`

## Functions

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)
`(since 6.10)` Result | qJoin(InputIterator first, InputIterator last, Result init, const Separator &separator = {}, Projection p = {})
uint | qPopulationCount(quint8 v)
uint | qPopulationCount(quint16 v)
uint | qPopulationCount(quint32 v)
uint | qPopulationCount(quint64 v)

## Detailed Description

Qt provides a number of global template functions in `<QtAlgorithms>` that work on containers and perform small tasks to make life easier, such as qDeleteAll(), which invokes `operator delete` on all items in a given container or in a given range. You can use these algorithms with any container class that provides STL-style iterators, including Qt's QList, QMap, and QHash classes.

Most algorithms take STL-style iterators as parameters. The algorithms are generic in the sense that they aren't bound to a specific iterator class; you can use them with any iterators that meet a certain set of requirements.

Different algorithms can have different requirements for the iterators they accept. The iterator types required are specified for each algorithm. If an iterator of the wrong type is passed (for example, if QList::ConstIterator is passed as an output iterator), you will always get a compiler error, although not necessarily a very informative one.

Some algorithms have special requirements on the value type stored in the containers. For example, qDeleteAll() requires that the value type is a non-const pointer type (for example, QWidget *). The value type requirements are specified for each algorithm, and the compiler will produce an error if a requirement isn't met.

The generic algorithms can be used on other container classes than those provided by Qt and STL. The syntax of STL-style iterators is modeled after C++ pointers, so it's possible to use plain arrays as containers and plain pointers as iterators.

#### Types of Iterators

The algorithms have certain requirements on the iterator types they accept, and these are specified individually for each function. The compiler will produce an error if a requirement isn't met.

##### Input Iterators

An input iterator is an iterator that can be used for reading data sequentially from a container. It must provide the following operators: `==` and `!=` for comparing two iterators, unary `*` for retrieving the value stored in the item, and prefix `++` for advancing to the next item.

The Qt containers' iterator types (const and non-const) are all input iterators.

##### Output Iterators

An output iterator is an iterator that can be used for writing data sequentially to a container or to some output stream. It must provide the following operators: unary `*` for writing a value (i.e., `*it = val`) and prefix `++` for advancing to the next item.

The Qt containers' non-const iterator types are all output iterators.

##### Forward Iterators

A forward iterator is an iterator that meets the requirements of both input iterators and output iterators.

The Qt containers' non-const iterator types are all forward iterators.

##### Bidirectional Iterators

A bidirectional iterator is an iterator that meets the requirements of forward iterators but that in addition supports prefix `--` for iterating backward.

The Qt containers' non-const iterator types are all bidirectional iterators.

##### Random Access Iterators

The last category, random access iterators, is the most powerful type of iterator. It supports all the requirements of a bidirectional iterator, and supports the following operations:

`i += n` | advances iterator `i` by `n` positions
`i -= n` | moves iterator `i` back by `n` positions
`i + n` or `n + i` | returns the iterator for the item `n` positions ahead of iterator `i`
`i - n` | returns the iterator for the item `n` positions behind of iterator `i`
`i - j` | returns the number of items between iterators `i` and `j`
`i[n]` | same as `*(i + n)`
`i < j` | returns `true` if iterator `j` comes after iterator `i`

QList's non-const iterator type is random access iterator.

See also container classes and <QtGlobal>.

## Function Documentation

### `[constexpr noexcept]` uint qCountLeadingZeroBits(quint16 v)

Returns the number of consecutive zero bits in v, when searching from the MSB. For example, qCountLeadingZeroBits(quint16(1)) returns 15 and qCountLeadingZeroBits(quint16(8)) returns 12.

### `[constexpr noexcept]` uint qCountLeadingZeroBits(quint32 v)

Returns the number of consecutive zero bits in v, when searching from the MSB. For example, qCountLeadingZeroBits(quint32(1)) returns 31 and qCountLeadingZeroBits(quint32(8)) returns 28.

### `[constexpr noexcept]` uint qCountLeadingZeroBits(quint64 v)

Returns the number of consecutive zero bits in v, when searching from the MSB. For example, qCountLeadingZeroBits(quint64(1)) returns 63 and qCountLeadingZeroBits(quint64(8)) returns 60.

### `[constexpr noexcept]` uint qCountLeadingZeroBits(quint8 v)

Returns the number of consecutive zero bits in v, when searching from the MSB. For example, qCountLeadingZeroBits(quint8(1)) returns 7 and qCountLeadingZeroBits(quint8(8)) returns 4.

### `[constexpr noexcept]` uint qCountTrailingZeroBits(quint8 v)

Returns the number of consecutive zero bits in v, when searching from the LSB. For example, qCountTrailingZeroBits(1) returns 0 and qCountTrailingZeroBits(8) returns 3.

### `[constexpr noexcept]` uint qCountTrailingZeroBits(quint16 v)

This is an overloaded function.

### `[constexpr noexcept]` uint qCountTrailingZeroBits(quint32 v)

This is an overloaded function.

### `[constexpr noexcept]` uint qCountTrailingZeroBits(quint64 v)

This is an overloaded function.

### template <typename ForwardIterator> void qDeleteAll(ForwardIterator begin, ForwardIterator end)

Deletes all the items in the range [begin, end) using the C++ `delete` operator. The item type must be a pointer type (for example, `QWidget *`).

Example:

     QList<QColor *> list;
     list.append(new QColor(Qt::blue));
     list.append(new QColor(Qt::yellow));

     qDeleteAll(list.begin(), list.end());
     list.clear();

Notice that qDeleteAll() doesn't remove the items from the container; it merely calls `delete` on them. In the example above, we call clear() on the container to remove the items.

This function can also be used to delete items stored in associative containers, such as QMap and QHash. Only the objects stored in each container will be deleted by this function; objects used as keys will not be deleted.

See also forward iterators.

### template <typename Container> void qDeleteAll(const Container &c)

This is the same as qDeleteAll(c.begin(), c.end()).

This is an overloaded function.

### `[since 6.10]` template < typename InputIterator, typename Result, typename Separator = Result, typename Projection = std::identity > Result qJoin(InputIterator first, InputIterator last, Result init, const Separator &separator = {}, Projection p = {})

Starts with initial value init and cumulatively adds to it each entry in the range from first to last. Each entry is, before adding, mapped through the projection p (which defaults to the identity). Between each entry and the next, an optional separator is added. For example:

     QList<int> l = {1, 2, 3};
     QString res = qJoin(l.cbegin(), l.cend(), QString(), u" / ",
                         [](int n) { return QString::number(2 * n);});
     // res == "2 / 4 / 6"

Note: `q20::identity` is a C++17 backport of C++20's `std::identity`.

This function was introduced in Qt 6.10.

### `[constexpr noexcept]` uint qPopulationCount(quint8 v)

Returns the number of bits set in v. This number is also called the Hamming Weight of v.

### `[constexpr noexcept]` uint qPopulationCount(quint16 v)

This is an overloaded function.

### `[constexpr noexcept]` uint qPopulationCount(quint32 v)

This is an overloaded function.

### `[constexpr noexcept]` uint qPopulationCount(quint64 v)

This is an overloaded function.