Qt of three containers: Universal Algorithm

In <QtAlgorithm> header file, Qt provides a number of global template functions that are very commonly used algorithms can be used on the container. We can use these algorithms on any offers STL style iterators container classes, including QList, QLinkedList, QVector, QMap and QHash. If you can use STL on the target platform, you can use STL algorithms instead of these algorithms Qt because STL provides more algorithms, and algorithms Qt only provides some of the most important of them. Here are several commonly used algorithms for which the presentation, you can view Generic Algorithms keyword index to help understand other algorithms.


(1) Sort

Example 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 () using the <operator comparing element, i.e., in ascending order. If necessary to descending, need qGreater <T> () passed as the third parameter, qSort () function.


(2) Find

Examples of search 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"

Further there is a qBinaryFind () function, which behaves like qFind (), except that, qBinaryFind () is a binary search algorithm, it applies only to look after the set of ordered, and qFind () is the standard linear search . Typically, conditions of use binary search method is more demanding, but the efficiency will be higher.


(3) Copy

Example copy 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")


(4) exchange

Example exchange algorithm is as follows:

//交换pi和e的值
double p1 = 3.14;
double e1 = 2.71;
std::swap(p1, e1);
qDebug() << "p1:" << p1 << "e1:" << e1; // 输出:p1=2.71,e1=3.14

//交换pi和e的值
double p2 = 3.14;
double e2 = 2.71;
std::swap(p2, e2);
qDebug() << "p2:" << p2 << "e2:" << e2; // 输出:p2=2.71,e2=3.14


(5) filled

Example filling algorithm is as follows:

//将vector的前5位设置成3,而最后5位设置为7
QVector<int> vect(10);
qFill(vect.begin(), vect.begin() + 5, 3);
qFill(vect.end() - 5, vect.end(), 7);
qDebug() << vect; //输出:QVector(3, 3, 3, 3, 3, 7, 7, 7, 7, 7)

//将vector的前5位设置成3,而最后5位设置为7
QVector<int> vect2(10);
std::fill(vect2.begin(), vect2.begin() + 5, 3);
std::fill(vect2.end() - 5, vect2.end(), 7);
qDebug() << vect2; //输出:QVector(3, 3, 3, 3, 3, 7, 7, 7, 7, 7)


(6) Other

Other common arithmetic functions are as follows (some not listed):

  • qCount () calculates the number of times a value occurs in the container.
  • qDeleteAll () calls the C ++ operator delete, the destructor of the container element.
  • qEqual () compares two sequences are equal elements.



reference:

General Algorithm QT container


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11027942.html