在 C++ 中使用 std::sort 来对数组进行排序
在编程语言中,排序是一种基本函数,用于对数据进行排序,将这些数据排列为升序或降序数据。在 C++ 程序中,有一个函数 std::sort() 用于对数组进行排序。
sort(start address, end address)
在此处,
Start address => The first address of the element. Last address => The address of the next contiguous location of the last element of the array.
示例代码
#include <iostream>
#include <algorithm>
using namespace std;
void display(int a[]) {
for(int i = 0; i < 5; ++i)
cout << a[i] << " ";
}
int main() {
int a[5]= {4, 2, 7, 9, 6};
cout << "\n The array before sorting is : ";
display(a);
sort(a, a+5);
cout << "\n\n The array after sorting is : ";
display(a);
return 0;
}输出
The array before sorting is : 4 2 7 9 6 The array after sorting is : 2 4 6 7 9
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP