第九章(13):STL之常用排序算法

简介: 第九章(13):STL之常用排序算法

前情回顾


在上一块石碑中,我学到了,同时下一块石碑也显露出来…


🚄上章地址:第九层(12):STL之常用查找算法


常用排序算法


常用的排序算法有有四种

sort//排序算法,对容器内的元素进行排序
random_shuffle//对于指定范围内的元素进行随机打乱
merge//将两个元素合并放到另一个容器当中
reverse//反转指定区间内的元素


sort


sort是最常用的排序算法,它可以排序任何类型,对于内置类型可以直接排序,对于自定义类型要指定规则,可以利用仿函数或者函数(两者都必须是谓词),也可以利用谓词去改变排序规则,sort的默认规则是从小到大

sort(beg,end,_pred)


beg是要排序区间的开始迭代器,end为结束迭代器,_pred为谓词,可以写,也可以不写,默认从小到大进行排序

使用:


#include<iostream>
using namespace std;
#include<algorithm>
bool _sort(int a, int b)
{
  return a > b;
}
void test1()
{
  int arr[10] = { 0,5,3,4,6,9,2,1,7,8 };
  cout << "排序前:" << endl;
  for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
  {
  cout << arr[i] << " ";
  }
  cout << endl;
  sort(arr, arr+sizeof(arr)/sizeof(int));
  cout << "排序后:" << endl;
  for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
  {
  cout << arr[i] << " ";
  }
  cout << endl;
  sort(arr, arr + sizeof(arr) / sizeof(int),_sort);
  cout << "升序排序后:" << endl;
  for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
  {
  cout << arr[i] << " ";
  }
  cout << endl;
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


random_shuffle


random_shuffle可以去打乱指定范围内的元素,随机打乱,但是又缺点,就和rand一样,它的随机不是完全随机的,在程序第一次运行起来,这个随机就定下来了,所以想让random_shuffle真正随机起来,需要借助时间戳和srand

random_shuffle(beg,end);


beg是要打乱区间的开始迭代器,end为结束迭代器

使用:

2e9b90b2ca334476abebe75bafe6eeaa.png

#include<iostream>
using namespace std;
#include<algorithm>
#include<ctime>
void test1()
{
  srand((unsigned int)time(NULL));
  int arr[10] = { 0,5,3,4,6,9,2,1,7,8 };
  cout << "打乱前:" << endl;
  for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
  {
  cout << arr[i] << " ";
  }
  cout << endl;
  random_shuffle(arr, arr + sizeof(arr) / sizeof(int));
  cout << "打乱后:" << endl;
  for (int i = 0; i < sizeof(arr) / sizeof(int); i++)
  {
  cout << arr[i] << " ";
  }
  cout << endl;
}
int main()
{
  test1();
  return 0;
}

0eacb84100b54626af849e6b562bf92a.png

2d65d23f6d4748949b924e4057485923.png

2e9b90b2ca334476abebe75bafe6eeaa.png


merge


merge的主要作用是合并两个容器,成为一个新的容器,这里要注意,容器内元素必须有序,而且在给新容器放元素之前,要提前开辟好空间并且空间要足够大

merge(beg1,end1,beg2,end2.dest);


beg1是容器1的开始迭代器,end1为容器1的结束迭代器,beg2为容器2的开始迭代器,end2是容器2的结束迭代器,dest为存放容器的开始迭代器

使用:


#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void print(vector<int>& a)
{
  for (auto b = a.begin(); b < a.end(); b++)
  {
  cout << *b << " ";
  }
  cout << endl;
}
void test1()
{
  vector<int> a, b;
  for (int i = 0; i < 10; i++)
  {
  a.push_back(i);
  b.push_back(i + 1);
  }
  print(a);
  print(b);
  vector<int> c;
  c.resize(a.size() + b.size());
  merge(a.begin(), a.end(), b.begin(), b.end(), c.begin());
  print(c);
}
int main()
{
  test1();
  return 0;
}

4cebaac233b3433da32a72337a77fc60.png


可以发现是直接排序好的,可以用于归并排序归并部分


reverse


reverse的作用是逆序容器内的元素

reverse(beg,end);


beg是要逆序区间的开始迭代器,end为结束迭代器

使用:


#include<iostream>
using namespace std;
#include<algorithm>
#include<vector>
void print(vector<int>& a)
{
  for (auto b = a.begin(); b < a.end(); b++)
  {
  cout << *b << " ";
  }
  cout << endl;
}
void test1()
{
  vector<int> a;
  for (int i = 0; i < 10; i++)
  {
  a.push_back(i);
  }
  cout << "逆序前" << endl;
  print(a);
  reverse(a.begin(), a.end());
  cout << "逆序后" << endl;
  print(a);
}
int main()
{
  test1();
  return 0;
}

6de278e6d6694ce5bb08e7e842b7e74b.png


下一座石碑


这座石碑倒下了,露出了下一座石碑…


😘预知后事如何,关注新专栏,和我一起征服C++这座巨塔

🚀专栏:C++爬塔日记

🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉


相关文章
|
12月前
|
算法 搜索推荐 C++
【C++STL基础入门】vector运算和遍历、排序、乱序算法
【C++STL基础入门】vector运算和遍历、排序、乱序算法
178 0
|
4月前
|
算法 前端开发 Linux
【常用技巧】C++ STL容器操作:6种常用场景算法
STL在Linux C++中使用的非常普遍,掌握并合适的使用各种容器至关重要!
77 10
|
3月前
|
算法 C++
STL算法大全
以上只是一部分STL算法的简单概述,每一个算法都有其特定的使用场景和规则,具体使用时需要参考相关文档或者教程进行深入理解和学习。
28 0
|
4月前
|
算法 搜索推荐 C++
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
|
5月前
|
算法 C++ 容器
黑马c++ STL常用算法 笔记(5) 常用算术生成算法
黑马c++ STL常用算法 笔记(5) 常用算术生成算法
|
5月前
|
算法 C++ 容器
黑马c++ STL常用算法 笔记(4) 常用拷贝和替换算法
黑马c++ STL常用算法 笔记(4) 常用拷贝和替换算法
|
5月前
|
存储 算法 搜索推荐
黑马c++ STL常用算法 笔记(3) 排序算法
黑马c++ STL常用算法 笔记(3) 排序算法
|
5月前
|
算法 C++
黑马c++ STL常用算法 笔记(2) 查找算法
黑马c++ STL常用算法 笔记(2) 查找算法
|
5月前
|
算法 C++
c++算法学习笔记 (21) STL
c++算法学习笔记 (21) STL
|
5月前
|
算法 C++ 容器
黑马c++ STL常用算法 笔记(6) 常用集合算法
黑马c++ STL常用算法 笔记(6) 常用集合算法