Home

OFF-SOFT.net

OFF-SOFT.net

This site is support & information site of WEB,and Software. This site might help you that create software or Web Site…perhaps?[:]

Qt (B1) Sort of QList

Published on| June 15th, 2009 | 1 Comment.
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?

Sorting QList object address
QList address below if you are in management, will be sorted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//	main
int main(int /* argc */, char* /*argv[]*/ )
{
	QList<MyName*> olist;
	MyName *my1=new MyName("test");
	MyName *my2=new MyName("taro");
	MyName *my3=new MyName("hanako");
	olist.append(my1);
	olist.append(my2);
	olist.append(my3);
 
	outputList("Not Sort",olist);
 
	//	sort - origin method
	qSort(olist);
	outputList("Sorted",olist);
 
	qDeleteAll(olist);
	olist.clear();
 
	return 0;
}

Class MyName that is exactly the same as the previous sample. QList only address the management.

Line 18, QList case management to address, you must delete themselves. Function is able to implement it, qDeleteAll.
When you delete by itself, a step there, it is useful to know.

The following results and try to compile and run it.

> debug\sort3.exe
Not Sort
[0]=test
[1]=taro
[2]=hanako
Sorted
[0]=test
[1]=taro
[2]=hanako

Does not change anything. It is only by comparing the address of an object is a natural result.
To compare the contents of the object are required, they must have a function that compare.

Below, for example, MyName comparison class provides static methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//	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()); };
 
	static bool   compare(const MyName *d1,const MyName *d2){ return (d1->WhatYourName()<d2->WhatYourName()); };
 
private:
	QString m_sname;
};

Line 14, the static method.
The argument of this method, MyName will address the comparison of the two. Bool compare the results and outputs.

Per this, C-language standard qsort function is the same as how to handle it.

Also, to compare this method with the parameter qSort.

1
	qSort(olist.begin(),olist.end(),MyName::compare);

Specify how this is not the same as specified in descending order. The last function was not descending, MyName prepared in the compare is specified.

This, to compile, try to run.

> debug\sort3_2.exe
Not Sort
[0]=test
[1]=taro
[2]=hanako
Sorted
[0]=hanako
[1]=taro
[2]=test

We sorted out.
Here, MyName in, static methods are available, the same argument, the usual sort C can be the same to them and provide the function.

Now, the last qUpperBound, qLowerBound Let's do a little bit.

QList in, find a location
qUpperBound, qLowerBound, the use is a very useful function, and then try to explain it.
qUpperBound, qLowerBound, respectively, in the sort QList the end of the specified information, the first is able to output iterator.

Described in a simple example. With a name like: you have a QList <MyName>.
i.e) If it wants all items where you include word "Test", the function of qUpperBound and qLowerBound will help you very powerful.

QList[0]=hanako
QList[1]=taro
QList[2]=test
QList[3]=test
QList[4]=test
QList[5]=test1

1
2
3
4
	MyName my_check("test");
 
	QList<MyName>::iterator ipos1=qUpperBound(olist.begin(),olist.end(),my_check);
	QList<MyName>::iterator ipos2=qLowerBound(olist.begin(),olist.end(),my_check);

Thus, qUpperBound, qLowerBound use. my_check is the object for comparison.
ipos1 in, QList [5] = test1
ipos2 in, QList [2] = test
Is set.

In other words, qLowerBound will hunt first object of "test" (QList [2]).
Also, qUpperBound will hunt last (+1) object of "test" (QList [5]).
The last, QList [4] However, note that you retrieve the +1 position.

The following is an existence of sorts, which is testing whether search?
Naturally, if the operation is not sorted in ascending order is not guaranteed.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//	main
int main(int /* argc */, char* /*argv[]*/ )
{
	QList<MyName>::iterator ipos;
	QList<MyName> olist;
	MyName my1("test",1);
	MyName my12("test",2);
	MyName my13("test",3);
	MyName my2("taro",4);
	MyName my3("hanako",5);
	MyName my14("test1",6);
	olist.append(my1);
	olist.append(my12);
	olist.append(my13);
	olist.append(my2);
	olist.append(my3);
	olist.append(my14);
 
 
	MyName my_check("test",-1);
 
	outputList("Not Sort",olist);
 
	ipos=qUpperBound(olist.begin(),olist.end(),my_check);
	outputItem("[Not Sort(Upper)]",ipos,olist.end());
 
	ipos=qLowerBound(olist.begin(),olist.end(),my_check);
	outputItem("[Not Sort(Lower)]",ipos,olist.end());
 
	//	sort - qLess
	qSort(olist.begin(),olist.end(),qLess<MyName>());
	outputList("Sorted(qLess)",olist);
 
	ipos=qUpperBound(olist.begin(),olist.end(),my_check);
	outputItem("[Sorted(qLess)(Upper)]",ipos,olist.end());
 
	ipos=qLowerBound(olist.begin(),olist.end(),my_check);
	outputItem("[Sorted(qLess)(Lower)]",ipos,olist.end());
 
	//	sort - qGreater
	qSort(olist.begin(),olist.end(),qGreater<MyName>());
 
	outputList("Sorted(qGreater)",olist);
 
	ipos=qUpperBound(olist.begin(),olist.end(),my_check);
	outputItem("[Sorted(qGreater)(Upper)]",ipos,olist.end());
 
	ipos=qLowerBound(olist.begin(),olist.end(),my_check);
	outputItem("[Sorted(qGreater)(Lower)]",ipos,olist.end());
 
	return 0;
}

Compile the above as a result of execution is as follows.

> debug\sort5.exe
Not Sort
[0]=test(1)
[1]=test(2)
[2]=test(3)
[3]=taro(4)
[4]=hanako(5)
[5]=test1(6)
[Not Sort(Upper)]test1(6)
[Not Sort(Lower)]test1(6)
Sorted(qLess)
[0]=hanako(5)
[1]=taro(4)
[2]=test(1)
[3]=test(3)
[4]=test(2)
[5]=test1(6)
[Sorted(qLess)(Upper)]test1(6)
[Sorted(qLess)(Lower)]test(1)
Sorted(qGreater)
[0]=test1(6)
[1]=test(2)
[2]=test(3)
[3]=test(1)
[4]=taro(4)
[5]=hanako(5)
[Sorted(qGreater)(Upper)]** last ** previous item --> hanako(5)
[Sorted(qGreater)(Lower)]test1(6)

This process has made a basic sort. QtAlgorithms, search and other qFind, copy and qCopy are useful.
QtAlgorithms is basically a template iterator class work if you're managing.
(QVector from, QList, and can be copied to. )

You can create your own, it is useful to know.

If you have noticed something, I would appreciate comments.

Comments

1 Comment. “Qt (B1) Sort of QList”


  1. oscar
    February 3rd, 2014 @ 11:26:06

    buena informacion
    me ayudo en mucho

Leave a Reply







  • はてなブックマークへ追加する
  • Facebookでシェアする
  • twitter でつぶやく
  • Google Plusでシェアする
  • Pocketでシェアする
ページトップへ