The beginning of today's section is about some of the general algorithms provided by QT. This section is from the C + + GUI programming with Qt 4, 2nd Edition. <QtAlgorithms> provides a series of generic template functions for implementing the basic algorithms above the container. This part of the algorithm relies heavily on STL-style ergodic (remember the Java-style walker and STL-style walker that was mentioned earlier)? )。 In fact, the C + + STL also provides a number of common algorithms that are included in<algorithm> in the header file. This part of the algorithm is also suitable for QT containers. Therefore, if the algorithm you want to use is not included in the QT <QtAlgorithms> header file, then you can use the STL algorithm instead, which does not create any conflicts. Here we have a few common algorithms in Qt. Although these algorithms are simple, library functions tend to be more efficient than they are written, so it is recommended to use system-provided functions. The first is the Qfind () function. The Qfind () function looks for a specific value in the container. It has a starting position and a terminating position in its parameters, and if the element being looked for exists, the function returns the position of the first occurrence, otherwise it returns the terminating position. Note that the "location" we're talking about here is actually a STL-style walker. We know that using an STL style walker can be a reflection of a location. For example, the value of I will be list.begin () + 1, and J will be 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");Qbinaryfind () behaves much like Qfind (), and the difference is thatQbinaryfind () is a binary lookup algorithm that only applies to finding the collection after sorting, while Qfind () is the standard linear lookup. In general, binary search methods are more demanding, but more efficient. Qfill () fills the container with the given value. For example:qlinkedlist<int> list (10);
Qfill (List.begin (), List.end (), 1009); As with other ergodic-based algorithms, Qfill () can also operate on a portion of a container, such as the following code will set the first 5 bits of the vector to 1009 and the last 5 bits to 2013:qvector<int> Vect (10);
Qfill (Vect.begin (), Vect.begin () + 5, 1009);
Qfill (Vect.end ()-5, Vect.end (), 2013);The qcopy () algorithm can be implemented to copy elements from one container to another, for example: qvector<int> Vect (List.count ());
Qcopy (List.begin (), List.end (), Vect.begin ()); qcopy () can also be used for replication of elements in the same container. The key to the success of the Qcopy () operation is that the scope of the source container and destination container does not overflow. For example, the following code, we will copy the last two elements of a list to the first two elements: qcopy (List.begin (), List.begin () + 2, List.end ()-2); QSort () implements the increment ordering of container elements and is simple to use: QSort (List.begin (), List.end ()); by default, QSort () uses the < operator to compare elements. This implies that you must define the < operator if necessary. If you need to sort by descending order, you need to pass qgreater<t> () as a third argument to the Qsort () function. For example: QSort (List.begin (), List.end (), qgreater<int> ()); Note that the t here is actually the generic type of the container. In fact, we can use the third parameter to define a sort. For example, we have a custom data type that has a less-case-insensitive qstring than the comparison function: bool Insensitivelessthan (const QString &str1, const QString &str2)
{
return str1.tolower () < Str2.tolower ();
} So, we can use Qsort () so that we can take advantage of this function: qstringlist list;
// ...
QSort (List.begin (), List.end (), Insensitivelessthan); the Qstablesort () function is similar to Qsort (), except that it is a stable sort. Stable sequencing is a noun in algorithmic design, meaning that, in the ordering process, if there are two elements equal, then the order of the two elements in the sorting result is the same as the original order before ordering. For example, for a sequence: A1, A5, A32, A31, A4, their order of magnitude is A1 < A31 = A32 < A4 < A5, then the result of a stable sort should be A1, A32, A31, A4, A5, that is, equal elements in the sort The order in which the results appear is consistent with the original order. Stable sequencing is useful in some situations, for example, there is now a student transcript that is sorted by number of studies. You want to re-order according to the grade, for the same students, or follow the original study number sequence. This is a stable sort. The Qdeleteall () function deletes all the pointers stored in the container. This function is only applicable if the container element is a pointer. After executing this function, the pointer in the container is executed by the delete operation, but these pointers are still stored in the container and become wild pointers, you need to call the container's clear () function to avoid the misuse of these pointers: Qdeleteall (list);
List.clear (); the Qswap () function can exchange the position of two elements. For example: int x1 = line.x1 ();
int x2 = line.x2 ();
if (X1 > x2)
Qswap (x1, x2); Finally, in the <QtGlobal> header file, several useful functions are also defined. This header file is include in all other header files, so you do not need to explicitly include this header file. there are several functions in this header file: Qabs () returns the absolute value of the parameter, Qmin () and Qmax () return the maximum and minimum values of two values.
This article is from the "Bean Space" blog, make sure to keep this source http://devbean.blog.51cto.com/448512/272421
QT Learning Pathway (49): a universal algorithm