c++用同一个函数名对n个数据进行从小到大排序
时间: 2024-06-12 13:10:12 浏览: 16
可以使用函数模板来实现对n个数据进行从小到大排序,具体实现如下:
```c++
template <typename T>
void sort(T arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
```
使用时,只需要将需要排序的数组和数组长度传入即可,例如:
```c++
int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
sort(arr, 10);
```
排序后,数组arr中的元素就会从小到大排列。
相关问题
c++用同一个函数名对n(n<=10)个数据进行从小到大排序,数据类型可以是整数、浮点数,用函数重载实现
好的,我明白了。你的问题是想要实现一个函数,可以对一个包含 n 个数据的数组从小到大进行排序,数据类型可以是整数或者浮点数,对吧?
那么可以使用函数重载的方式,创建两个函数,一个针对整数数组,一个针对浮点数数组,分别进行排序。
以下是一个参考代码,帮助你实现该功能:
```C++
#include <iostream>
#include <algorithm>
using namespace std;
/* 对整数数组进行排序 */
void sort(int arr[], int n){
sort(arr, arr + n);
}
/* 对浮点数数组进行排序 */
void sort(float arr[], int n){
sort(arr, arr + n);
}
int main(){
int arr1[] = {3, 2, 5, 1, 4};
sort(arr1, 5);
float arr2[] = {3.1, 2.2, 5.1, 1.1, 4.3};
sort(arr2, 5);
return 0;
}
```
以上代码中使用了 C++ STL 中的 sort() 函数,对数组进行排序。使用方式都是非常类似的,只需要传入对应的数组和数组长度即可。
希望这个答案对你有所帮助,如有疑问,可以继续追问。
使用c++语言用同一个函数名对n(n<=10)个数据进行从小到大排序,数据类型可以是整数、浮点数,用函数重载实现
可以使用函数重载的方法,编写多个函数名相同但参数不同的函数来实现对不同数据类型的排序,下面是使用C语言实现对n(n<=10)个整数和浮点数从小到大排序的示例代码:
```
#include <stdio.h>
// 对整数数组从小到大排序
void sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 对浮点数数组从小到大排序
void sort(float arr[], int n) {
int i, j;
float temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int i;
int int_arr[5] = {3, 1, 4, 2, 5};
float float_arr[5] = {3.1, 1.2, 4.3, 2.4, 5.5};
// 对整数数组进行排序
sort(int_arr, 5);
printf("sorted int array:");
for (i = 0; i < 5; i++) {
printf("%d ", int_arr[i]);
}
printf("\n");
// 对浮点数数组进行排序
sort(float_arr, 5);
printf("sorted float array:");
for (i = 0; i < 5; i++) {
printf("%.1f ", float_arr[i]);
}
printf("\n");
return 0;
}
```
运行结果:
```
sorted int array:1 2 3 4 5
sorted float array:1.2 2.4 3.1 4.3 5.5
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)