The model-view section of Qt has come to an end. Today we will start a new section. Some friends may think that the previous section is very simple. There is no way for me to do this. After all, Qt is a very large library, and at half past one it will be impossible to exhaust all the content, and I also have a lot of things I don't know, sometimes you have to search for information to understand. 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. The first is the 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 is list. begin () + 1, and j will be list. end (): 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> QStringList;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "align =" top "/> list <" Emma "<" Karl "<" James "<" Mariette ";
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "align =" top "/> QStringList: iterator I = qFind (list. begin (), list. end (), "Karl ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "align =" top "/> QStringList: iterator j = qFind (list. begin (), list. end (), "Petra"); qBinaryFind () acts like qFind (). The difference is that qBinaryFind () is a binary search algorithm, it is only applicable to search for sorted sets, while qFind () is a standard linear query. Generally, binary lookup is more demanding, but more efficient. QFill () fills the container with the given value. Example: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> q1_list <int> list (10 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "align =" top "/> 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: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> QVector <int> vect (10 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> qFill (vect. begin (), vect. begin () + 5, 1009 );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "align =" top "/> qFill (vect. end ()-5, vect. end (), 2013); The qCopy () algorithm can replicate elements in one container to another container, for example, 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> QVector <int> vect (list. count ());
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'onclick =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "Align =" top "/> 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, in the following code, we will copy the last two elements of a list to the first two elements: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'onclick =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "Align =" top "/> qCopy (list. begin (), list. begin () + 2, list. end ()-2); qSort () implements the incremental sorting of container elements, which is also easy to use: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'onclick =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "Align =" top "/> qSort (list. begin (), list. end (); by default, qSort () compares elements using the <operator. 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. Example: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'onclick =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "Align =" top "/> 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: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'onclick =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "Align =" top "/> bool insensitiveLessThan (const QString & str1, const QString & str2)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> return str1.toLower () <str2.toLower ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "align =" top "/>} so we can use qSort () so that we can use this function: 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> QStringList;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> //...
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'onclick =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "Align =" top "/> qSort (list. begin (), list. end (), insensitiveLessThan); The qStableSort () function is similar to qSort (). The difference is that it is a stable 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. The qDeleteAll () function deletes 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 clear () Operation of the container () function to avoid misuse of these pointers: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) 'onclick =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "Align =" top "/> qDeleteAll (list );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> list. clear (); The qSwap () function can swap the positions of two elements. Example: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> int x1 = line. x1 ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> int x2 = line. x2 ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif" align = "top"/> if (x1> x2)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'onclick =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1Q1206296-0.gif "align =" top "/> 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. The header file contains the following functions: qAbs () returns the absolute value of the parameter. qMin () and qMax () return the maximum and minimum values of the two values.
This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/272421