Qt (B1) Sort of QList
Published on| June 15th, 2009 | No Comment.
Categories:Qt |
Description: Qt (1) - (10) until, Qt has explained the characteristics of a street. (Yet, you might finish writing minutes, at any time, Please note that you posted. ) Now, Qt B series, we described how to use the basic classes. These articles, Qt is the article for beginners.
The theme was, QList is for sorting. QtAlgorithms of qSort I briefly describes the cases of the sort to do it.
Qt is eject multiple on-screen information, and most are using QList template.
QList the inside, along with the intended use, and is in line IEMASEN. In that, QList or search for a while, you may need to order or reorder.
This time, ascending, descending, QList try sort.
Sample source code you used here:
Int sort of simple information
Simplest QList <int> try to sort it.
[sample.cpp]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <QtGui> #include <iostream> using namespace std; // dump list void outputList(const char *title,QList<int> &plist ) { int ni; std::cout << title << std::endl; for(ni=0;ni<plist.count();ni++){ std::cout << "[" << ni << "]="<< plist.at(ni) << std::endl; } } // main int main(int /* argc */, char* /*argv[]*/ ) { QList<int> olist; olist.append(1); olist.append(3); olist.append(2); outputList("Not Sort",olist); // sort qSort(olist); outputList("Sorted",olist); return 0; } |
6 from the line 14 to line outputList, QList is the function to the console output.
Sort of running in practice, in line 27.
to qSort, QList is only passing.
Compile the above, try to run.
Above, So Consol Application, compiled as follows.
Articles: Qt (1) examine the control path in the Qt Consol Application
> qmake -project > qmake "CONFIG+=console" > nmake
Results are as follows.
> debug\sort1.exe Not Sort [0]=1 [1]=3 [2]=2 Sorted [0]=1 [1]=2 [2]=3
Notice that the sort in ascending order to clean.
Now, with a separate class QList is sort of what to do?
QList class of its own sort of
For example, you might create a class name with the following information.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // class for test class MyName { public : MyName(const QString name) { m_sname=name; }; MyName(const MyName &other) { *this=other; }; QString WhatYourName() const { return m_sname; }; MyName & operator =(const MyName &other) { m_sname=other.m_sname;return *this; }; private: QString m_sname; }; |
Method will query the name "WhatYourName" is about. This class is easy.
(QList has made it necessary to use the Copy Constructor. )
QList create this class, we will implement the sort.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | // main int main(int /* argc */, char* /*argv[]*/ ) { QList<MyName> olist; MyName my1("test"); MyName my2("taro"); MyName my3("hanako"); olist.append(my1); olist.append(my2); olist.append(my3); outputList("Not Sort",olist); // sort qSort(olist); outputList("Sorted",olist); return 0; } |
Like this, "test", "taro", "hanako" QList in order to sort the things in, and output.
Error to compile it.
qalgorithms.h(178) : error C2678:
This is, qSort methods are required, MyName does not exist error.
qSort If you use the above, MyName the operator < must be overloaded.
In other words, qSort, each class of operator < is open, it has been sorted.
So, immediately, operator < Let's add.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // class for test class MyName { public : MyName(const QString name) { m_sname=name; }; MyName(const MyName &other) { *this=other; }; QString WhatYourName() const { return m_sname; }; MyName & operator =(const MyName &other) { m_sname=other.m_sname;return *this; }; bool operator <(const MyName &other) const { return (m_sname<other.WhatYourName()); }; private: QString m_sname; }; |
12 line operator < has been added.
You can compile it and compile it successfully, we will continue to run.
> debug\sort2.exe Not Sort [0]=test [1]=taro [2]=hanako Sorted [0]=hanako [1]=taro [2]=test
Well, we sort in ascending order of their names.
Next, try to sort in descending order.
Sort of its own class QList (low)
To sort in descending order are, qSort parameter specifies to sort in descending order.
Until now, the following parameters were qSort to.
1 | qSort(olist); |
Descending to, the following parameters to specify qSort.
1 | qSort(olist.begin(),olist.end(),qGreater<MyName>()); |
The second argument, olist sort of range. The third argument is specified to be performed in descending order.
qGreater there is a template function, simply specify a class there.
Originally, ascending or just a short man with a parameter to specify the path as follows.
1 | qSort(olist.begin(),olist.end(),qLess<MyName>()); |
Descending the qGreater, qLess This means ascending order.
Then, the following main changes to the routine, try to run.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // main int main(int /* argc */, char* /*argv[]*/ ) { QList<MyName> olist; MyName my1("test"); MyName my2("taro"); MyName my3("hanako"); olist.append(my1); olist.append(my2); olist.append(my3); outputList("Not Sort",olist); // sort - qLess qSort(olist.begin(),olist.end(),qLess<MyName>()); outputList("Sorted(qLess)",olist); // sort - qGreater qSort(olist.begin(),olist.end(),qGreater<MyName>()); outputList("Sorted(qGreater)",olist); return 0; } |
Results are as follows.
> debug\sort4.exe Not Sort [0]=test [1]=taro [2]=hanako Sorted(qLess) [0]=hanako [1]=taro [2]=test Sorted(qGreater) [0]=test [1]=taro [2]=hanako
Next, QList sort of case that set the address of an object is, what shall I do?
Pages: 1 2
You might also like:
Trackback URL
After Admin approves this comment, it will be shown.
Comments
Leave a Reply
