Qt Container Classes: Generic Algorithms

In the <QtAlgorithm> header file, Qt provides some global template functions, which are very commonly used algorithms that can be used on containers. We can use these algorithms on any container class that provides STL-style iterators, including QList, QLinkedList, QVector, QMap, and QHash. If STL can be used on the target platform, then the algorithms of STL can be used instead of these algorithms of Qt, because STL provides more algorithms, while Qt only provides some of the most important algorithms.

Historically, Qt has provided functions that are direct equivalents of many STL algorithmic functions. Starting with Qt 5.0, Qt encourages us to directly use the implementations available in the STL; most of them have been deprecated by Qt (although they can still be used to keep old code compiling, with warnings).

In most cases, applications that use deprecated Qt algorithm functions can be easily ported to use equivalent STL functions. You need to
add #include <algorithm> header file, and replace Qt functions with STL corresponding functions, as shown in the table below.

Qt function

STL function

qBinaryFind

std::binary_search | std::lower_bound

qCopy

std::copy

qCopyBackward

std::copy_backward

qEqual

std::equal

qFill

std::fill

qFind

std::find

qCount

std::count

qSort

std::sort

qStableSort

std::stable_sort

qLowerBound

std::lower_bound

qUpperBound

std::upper_bound

qLess

std::less

qGreater

std::greater

The following is a demonstration of several commonly used algorithms. You can check the Generic Algorithms keyword in the help index to learn about other algorithms.

(1) Sort

An example of a sorting algorithm is as follows:

//使用快速排序算法对list进行升序排序,排序后两个12的位置不确定
QList<int> list1;
list1 << 33 << 12 << 68 << 6 << 12;
std::sort(list1.begin(), list1.end());
qDebug() << list1; // 输出:(6, 12, 12, 33, 68)

//使用一种稳定排序算法对list2进行升序排序,排序前在前面的12,排序后依然在前面
QList<int> list2;
list2 << 33 << 12 << 68 << 6 << 12;
std::stable_sort(list2.begin(), list2.end());
qDebug() << list2; // 输出:(6, 12, 12, 33, 68)

//使用qSort()函数和std::greater算法中使list3反向排序
QList<int> list3;
list3 << 33 << 12 << 68 << 6 << 12;
qSort(list3.begin(), list3.end(), std::greater<int>()); //Qt5已弃用
qDebug() << list3; // 输出:(68, 33, 12, 12, 6)

By default, qSort() will compare elements using the < operator, i.e. in ascending order. If you need to change to descending order, you need to pass qGreater<T>() as the third parameter to the qSort() function.

(2) Find

An example of a lookup algorithm is as follows:

//使用std::find()从list中查找"two",返回第一个对应的值的迭代器,如果没有找到则返回end()
QStringList list1;
list1 << "one" << "two" << "three";
QList<QString>::iterator i = std::find(list1.begin(), list1.end(), "two");
qDebug() << *i; //输出:"two"

//使用qFind()从list中查找"two",返回第一个对应的值的迭代器,如果没有找到则返回end()
QStringList list2;
list2 << "one" << "two" << "three";
QStringList::iterator j = qFind(list2.begin(), list2.end(), "two");
qDebug() << *j; //输出:"two"

There is also a qBinaryFind() function, which behaves like qFind(). The difference is that qBinaryFind() is a binary search algorithm, which is only suitable for searching sorted collections, while qFind() is a standard linear search. In general, the binary search method is more demanding, but more efficient.

(3) Copy

An example of a replication algorithm is as follows:

//将list1中所有项目复制到vect1中
QStringList list1;
list1 << "one" << "two" << "three";
QVector<QString> vect1(list1.count());
std::copy(list1.begin(), list1.end(), vect1.begin());
qDebug() << vect1; //输出:QVector("one", "two", "three")

//将list2中所有项目复制到vect2中
QStringList list2;
list2 << "one" << "two" << "three";
QVector<QString> vect2(list2.count());
qCopy(list2.begin(), list2.end(), vect2.begin());
qDebug() << vect2; //输出:QVector("one", "two", "three")

The article is transferred from the blog garden (fengMisaka): Qt container class three: general algorithm - fengMisaka - blog garden

The benefits of this article, the fee to receive Qt development learning materials package, technical video, content includes (C++ language foundation, Qt programming introduction, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓ See below

Guess you like

Origin blog.csdn.net/QtCompany/article/details/131816614