- Sequential Containers
- Associative Containers
- Generic Algorithms
- Strings, Byte Arrays, and Variants
Generic Algorithms
The <QtAlgorithms> header declares a set of global template functions that implement basic algorithms on containers. Most of these functions operate on STL-style iterators.
The STL <algorithm> header provides a more complete set of generic algorithms. These algorithms can be used on Qt containers as well as STL containers. If STL implementations are available on all your platforms, there is probably no reason to avoid using the STL algorithms when Qt lacks an equivalent algorithm. Here, we will introduce the most important Qt algorithms.
The qFind() algorithm searches for a particular value in a container. It takes a "begin" and an "end" iterator and returns an iterator pointing to the first item that matches, or "end" if there is no match. In the following example, i is set to list.begin() + 1, whereas j is set to list.end():
QStringList list; list << "Emma" << "Karl" << "James" << "Mariette"; QStringList::iterator i = qFind(list.begin(), list.end(), "Karl"); QStringList::iterator j = qFind(list.begin(), list.end(), "Petra");
The qBinaryFind() algorithm performs a search just like qFind(), except that it assumes that the items are sorted in ascending order and uses fast binary searching rather than qFind()'s linear searching.
The qFill() algorithm populates a container with a particular value:
QLinkedList<int> list(10); qFill(list.begin(), list.end(), 1009);
Like the other iterator-based algorithms, we can also use qFill() on a portion of the container by varying the arguments. The following code snippet initializes the first five items of a vector to 1009 and the last five items to 2013:
QVector<int> vect(10); qFill(vect.begin(), vect.begin() + 5, 1009); qFill(vect.end() - 5, vect.end(), 2013);
The qCopy() algorithm copies values from one container to another:
QVector<int> vect(list.count()); qCopy(list.begin(), list.end(), vect.begin());
qCopy() can also be used to copy values within the same container, as long as the source range and the target range don't overlap. In the next code snippet, we use it to overwrite the last two items of a list with the first two items:
qCopy(list.begin(), list.begin() + 2, list.end() - 2);
The qSort() algorithm sorts the container's items into ascending order:
qSort(list.begin(), list.end());
By default, qSort() uses the < operator to compare the items. To sort items in descending order, pass qGreater<T>() as the third argument (where T is the container's value type), as follows:
qSort(list.begin(), list.end(), qGreater<int>());
We can use the third parameter to define custom sort criteria. For example, here's a "less than" comparison function that compares QStrings in a case-insensitive way:
bool insensitiveLessThan(const QString &str1, const QString &str2) { return str1.toLower() < str2.toLower(); }
The call to qSort() then becomes
QStringList list; ... qSort(list.begin(), list.end(), insensitiveLessThan);
The qStableSort() algorithm is similar to qSort(), except it guarantees that items that compare equal appear in the same order after the sort as before. This is useful if the sort criterion takes into account only parts of the value and the results are visible to the user. We used qStableSort() in Chapter 4 to implement sorting in the Spreadsheet application (p. 92).
The qDeleteAll() algorithm calls delete on every pointer stored in a container. It makes sense only on containers whose value type is a pointer type. After the call, the items are still present in the container as dangling pointers, so normally we should also call clear() on the container. For example:
qDeleteAll(list); list.clear();
The qSwap() algorithm exchanges the value of two variables. For example:
int x1 = line.x1(); int x2 = line.x2(); if (x1 > x2) qSwap(x1, x2);
Finally, the <QtGlobal> header, which is included by every other Qt header, provides several useful definitions, including the qAbs() function, that returns the absolute value of its argument, and the qMin() and qMax() functions, that return the minimum or maximum of two values.