C++ sort排序(从小到大,从大到小)
使用 sort() 函数,需要先引入头文件:algorithm。 #include <algorithm>。
sort() 函数的模板:
/**
* @brief Sort the elements of a sequence.
* @ingroup sorting_algorithms
* @param __first An iterator.
* @param __last Another iterator.
* @return Nothing.
*
* Sorts the elements in the range @p [__first,__last) in ascending order,
* such that for each iterator @e i in the range @p [__first,__last-1),
* *(i+1)<*i is false.
*
* The relative ordering of equivalent elements is not preserved, use
* @p stable_sort() if this is needed.
*/
template<typename _RandomAccessIterator>
inline void sort(_RandomAccessIterator __first, _RandomAccessIterator __last);
/**
* @brief Sort the elements of a sequence using a predicate for comparison.
* @ingroup sorting_algorithms
* @param __first An iterator.
* @param __last Another iterator.
* @param __comp A comparison functor.
* @return Nothing.
*
* Sorts the elements in the range @p [__first,__last) in ascending order,
* such that @p __comp(*(i+1),*i) is false for every iterator @e i in the
* range @p [__first,__last-1).
*
* The relative ordering of equivalent elements is not preserved, use
* @p stable_sort() if this is needed.
*/
template<typename _RandomAccessIterator, typename _Compare>
inline void sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp);
sort() 函数接受 个参数。
__first:起始地址。__last:结束地址。__comp:排序方法。
注意:区间是左闭右开 。关于参数的含义,有专业的叫法,上面也给出了英文注释。这里为了方便理解,所以这样叫。
从小到大
默认是从小到大,直接用。
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
const int N = 20;
int a[N];
vector<int> v_a(N);
void _printf(int a[], int n)
{
for ( int i = 0; i < n; i ++ ) printf("%d ", a[i]);
puts("");
}
void _printf(vector<int> a, int n)
{
for ( int i = 0; i < n; i ++ ) printf("%d ", a[i]);
puts("");
}
int main()
{
srand(time(0));
for ( int i = 0; i < N; i ++ ) a[i] = v_a[i] = rand() % 100;
puts("排序前:");
_printf(a, N);
_printf(v_a, N);
int l = 0, r = N;
sort(a + l, a + r);
sort(v_a.begin() + l, v_a.begin() + r);
// 或者
sort(v_a.begin() + l, v_a.end());
puts("排序后:");
_printf(a, N);
_printf(v_a, N);
return 0;
}
从大到小
很灵活,可以使用greater<int>(),或者自己写排序方法。排序方法可以写全局的,也可以写 Lambda。给对象排序还可以重载 < 运算符。
排基本类型
vector和普通数组使用方式大同小异,这里就只写普通数组了。
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
const int N = 20;
int a[N];
void _printf(int a[], int n)
{
for ( int i = 0; i < n; i ++ ) printf("%d ", a[i]);
puts("");
}
// 其实就是在问,a要放在b的前面吗?
// 如果排对象,注意加引用,免得影响效率。
bool comp(int a, int b)
{
return a > b;
}
int main()
{
srand(time(0));
for ( int i = 0; i < N; i ++ ) a[i] = rand() % 100;
puts("排序前:");
_printf(a, N);
sort(a, a + N, greater<int>());
sort(a, a + N, comp);
// lambda
sort(a, a + N, [](int a, int b) { return a > b; });
puts("排序后:");
_printf(a, N);
return 0;
}
排对象
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
const int N = 10;
struct Obj
{
int x, y;
// 按照y的大小,从大到小排序,从小到大排序同理
// 也可以写全局的排序方法
bool operator < (const Obj &o)
{
return this->y > o.y;
// 从小到大
// return this->y < o.y;
}
}a[N];
void _printf(Obj a[], int n)
{
for ( int i = 0; i < n; i ++ )
printf("(%02d, %02d) ", a[i].x, a[i].y);
puts("");
}
int main()
{
srand(time(0));
for ( int i = 0; i < N; i ++ )
a[i].x = rand() % 30, a[i].y = rand() % 100;
puts("排序前:");
_printf(a, N);
sort(a, a + N);
puts("排序后:");
_printf(a, N);
return 0;
}
