Some general algorithms provided by QT started today. This part is from C ++ GUI programming with QT 4, 2nd edition. <Qtalgorithms> provides a series of common template functions to implement the basic algorithms on the container. Many of these algorithms depend on the STL-style traversal tool (remember the Java-style traversal tool and STL-style traversal tool mentioned earlier ?). In fact, C ++ STL also provides many common algorithms, including in the header file <algorithm>. This algorithm is also applicable to QT containers. Therefore, if the algorithm you want to use is not included in the <qtalgorithms> header file of QT, you can use the STL algorithm instead, which will not cause any conflict. Here are some general algorithms in QT. Although these algorithms are very simple, library functions are often more efficient than self-compiled ones. Therefore, we recommend using the functions provided by the system. First
Qfind ()Function. The qfind () function searches for a specific value in the container. Its parameters have a start position and end position. If the queried element exists, the function returns the position of the first matching item. Otherwise, the function returns the end position. Note that the "location" we refer to here is actually an STL-style traversal tool. We know that using the STL-style traversal tool can reflect a location. For example, in the following example, the value of I will be list. Begin () + 1, and J will be list. End (): qstringlist;
List <"Emma" <"Karl" <"James" <"Mariette ";
Qstringlist: iterator I = qfind (list. Begin (), list. End (), "Karl ");
Qstringlist: iterator J = qfind (list. Begin (), list. End (), "Petra ");
Qbinaryfind ()Qbinaryfind () is a binary search algorithm. qbinaryfind () is applicable only to the sorted sets, while qfind () is a standard linear query. Generally, binary lookup is more demanding, but more efficient.
Qfill ()The specified value is used to fill the container. Example: qinilist <int> List (10 );
Qfill (list. begin (), list. end (), 1009); like other traversal-based algorithms, qfill () can also perform operations on a part of the container, for example, the following code sets the first five digits of the vector to 1009, and the last five digits to 2013: qvector <int> vect (10 );
Qfill (vect. Begin (), vect. Begin () + 5, 1009 );
Qfill (vect. End ()-5, vect. End (), 2013 );
Qcopy ()The algorithm can replicate elements in one container to another container, for example:
Qvector <int> vect (list. Count ());
Qcopy (list. Begin (), list. End (), vect. Begin ());
Qcopy () can also be used for copying elements in the same container. The key to successful qcopy () operations is that the range of the source container and the target container does not overflow. For example, the following code copies the last two elements of a list to the first two elements: qcopy (list. begin (), list. begin () + 2, list. end ()-2 );Qsort ()Implements the incremental sorting of container elements, which is also easy 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 want to sort by descent, you need to pass qgreater <t> () as the third parameter to the qsort () function. For example, qsort (list. Begin (), list. End (), qgreater <int> (). Note that t is actually the generic type of the container. In fact, we can use the third parameter to define the sorting. For example, the user-defined data type contains a case-insensitive qstring less than the comparison function: bool insensitivelessthan (const qstring & str1, const qstring & str2)
{
Return str1.tolower () <str2.tolower ();
} So we can use qsort () to use this function: qstringlist;
//...
Qsort (list. Begin (), list. End (), insensitivelessthan );Qstablesort ()Similar to qsort (), a function is stable in sorting. Stable sorting is a term in algorithm design. It means that during the sorting process, if two elements are equal, in the sorting result, the order of the two elements is the same as the original order before the sorting. For example, for a sequence: A1, A5, A32, A31, and A4, the order of their sizes is A1 <A31 = A32 <A4 <A5, then, the result of the stable sorting should be A1, A32, A31, A4, A5, that is, the order in which equal elements appear in the sorting result is consistent with the original order. Stable sorting is useful in some scenarios. For example, there is a student transcript sorted by student ID. You want to sort the scores again based on the scores. for students with the same scores, you should follow the original student ID order. At this time, the sorting will be stable.Qdeleteall ()The function will delete all the pointers stored in the container. This function is applicable only when the container element is a pointer. After this function is executed, all the pointers in the container are executed with the delete operation, but these pointers are still stored in the container and become wild pointers. You need to call the container'sClear ()Function to avoid misuse of these pointers: qdeleteall (list );
List. Clear (); when we need to dynamically apply for a lot of space, but we cannot delete them for the moment, we can put them into the qlist and then delete them when appropriate.Qswap ()The function can swap the positions of two elements. Example: int X1 = line. x1 ();
Int X2 = line. X2 ();
If (x1> x2)
Qswap (x1, x2); finally, several useful functions are defined in the header file <qtglobal>. This header file is included by 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 a parameter,Qmin ()AndQmax ()Returns the maximum and minimum values of the two values.