人比较懒。。。直接上代码:
1
#include
"
stdafx.h
"
2 #include < Windows.h >
3 #include < vector >
4 #include < algorithm >
5 #include < iostream >
6 #include < functional >
7
8
9 class xTestElement
10 {
11 public :
12 xTestElement()
13 {
14 }
15 virtual ~ xTestElement()
16 {
17 }
18 // return whether self's m_value is less then another object's m_value
19 bool operator < ( const xTestElement & rhv ) const
20 {
21 return this -> m_value < rhv.m_value;
22 }
23
24 public :
25 int m_value;
26 };
27
28 class xTestElementSorter
29 {
30 public :
31 // Return whether first element is less than the second
32 bool operator () ( const xTestElement * a, const xTestElement * b) const
33 {
34 return * a < * b;
35 };
36 };
37
38 // Return whether first element is less than the second
39 bool Testless(xTestElement * a,xTestElement * b)
40 {
41 return * a < * b;
42 }
43
44 typedef std::vector < xTestElement *> vTestQueue;
45 typedef vTestQueue::iterator itTestQueue;
46 vTestQueue g_vQueues;
47
48
49 int _tmain( int argc, _TCHAR * argv[])
50 {
51 xTestElement TestQueue[ 10 ];
52 for ( int i = 0 ;i < 10 ;i ++ )
53 {
54 TestQueue[i].m_value = rand();
55 std::cout << TestQueue[i].m_value << std::endl;
56 g_vQueues.push_back( & TestQueue[i]);
57 }
58
59 std::cout << " begin sort :[ " << GetTickCount() << " ]! " << std::endl;
60 // use sort class
61 std::sort(g_vQueues.begin() , g_vQueues.end() , xTestElementSorter());
62 // use sort function
63 // std::sort(g_vQueues.begin() , g_vQueues.end() , Testless);
64
65 std::cout << " end sort :[ " << GetTickCount() << " ]! " << std::endl;
66
67 for (itTestQueue it = g_vQueues.begin();it != g_vQueues.end(); ++ it)
68 {
69 std::cout << ( * it) -> m_value << std::endl;
70 }
71 g_vQueues.clear();
72 return 0 ;
73 }
74
75
2 #include < Windows.h >
3 #include < vector >
4 #include < algorithm >
5 #include < iostream >
6 #include < functional >
7
8
9 class xTestElement
10 {
11 public :
12 xTestElement()
13 {
14 }
15 virtual ~ xTestElement()
16 {
17 }
18 // return whether self's m_value is less then another object's m_value
19 bool operator < ( const xTestElement & rhv ) const
20 {
21 return this -> m_value < rhv.m_value;
22 }
23
24 public :
25 int m_value;
26 };
27
28 class xTestElementSorter
29 {
30 public :
31 // Return whether first element is less than the second
32 bool operator () ( const xTestElement * a, const xTestElement * b) const
33 {
34 return * a < * b;
35 };
36 };
37
38 // Return whether first element is less than the second
39 bool Testless(xTestElement * a,xTestElement * b)
40 {
41 return * a < * b;
42 }
43
44 typedef std::vector < xTestElement *> vTestQueue;
45 typedef vTestQueue::iterator itTestQueue;
46 vTestQueue g_vQueues;
47
48
49 int _tmain( int argc, _TCHAR * argv[])
50 {
51 xTestElement TestQueue[ 10 ];
52 for ( int i = 0 ;i < 10 ;i ++ )
53 {
54 TestQueue[i].m_value = rand();
55 std::cout << TestQueue[i].m_value << std::endl;
56 g_vQueues.push_back( & TestQueue[i]);
57 }
58
59 std::cout << " begin sort :[ " << GetTickCount() << " ]! " << std::endl;
60 // use sort class
61 std::sort(g_vQueues.begin() , g_vQueues.end() , xTestElementSorter());
62 // use sort function
63 // std::sort(g_vQueues.begin() , g_vQueues.end() , Testless);
64
65 std::cout << " end sort :[ " << GetTickCount() << " ]! " << std::endl;
66
67 for (itTestQueue it = g_vQueues.begin();it != g_vQueues.end(); ++ it)
68 {
69 std::cout << ( * it) -> m_value << std::endl;
70 }
71 g_vQueues.clear();
72 return 0 ;
73 }
74
75