30.algorithm排序小结

  • 如果容器中是类,如果要调用sort则需要重载操作符 "<"
  • 包含头文件
    1 #define _CRT_SECURE_NO_WARNINGS
    2 #include <vector>
    3 #include <list>
    4 #include <algorithm>
    5 #include <iostream>
    6 #include <algorithm>
    7 #include <string>
    8 #include <iterator>
    9 using namespace std;
  • 通过结构体vector对象数组与仿函数对对象进行排序
     1 //vector结构体排序
     2 struct student
     3 {
     4     int score;
     5     char name[20];
     6     student(int s, char *n)
     7     {
     8         score = s;
     9         strcpy(name, n);
    10     }
    11     //需要重载小于运算符来对vector数组进行排序
    12     bool operator <(const student &s2)
    13     {
    14         if (this->score > s2.score)
    15         {
    16             return 1;
    17         }
    18         else
    19         {
    20             return 0;
    21         }
    22     }
    23 };
    24 
    25 void main1()
    26 {
    27     vector<student> mylist{ {100,"1"},{99,"2"},{88,"3"},{ 77,"4" } };
    28     //取前两名
    29     partial_sort(mylist.begin(), mylist.begin() + 2, mylist.end());
    30 
    31     for (auto i : mylist)
    32     {
    33         cout << i.name <<"  " << i.score << endl;
    34     }
    35 
    36     cin.get();
    37 }
  • sort()对数组进行排序,以及取出前三名
     1 //数组的sort
     2     {
     3         vector<int> mylist{ 1,9,2,8,3,7 };
     4         sort(mylist.begin(), mylist.end());
     5         for (auto i : mylist)
     6         {
     7             cout << i << endl;
     8         }
     9     }
    10     cout << endl;
    11 
    12     //只取一部分(比如对一串数据只取前三名)
    13     {
    14         vector<int> mylist{ 1,9,2,8,3,7 };
    15         partial_sort(mylist.begin(), mylist.begin() + 4, mylist.end());
    16         for (auto i : mylist)
    17         {
    18             cout << i << endl;
    19         }
    20     }
  • 全排列,一直对调,随机打乱,一直到升序的时候不能再打乱
     1 //全排列
     2     //一直对调,随机打乱,一直到升序的时候不能再打乱
     3     {
     4         int a[5] = { 9,5,3,4,5 };
     5         int count = 0;
     6         do
     7         {
     8             for (auto i : a)
     9             {
    10                 cout << i;
    11             }
    12             cout << endl;
    13             count++;
    14         } while (prev_permutation(a, a + 5));
    15         cout << count << endl;
    16     }
  • 全排列随机对调,直到降序不能再打乱
     1 int a[5] = { 9,5,3,4,5 };
     2         int count = 0;
     3         do
     4         {
     5             for (auto i : a)
     6             {
     7                 cout << i;
     8             }
     9             cout << endl;
    10             count++;
    11             //如果有序,则返回值是零
    12         } while (next_permutation(a, a + 5));
    13         cout << count << endl;
  • 自动对字符串进行排序
     1 char *s1 = "book";
     2         char *s2 = "hello";
     3         bool res = lexicographical_compare(s1, s1 + strlen(s1), s2, s2 + strlen(s2));
     4         if (res)
     5         {
     6             cout << "s1在s2前面" << endl;
     7         }
     8         else
     9         {
    10             cout << "s1在s2后面" << endl;
    11         }
  • 分块排序,小的向左,大 的向右
    1 //分块排序
    2     {
    3         int a[15] = { 3,4,5,6,7,8,32,23,4325,65,56,5,34,43,3 };
    4         //以a+8为中心,小于向左,大于向右
    5         nth_element(a, a + 8, a + 14);
    6         for_each(a, a + 14, [](int x) {cout << x << endl; });
    7     }
  • 求最大最小
    1 {
    2         //求最大值最小值
    3         int a = max({ 1,2,3,4,5 });
    4         int b = min({ 1,2,3,4,5 });
    5         auto it = minmax({ 1,2,3,4,5 });
    6         cout << a << endl;
    7         cout << it.first << endl;
    8         cout << it.second << endl;
    9     }
  • 判断是全满足还是部分满足还是都不满足
     1 //all_of  全部满足
     2     //any_of  至少有一个满足
     3     //none_of 都不满足
     4     {
     5         vector<int> myint{ 1,2,3,4,5 };
     6         bool isit = all_of(myint.begin(), myint.end(), [](int i)->bool {return i % 2 != 0; });
     7         //类似还有 any_of none_of
     8         if (isit)
     9         {
    10             cout << "OK" << endl;
    11         }
    12         else
    13         {
    14             cout << "NOT OK" << endl;
    15         }
    16     }
  • 条件拷贝
    1 //copy_if 条件拷贝
    2     {
    3         vector<int> v1{ 1,2,3,4,5 };
    4         vector<int> v2(v1.size());
    5         auto it = copy_if(v1.begin(), v1.end(), v2.begin(),[](int i) {return i % 2; });
    6         //压缩容器
    7         v1.resize(distance(v2.begin(), it));
    8     }
  • 部分排序以后复制到另外一个容器对副本进行操作(局部排序)
     1 void main22()
     2 {
     3     //数组的sort
     4     {
     5         vector<int> mylist{ 1,9,2,8,3,7 };
     6         int a[6]{ 0 };
     7         partial_sort_copy(mylist.begin(), mylist.end(), a, a + 6);
     8     }
     9     cin.get();
    10 }
  • 容器二分查找
     1 void main3()
     2 {
     3     int a[10] = { 1,9,2,8,3,7,4,6,5,0 };
     4     //默认从小到大
     5     sort(a, a + 10, [](int a, int b) {return a < b; });
     6     for (auto i : a)
     7     {
     8         cout << i << endl;
     9     }
    10 
    11     //取数组中的最小值
    12     cout << *min_element(a, a + 10) << endl;
    13     //取数组中的最大值
    14     cout << *max_element(a, a + 10) << endl;
    15     
    16     //二分查找(必须从小到大)
    17     auto it = binary_search(a, a + 10, 4);
    18     if (it)
    19     {
    20         cout << "find" << endl;
    21     }
    22     else
    23     {
    24         cout << "not find" << endl;
    25     }
    26     cin.get();
    27 }
  • max min操作
    1 //取max
    2 void main4()
    3 {
    4     int num1 = max(100, 90);
    5     int num2 = min(100, 90);
    6     cout << max((char *)"123", (char *)"234", [](const char *str1, const char *str2)->bool {return strcmp(str1, str2) < 0; });
    7 
    8     cin.get();
    9 }
  • 两个容器归并排序
     1 //用于归并的字符串模板
     2 //template<class T>
     3 //bool greater(T t1, T t2)
     4 //{
     5 //    return t1 < t2;
     6 //}
     7 
     8 //也可以用仿函数
     9 template<class T>
    10 class greater
    11 {
    12 public:
    13     bool operator ()(T t1, T t2)
    14     {
    15         return t1 < t2;
    16     }
    17 };
    18 //两个数组归并排序
    19 void main5()
    20 {
    21     int a[10] = { 1,9,2,8,3,7,4,6,5,0 };
    22     int b[5]{ 1, 9, 2, 8, 3 };
    23     int c[15]{ 0 };
    24     //从大到小排序
    25     sort(a, a + 10, [](int a, int b) {return a > b; });
    26     sort(b, b + 5, [](int a, int b) {return a > b; });
    27     //通过lambda
    28     //merge(a, a + 10, b, b + 5, c, [](int a, int b) {return a > b; });
    29 
    30     //通过仿函数
    31     merge(a, a + 10, b, b + 5, c, greater<int>());
    32     for (auto i : c)
    33     {
    34         cout << i << endl;
    35     }
    36     cin.get();
    37 }
  • 判断是否有连续相等区间
     1 //判断是否是包含关系(要在realease下执行) 必须要连续相等
     2 void main6()
     3 {
     4     int a[10] = { 1,9,2,8,3,7,4,6,5,0 };
     5     int b[5] = { 1,9,5,8,7 };
     6     bool check = includes(a, a + 10,  b, b + 4);
     7     if (check)
     8     {
     9         cout << "包含" << endl;
    10     }
    11     else
    12     {
    13         cout << "不包含" << endl;
    14     }
    15 
    16     cin.get();
    17 }
  • 求两个容器交并集
     1 //求并集
     2 void main7()
     3 {
     4     int a[10] = { 1,9,2,8,3,7,4,6,5,0 };
     5     int b[5] = { 1,99,5,8,7 };
     6     //这种操作重复数据会被省略(第一个不重复的后面会被保留)
     7     /*set_union(a, a + 10, b, b + 5, ostream_iterator<int>(cout, " "));*/
     8     //这种操作会都保留
     9     int c[15]{ 0 };
    10     set_union(a, a + 10, b, b + 5, c);
    11     for (auto i : c)
    12     {
    13         cout << i << endl;
    14     }
    15     cin.get();
    16 }
  • 把一个数组转化成一个堆
     1 //把一个数组转成一个堆
     2 void main8()
     3 {
     4     int a[10]{ 4,5,6,7,8,10,1,9,2,0 };
     5 
     6     //创建一个堆(创建一个最小堆:顶部值最小)
     7     make_heap(a, a + 10, [](int a, int b)->bool {return a > b; });
     8     for (auto i : a)
     9     {
    10         cout << i << endl;
    11     }
    12     cout << endl << endl;
    13 
    14     //将第一个最大元素移动到最后,同时将剩下的元素重新构造成一个新的heap。
    15     /*pop_heap(a, a + 10);
    16     for (auto i : a)
    17     {
    18         cout << i << endl;
    19     }
    20     cout << endl << endl;*/
    21 
    22     //堆排序(最小堆sort_heap是从大到小排序,最大堆sort_heap是从小到大排序)
    23     sort_heap(a, a + 10,[](int a, int b)->bool {return a > b; });
    24     for (auto i : a)
    25     {
    26         cout << i << endl;
    27     }
    28     cout << endl << endl;
    29     cin.get();
    30 }
  • 内部区间归并排序(需要有两个个排序好子列)
     1 //内部区间归并排序(需要有两个个排序好子列)
     2 void main9()
     3 {
     4     int a[10] = { 2,4,6,8,10,1,3,5,7,9 };
     5     //inplace_merge(a, a + 5, a + 10);
     6     int b[10] = { 10,8,6,4,2,9,7,5,3,1 };
     7     inplace_merge(b, b + 5, b + 10, [](int a, int b)->bool {return a > b; });
     8     for (auto i : b)
     9     {
    10         cout << i << endl;
    11     }
    12     cin.get();
    13 }
  • 判断容器是否有序
     1 //判断是否有序
     2 void main10()
     3 {
     4     int a[10] = { 1,2,3,4,5,6,7,8,9 };
     5     if (is_sorted(a, a + 10))
     6     {
     7         cout << "有序" << endl;
     8     }
     9     cin.get();
    10 }
  • 取出第n大的数据并排好序
     1 //取出第n大数据并排好序
     2 void main11()
     3 {
     4     int a[10] = { 99,2,3,4,5,6,7,8,9 };
     5     nth_element(a, a + 3, a + 10);
     6     for (auto i : a)
     7     {
     8         cout << i << endl;
     9     }
    10     cin.get();
    11 }
  • 求出数组中第一个大于(大于等于)某个数的数据
     1 //求出数组中第一个大于(大于等于)某个数的数据
     2 void main12()
     3 {
     4     //需要有序
     5     int a[10] = { 1,2,3,4,5,6,7,9,20};
     6     //寻找数组中第一个大于等于的数据
     7     int *p = lower_bound(a, a + 10, 8);
     8     cout << "last:" << *p << endl;
     9 
    10     //寻找第一个大于的数据
    11     int *p2 = upper_bound(a, a + 10, 5);
    12     cout << "last:" << *p2 << endl;
    13 
    14     for (auto i : a)
    15     {
    16         cout << i << endl;
    17     }
    18     cin.get();
    19 }
  • 处理有序数组中的重复序列,红黑树的每一个结点是一个链表
     1 //处理有序数组中的重复序列,红黑树的每一个结点是一个链表
     2 void main13()
     3 {
     4     int a[10] = { 1,2,3,4,5,27,27,35,39,50 };
     5     pair<int *, int *> range = equal_range(a, a + 10, 27);
     6     cout << *range.first << endl;
     7     cout << *range.second << endl;
     8     for_each(range.first, range.second, [](int x) {cout << x << endl; });
     9     cin.get();
    10 }
  • 求两个容器交集差集,以及各自独有的汇总到一起
     1 //求两个容器交集差集,以及各自独有的汇总到一起
     2 void main()
     3 {
     4     int a[5] = { 1,2,3,4,5 };
     5     int b[5] = { 1,2,6,7,8 };
     6     int c[2] = { 0 };
     7     //求交集
     8     set_intersection(a, a + 5, b, b + 5, c);
     9     //求差集(a中有b中没有的数据存到d里面)
    10     int d[6]{ 0 };
    11     set_difference(a, a + 5, b, b + 5, d);
    12     int e[6]{ 0 };
    13     //将a,b各自独有的汇总到一起
    14     set_symmetric_difference(a, a + 5, b, b + 5, e);
    15     for (auto i : c)
    16     {
    17         cout << i << endl;
    18     }
    19     cin.get();
    20 }
原文地址:https://www.cnblogs.com/xiaochi/p/8648095.html