如何使用 std::sort 在 C++ 中对数组进行排序
在编程语言中,排序是一种基本功能,它应用于数据,将这些数据是升序还是降序排列。在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输出结果#include 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
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短