【转】c++中Vector等STL容器的自定义排序

简介:

如果要自己定义STL容器的元素类最好满足STL容器对元素的要求
    必须要求:
     1、Copy构造函数
     2、赋值=操作符
     3、能够销毁对象的析构函数
    另外:
     1、可用的缺省构造函数,序列型容器必须,用于初始化元素
     2、==操作符定义,用于判断相等
     3、<操作符定义,关联型容器必须,用于缺省排序

 

你可在struct內加入 operator < ,就可以使struct有排序能力.
因為而你的pcd struct內沒有指針,所以不須要有copy constructor
和copy assignment, 編譯器會為你提供的, 你不須要自己做的.
當你要排序時只要寫 sort( obj.begin(), obj.end() )就可.

 

以上内容取自帖子:http://bbs.csdn.net/topics/40228627

另一篇参考地址:http://blog.csdn.net/tigernana/article/details/7293758

以下取自帖子:http://blog.csdn.net/guang11cheng/article/details/7556697

 

三种方式实现vector的自定义排序

方法1:重载运算符

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
53
54
55
56
57
58
59
60
61
62
#include <vector>
  #include <algorithm>
  #include <functional>
 
using  namespace  std;
struct  TItem
{
     int  m_i32Type;
     int  m_i32ID;
 
     bool  operator  <( const  TItem& rhs)  const  // 升序排序时必须写的函数
     {
         return  m_i32Type < rhs.m_i32Type;
     }
     bool  operator  >( const  TItem& rhs)  const  // 降序排序时必须写的函数
     {
         return  m_i32Type > rhs.m_i32Type;
     }
};
int  main()
{
     vector<TItem> stItemVec;
 
 
     TItem stItem1;
     stItem1.m_i32Type = 1;
     stItem1.m_i32ID = 1;
 
     TItem stItem2;
     stItem2.m_i32Type = 2;
     stItem2.m_i32ID = 2;
 
     TItem stItem3;
     stItem3.m_i32Type = 3;
     stItem3.m_i32ID = 3;
 
     TItem stItem4;
     stItem4.m_i32Type = 2;
     stItem4.m_i32ID = 4;
 
     stItemVec.push_back(stItem1);
     stItemVec.push_back(stItem2);
     stItemVec.push_back(stItem3);
     stItemVec.push_back(stItem4);
 
     // 升序排序
     sort(stItemVec.begin(), stItemVec.end(), less<TItem>());
     // 或者sort(ctn.begin(), ctn.end());   默认情况为升序
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
         printf( "type: %d, id: %d\n" , stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
 
     printf( "--\n" );
 
     // 降序排序
     sort(stItemVec.begin(), stItemVec.end(), greater<TItem>());
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
         printf( "type: %d, id: %d\n" , stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
 
     return  0;
}

方法2:全局的比较函数

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <vector>
  #include <algorithm>
  #include <functional>
  
 
using  namespace  std;
  
 
struct  TItem
  {
      int  m_i32Type;
      int  m_i32ID;
  };
  
 
bool  lessmark( const  TItem& stItem1,  const  TItem& stItem2)
  {
      return  stItem1.m_i32Type < stItem2.m_i32Type;
  }
  
 
bool  greatermark( const  TItem& stItem1,  const  TItem& stItem2)
  {
      return  stItem1.m_i32Type > stItem2.m_i32Type;
  }
  
 
int  main()
  {
      vector<TItem> stItemVec;
  
 
     TItem stItem1;
      stItem1.m_i32Type = 1;
      stItem1.m_i32ID = 1;
  
 
     TItem stItem2;
      stItem2.m_i32Type = 2;
      stItem2.m_i32ID = 2;
  
 
     TItem stItem3;
      stItem3.m_i32Type = 3;
      stItem3.m_i32ID = 3;
  
 
     TItem stItem4;
      stItem4.m_i32Type = 2;
      stItem4.m_i32ID = 4;
  
 
     stItemVec.push_back(stItem1);
      stItemVec.push_back(stItem2);
      stItemVec.push_back(stItem3);
      stItemVec.push_back(stItem4);
  
 
     sort(stItemVec.begin(), stItemVec.end(), lessmark);  //升序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  
 
     printf( "--\n" );
  
 
     sort(stItemVec.begin(), stItemVec.end(), greatermark);  //降序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  
 
     return  0;
  }

方法3:函数对象

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <vector>
  #include <algorithm>
  #include <functional>
  
 
using  namespace  std;
  
 
struct  TItem
  {
      int  m_i32Type;
      int  m_i32ID;
  };
  
 
class  CompLess
  {
  public :
      bool  operator  ()( const  TItem& stItem1,  const  TItem& stItem2)
      {
          return  stItem1.m_i32Type < stItem2.m_i32Type;
      }
  };
  
 
class  CompGreater
  {
  public :
      bool  operator  ()( const  TItem& stItem1,  const  TItem& stItem2)
      {
          return  stItem1.m_i32Type > stItem2.m_i32Type;
      }
  };
  
 
int  main()
  {
      vector<TItem> stItemVec;
  
 
     TItem stItem1;
      stItem1.m_i32Type = 1;
      stItem1.m_i32ID = 1;
  
 
     TItem stItem2;
      stItem2.m_i32Type = 2;
      stItem2.m_i32ID = 2;
  
 
     TItem stItem3;
      stItem3.m_i32Type = 3;
      stItem3.m_i32ID = 3;
  
 
     TItem stItem4;
      stItem4.m_i32Type = 2;
      stItem4.m_i32ID = 4;
  
 
     stItemVec.push_back(stItem1);
      stItemVec.push_back(stItem2);
      stItemVec.push_back(stItem3);
      stItemVec.push_back(stItem4);
  
 
     sort(stItemVec.begin(), stItemVec.end(), CompLess());  //升序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  
 
     printf( "--\n" );
  
 
     sort(stItemVec.begin(), stItemVec.end(), CompGreater());  //降序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i].m_i32Type, stItemVec[i].m_i32ID);
  
 
     return  0;
  }
  
 
/*
  结果如下:
  type: 1, id: 1
  type: 2, id: 2
  type: 2, id: 4
  type: 3, id: 3
  --
  type: 3, id: 3
  type: 2, id: 2
  type: 2, id: 4
  type: 1, id: 1
  可以看出vector的sort的稳定的。
  */

问题:

1,示例代码中只有>和<关系处理,==关系是如何推导出来的?

2,排序时要移动元素,效率怎样?

3,如果自定义结构定义在一个类的内部,使用函数对象进行排序,这个函数对象可以作为类的成员函数吗?

4,在上面的例子中,vector中存放的都是结构(对象)本身,如果存放的是结构指针,该如何排序呢?此时只能通过全局的比较函数或者函数对象来做,且比较函数的参数要是指针类型的,如下:

(1)全局的比较函数

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <vector>
  #include <algorithm>
  #include <functional>
  
 
using  namespace  std;
  
 
struct  TItem
  {
      int  m_i32Type;
      int  m_i32ID;
  };
  
 
bool  CompLess( const  TItem* pstItem1,  const  TItem* pstItem2)
  {
      return  pstItem1->m_i32Type < pstItem2->m_i32Type;
  }
  
 
bool  CompGreater( const  TItem* pstItem1,  const  TItem* pstItem2)
  {
      return  pstItem1->m_i32Type > pstItem2->m_i32Type;
  }
  
 
int  main()
  {
      vector<TItem*> stItemVec;
  
 
     TItem stItem1;
      stItem1.m_i32Type = 1;
      stItem1.m_i32ID = 1;
  
 
     TItem stItem2;
      stItem2.m_i32Type = 2;
      stItem2.m_i32ID = 2;
  
 
     TItem stItem3;
      stItem3.m_i32Type = 3;
      stItem3.m_i32ID = 3;
  
 
     TItem stItem4;
      stItem4.m_i32Type = 2;
      stItem4.m_i32ID = 4;
  
 
     stItemVec.push_back(&stItem1);
      stItemVec.push_back(&stItem2);
      stItemVec.push_back(&stItem3);
      stItemVec.push_back(&stItem4);
  
 
     sort(stItemVec.begin(), stItemVec.end(), CompLess);  //升序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
  
 
     printf( "--\n" );
  
 
     sort(stItemVec.begin(), stItemVec.end(), CompGreater);  //降序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
  
     return  0;
  }

  (2)函数对象

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <vector>
  #include <algorithm>
  #include <functional>
  
 
using  namespace  std;
  
 
struct  TItem
  {
      int  m_i32Type;
      int  m_i32ID;
  };
  
 
class  CompLess
  {
  public :
      bool  operator  ()( const  TItem* pstItem1,  const  TItem* pstItem2)
      {
          return  pstItem1->m_i32Type < pstItem2->m_i32Type;
      }
  };
  
 
class  CompGreater
  {
  public :
      bool  operator  ()( const  TItem* pstItem1,  const  TItem* pstItem2)
      {
          return  pstItem1->m_i32Type > pstItem2->m_i32Type;
      }
  };
  
 
int  main()
  {
      vector<TItem*> stItemVec;
  
 
     TItem stItem1;
      stItem1.m_i32Type = 1;
      stItem1.m_i32ID = 1;
  
 
     TItem stItem2;
      stItem2.m_i32Type = 2;
      stItem2.m_i32ID = 2;
  
 
     TItem stItem3;
      stItem3.m_i32Type = 3;
      stItem3.m_i32ID = 3;
  
 
     TItem stItem4;
      stItem4.m_i32Type = 2;
      stItem4.m_i32ID = 4;
  
 
     stItemVec.push_back(&stItem1);
      stItemVec.push_back(&stItem2);
      stItemVec.push_back(&stItem3);
      stItemVec.push_back(&stItem4);
  
 
     sort(stItemVec.begin(), stItemVec.end(), CompLess());  //升序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
  
 
     printf( "--\n" );
  
 
     sort(stItemVec.begin(), stItemVec.end(), CompGreater());  //降序排序
  
 
     for  (size_t i = 0; i < stItemVec.size(); i++)
          printf( "type: %d, id: %d\n" , stItemVec[i]->m_i32Type, stItemVec[i]->m_i32ID);
  
 
     return  0;
  }

  本文转自编程小翁博客园博客,原文链接:http://www.cnblogs.com/wengzilin/p/3937491.html,如需转载请自行联系原作者

相关文章
|
10月前
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
10月前
|
算法 编译器 C++
模拟实现c++中的vector模版
模拟实现c++中的vector模版
|
存储 编译器 C语言
【c++丨STL】vector的使用
本文介绍了C++ STL中的`vector`容器,包括其基本概念、主要接口及其使用方法。`vector`是一种动态数组,能够根据需要自动调整大小,提供了丰富的操作接口,如增删查改等。文章详细解释了`vector`的构造函数、赋值运算符、容量接口、迭代器接口、元素访问接口以及一些常用的增删操作函数。最后,还展示了如何使用`vector`创建字符串数组,体现了`vector`在实际编程中的灵活性和实用性。
711 4
|
存储 对象存储 C++
C++ 中 std::array<int, array_size> 与 std::vector<int> 的深入对比
本文深入对比了 C++ 标准库中的 `std::array` 和 `std::vector`,从内存管理、性能、功能特性、使用场景等方面详细分析了两者的差异。`std::array` 适合固定大小的数据和高性能需求,而 `std::vector` 则提供了动态调整大小的灵活性,适用于数据量不确定或需要频繁操作的场景。选择合适的容器可以提高代码的效率和可靠性。
|
存储 编译器 C语言
【c++丨STL】vector模拟实现
本文深入探讨了 `vector` 的底层实现原理,并尝试模拟实现其结构及常用接口。首先介绍了 `vector` 的底层是动态顺序表,使用三个迭代器(指针)来维护数组,分别为 `start`、`finish` 和 `end_of_storage`。接着详细讲解了如何实现 `vector` 的各种构造函数、析构函数、容量接口、迭代器接口、插入和删除操作等。最后提供了完整的模拟实现代码,帮助读者更好地理解和掌握 `vector` 的实现细节。
283 0
|
存储 C++ 索引
【C++打怪之路Lv9】-- vector
【C++打怪之路Lv9】-- vector
259 1
|
算法 C++ 容器
C++之打造my vector篇(下)
C++之打造my vector篇(下)
|
10月前
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
6月前
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
190 0
|
6月前
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
281 0