当前位置:   article > 正文

[C++ Primer] C++中sort对类对象进行排序_c++ 对象排序

c++ 对象排序

    使用C++进行编程时,常常需要对处理对象进行排序,因此C++中提供了sort范型算法。

1 sort的原型

  1. template <class RandomAccessIterator>
  2. void sort (RandomAccessIterator first, RandomAccessIterator last);
  3. template <class RandomAccessIterator, class Compare>
  4. void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
上面是两个范型算法的模板。

第一个函数有两个参数,分别是随机访问迭代器first和last,它默认使用迭代器引用的operator<进行排序。

第二个函数有三个参数,前两个和第一个一样,第三个参数是一个Compare,也就是说它使用comp对迭代器引用的对象进行排序。

comp是一个二元函数,它接受前两个参数指定范围内的两个元素,它的返回值可以转换为bool类型,这个函数不改变迭代器引用的对象,而且,它可以是函数指针和函数对象。

需要注意的是,这两个排序函数不是稳定的,稳定的排序可以看看stable_sort。


2 一个对整数进行排序的简单例子

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. int cmp_func(int a, int b)
  6. {
  7. if(a > b)
  8. return true;
  9. return false;
  10. }
  11. void print_res(int x)
  12. {
  13. cout << x << " ";
  14. }
  15. int main(int argc, char *argv[])
  16. {
  17. int arr[] = {6, 4, 8, 2, 1};
  18. int len = sizeof(arr) / sizeof(arr[0]);
  19. vector<int> ivec(arr, arr + len);
  20. sort(ivec.begin(), ivec.end());
  21. for_each(ivec.begin(), ivec.end(), print_res);
  22. cout << endl;
  23. sort(ivec.begin(), ivec.end(), cmp_func);
  24. for_each(ivec.begin(), ivec.end(), print_res);
  25. cout << endl;
  26. return 0;
  27. }
上面是一个使用sort的简单例子,定义了一个比较函数cmp_func,当a > b时,返回真,否则返回假。

主函数中先调用了两个形参的函数,然后调用了三个形参的函数。

第一个默认使用operator<排序,因此结果是1 2 4 6 8 。

第二个使用给定的cmp_func函数排序,因此结果是8 6 4 2 1。


3 使用sort对类对象进行排序

3.1 使用自定义的比较函数

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. struct Test{
  6. int val;
  7. Test(int x) : val(x) { }
  8. };
  9. bool nonmem_cmp(const Test &t1, const Test &t2)
  10. {
  11. if(t1.val > t2.val)
  12. return true;
  13. return false;
  14. }
  15. void print_res(Test t)
  16. {
  17. cout << t.val << " ";
  18. }
  19. int main(int argc, char *argv[])
  20. {
  21. vector<Test> ivec;
  22. ivec.push_back(Test(6));
  23. ivec.push_back(Test(4));
  24. ivec.push_back(Test(8));
  25. ivec.push_back(Test(2));
  26. ivec.push_back(Test(1));
  27. sort(ivec.begin(), ivec.end(), nonmem_cmp);
  28. for_each(ivec.begin(), ivec.end(), print_res);
  29. cout << endl;
  30. return 0;
  31. }
上例中定义了一个nonmem_cmp函数,然后在第二个版本的sort函数中的第三个参数指定为nonmem_cmp函数,就能够对迭代器引用区间的元素用nonmem_cmp函数进行排序。相当简单吧?


3.2 重载operator<操作符

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. struct Test{
  6. int val;
  7. Test(int x) : val(x) { }
  8. bool operator<(const Test &t) const
  9. {
  10. if(val < t.val)
  11. return true;
  12. return false;
  13. }
  14. };
  15. bool operator<(const Test &t1, const Test &t2)
  16. {
  17. if(t1.val < t2.val)
  18. return true;
  19. return false;
  20. }
  21. void print_res(Test t)
  22. {
  23. cout << t.val << " ";
  24. }
  25. int main(int argc, char *argv[])
  26. {
  27. vector<Test> ivec;
  28. ivec.push_back(Test(6));
  29. ivec.push_back(Test(4));
  30. ivec.push_back(Test(8));
  31. ivec.push_back(Test(2));
  32. ivec.push_back(Test(1));
  33. sort(ivec.begin(), ivec.end());
  34. for_each(ivec.begin(), ivec.end(), print_res);
  35. cout << endl;
  36. return 0;
  37. }
上面的代码用两种方式为Test类定义了operator<,当然,要想代码正确运行,只能有一种方式:成员函数或者非成员函数。此时,就能够用sort的第一个版本对迭代器引用区间的元素进行排序,此时使用operator<进行。上面的结果就是:1 2 4 6 8。


3.3 使用自定义的函数对象

什么是函数对象?就是定义了函数调用操作符()的类的对象。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. struct Test {
  6. int val;
  7. Test(int x) : val(x) { }
  8. };
  9. class Cmp {
  10. public:
  11. int val;
  12. bool operator()(const Test &t1, const Test &t2)
  13. {
  14. if(t1.val < t2.val)
  15. return true;
  16. return false;
  17. }
  18. };
  19. void print_res(Test t)
  20. {
  21. cout << t.val << " ";
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. vector<Test> ivec;
  26. ivec.push_back(Test(6));
  27. ivec.push_back(Test(4));
  28. ivec.push_back(Test(8));
  29. ivec.push_back(Test(2));
  30. ivec.push_back(Test(1));
  31. sort(ivec.begin(), ivec.end(), Cmp());
  32. for_each(ivec.begin(), ivec.end(), print_res);
  33. cout << endl;
  34. return 0;
  35. }
上面的代码定义了类Test,只定义了类成员变量和构造函数。

然后定义了一个类Cmp,它定义了operator(),因此Cmp的对象就是函数对象,它的对象可以当作函数一样使用。

在Cmp的operator()中,有两个Test类类型的参数,它们进行比较得到布尔值。

在主函数中,使用sort的第二个版本,而且第三个参数用的是默认构造函数构造的一个Cmp临时对象。

假设Cmp()创建的临时变量是compare,即Cmp compare,sort(ivec.begin(), ivec.end(), compare),在比较两个Test类对象时,会调用compare(t1, t2),由于我们为Cmp定义了operator()操作符,而且参数就是两个Test类对象,因此,该函数可以正常工作。


3.4 对<functional>中的类模板进行派生创建函数对象

在<functional>中定义了多种模板,这里使用二元函数模板。

  1. template <class Arg1, class Arg2, class Result>
  2. struct binary_function {
  3. typedef Arg1 first_argument_type;
  4. typedef Arg2 second_argument_type;
  5. typedef Result result_type;
  6. };
以上是二元函数结构体的模板定义,它只定义了三个参数,分别是二元函数的两个操作数和返回值。

在使用中我们需要对上面的类进行派生,添加operator(),然后就可以使用函数对象了。

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <functional>
  4. #include <vector>
  5. using namespace std;
  6. struct Test {
  7. int val;
  8. Test(int x) : val(x) { }
  9. };
  10. class Cmp : public binary_function<Test, Test, bool> {
  11. public:
  12. bool operator()(const Test &t1, const Test &t2)
  13. {
  14. if(t1.val < t2.val)
  15. return true;
  16. return false;
  17. }
  18. };
  19. void print_res(Test t)
  20. {
  21. cout << t.val << " ";
  22. }
  23. int main(int argc, char *argv[])
  24. {
  25. vector<Test> ivec;
  26. ivec.push_back(Test(6));
  27. ivec.push_back(Test(4));
  28. ivec.push_back(Test(8));
  29. ivec.push_back(Test(2));
  30. ivec.push_back(Test(1));
  31. sort(ivec.begin(), ivec.end(), Cmp());
  32. for_each(ivec.begin(), ivec.end(), print_res);
  33. cout << endl;
  34. return 0;
  35. }
这里,我们的Cmp派生于<functional>中struct binary_function,然后在Cmp中添加operator(),主函数中的代码不变,就能够像我们之前创建的实现operator()的类一样工作,不过,貌似比我们的要简单一些,“派生”,真是个好注意!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/926447
推荐阅读
相关标签
  

闽ICP备14008679号