c++排序函数sort(c++sort排序原理)

## C++ 排序函数 sort()### 简介`std::sort()` 是 C++ 标准库算法库中提供的一个排序函数,用于对指定范围内的元素进行排序。它使用快速排序算法或其变体(如 introsort),平均时间复杂度为 O(N

logN),效率较高。### 使用方法#### 1. 头文件使用 `std::sort()` 函数需要包含 `` 头文件:```cpp #include ```#### 2. 函数原型`std::sort()` 函数有两种原型:-

基本形式:

```cpp template void sort (RandomAccessIterator first, RandomAccessIterator last); ```-

带自定义比较函数的形式:

```cpp template void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); ```#### 3. 参数说明-

`first`, `last`:

迭代器,指向要排序的范围的起始位置和结束位置(不包括)。 -

`comp`:

可选参数,自定义比较函数,用于指定排序规则。### 使用示例#### 1. 对数组排序```cpp #include #include int main() {int arr[] = {3, 1, 4, 2, 5};int n = sizeof(arr) / sizeof(arr[0]);// 对数组 arr 进行升序排序std::sort(arr, arr + n);// 输出排序后的数组for (int i = 0; i < n; i++) {std::cout << arr[i] << " ";}std::cout << std::endl; // 输出: 1 2 3 4 5return 0; } ```#### 2. 对向量排序```cpp #include #include #include int main() {std::vector v = {3, 1, 4, 2, 5};// 对向量 v 进行降序排序std::sort(v.begin(), v.end(), std::greater());// 输出排序后的向量for (int x : v) {std::cout << x << " ";}std::cout << std::endl; // 输出: 5 4 3 2 1return 0; } ```#### 3. 自定义比较函数```cpp #include #include struct Point {int x, y; };// 自定义比较函数,按 x 坐标升序排序 bool compareByX(const Point& a, const Point& b) {return a.x < b.x; }int main() {Point points[] = {{3, 2}, {1, 4}, {2, 3}};int n = sizeof(points) / sizeof(points[0]);// 使用自定义比较函数对结构体数组排序std::sort(points, points + n, compareByX);// 输出排序后的结构体数组for (int i = 0; i < n; i++) {std::cout << "(" << points[i].x << ", " << points[i].y << ") ";}std::cout << std::endl; // 输出: (1, 4) (2, 3) (3, 2)return 0; } ```### 总结`std::sort()` 是 C++ 中一个非常实用的排序函数,可以方便地对各种容器和数组进行排序。熟练掌握其用法,可以帮助我们更高效地编写代码。

C++ 排序函数 sort()

简介`std::sort()` 是 C++ 标准库算法库中提供的一个排序函数,用于对指定范围内的元素进行排序。它使用快速排序算法或其变体(如 introsort),平均时间复杂度为 O(N*logN),效率较高。

使用方法

1. 头文件使用 `std::sort()` 函数需要包含 `` 头文件:```cpp

include ```

2. 函数原型`std::sort()` 函数有两种原型:- **基本形式:**```cpp template void sort (RandomAccessIterator first, RandomAccessIterator last); ```- **带自定义比较函数的形式:**```cpp template void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp); ```

3. 参数说明- **`first`, `last`:** 迭代器,指向要排序的范围的起始位置和结束位置(不包括)。 - **`comp`:** 可选参数,自定义比较函数,用于指定排序规则。

使用示例

1. 对数组排序```cpp

include

include int main() {int arr[] = {3, 1, 4, 2, 5};int n = sizeof(arr) / sizeof(arr[0]);// 对数组 arr 进行升序排序std::sort(arr, arr + n);// 输出排序后的数组for (int i = 0; i < n; i++) {std::cout << arr[i] << " ";}std::cout << std::endl; // 输出: 1 2 3 4 5return 0; } ```

2. 对向量排序```cpp

include

include

include int main() {std::vector v = {3, 1, 4, 2, 5};// 对向量 v 进行降序排序std::sort(v.begin(), v.end(), std::greater());// 输出排序后的向量for (int x : v) {std::cout << x << " ";}std::cout << std::endl; // 输出: 5 4 3 2 1return 0; } ```

3. 自定义比较函数```cpp

include

include struct Point {int x, y; };// 自定义比较函数,按 x 坐标升序排序 bool compareByX(const Point& a, const Point& b) {return a.x < b.x; }int main() {Point points[] = {{3, 2}, {1, 4}, {2, 3}};int n = sizeof(points) / sizeof(points[0]);// 使用自定义比较函数对结构体数组排序std::sort(points, points + n, compareByX);// 输出排序后的结构体数组for (int i = 0; i < n; i++) {std::cout << "(" << points[i].x << ", " << points[i].y << ") ";}std::cout << std::endl; // 输出: (1, 4) (2, 3) (3, 2)return 0; } ```

总结`std::sort()` 是 C++ 中一个非常实用的排序函数,可以方便地对各种容器和数组进行排序。熟练掌握其用法,可以帮助我们更高效地编写代码。

标签列表