#includeusing namespace std; #include #include void mySort(int*& pa,int n){ const int max = n; pa = new int[max]; if(pa != 0){ cout<<"sucess"; } for (int i = 0;i >temp; pa[i] = temp; } sort(pa,pa+n); } int main() { int i,n; int * a = 0; cin >> n; mySort(a, n); for (i=0;i 要求:
(1)在函数中创建动态数组保存数据,
(2)在函数中排序,
(3)在主程序中输出结果。
一些日记:第一次使用 头文件,含有一些算法;
其中sort方法使用时参考:
对于普通数组:sort()函数可以对给定区间所有元素进行排序。它有三个参数sort(begin, end, cmp),其中begin为指向待sort()的数组的第一个元素的指针,end为指向待sort()的数组的最后一个元素的下一个位置的指针(注:类似于左闭右开区间),cmp参数为排序准则,cmp参数可以不写,如果不写的话,默认从小到大进行排序。如果我们想从大到小排序可以将cmp参数写为greater
()就是对int数组进行排序,当然<>中我们也可以写double、long、float等等。如果我们需要按照其他的排序准则,那么就需要我们自己定义一个bool类型的函数来传入。比如我们对一个整型数组进行从大到小排序:
————————————————
版权声明:本文为CSDN博主「AC-fun」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41575507/article/details/105936466



