用两个函数模板来实现整型、实型、字符型的排序和输出

这篇博客介绍了如何通过设计函数模板selectSort和printArray,来实现对整型、实型和字符型数据的排序及数组打印。博主在代码中详细注释了实现步骤和关键点。
摘要由CSDN通过智能技术生成

分别设计一个支持整型、实型、字符型的选择排序的函数模板 selectSort,以及一个打 印整型、实型、字符型数组的函数模板 printArray,在主程序调用。

要点写在注释内了

#include<iostream>
using namespace std;

template < typename T>
void  selectSort(T a[ ], int n);//函数模板的声明包括着template
template < typename T>
void   printArray(T a[ ], int n);//虚拟类型同样可以定义为T,你知道为什么吗?

int main()
{ int intarr[10]={20,18, 7, 19, 9, 8, 2, 12,10,1};
 float floatarr[12]={9.1, 1.9, 8.2, 7.3, 6.4, 3.7, 5.5, 4.6, 2.8, 5.8, 4.6, 2.9};
 char chararr[8]= {'C', 'd', 'e','x','B','D','A', 'a' };

 selectSort(intarr,10);
 cout << "the sorted float array:";
 printArray(intarr,10);
 selectSort(floatarr,12);
 cout << "the sorted float array:";
 printArray(floatarr,12);
 selectSort(chararr,8);
 cout << "the sorted float array:";
 printArray(chararr,8);
 return 0;
}
template < typename T>
void  selectSort(T a
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值