函数模板的使用,对数字,字符的排序,
2014-10-15 13:27
288 查看
#include <iostream>
template <typename T>
void sort(T *a,T n){
T temp;
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<n;i++){
printf("%d\n",a[i]);
}
}
template <typename T1>
void sortchar(T1 *a,int n){
printf("sortchar\n");
char temp;
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(strcmp( &a[j],&a[j+1]) >0 ){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<n;i++){
printf("%c",a[i]);
}
}
int main(int argc,
const char * argv[])
{
int a[10]={2,3,4,11,5,7,8,6,9,0};
sort(a,10);
printf("-------\n");
char b[]={'d','b','c'};
sortchar(b,3);
printf("_______");
char y = 'y';
char z = 'z';
if(strcmp(&y,&z)>0){
printf("%c",y);
}else{
printf("%c",z);
}
getchar();
return 0;
}
template <typename T>
void sort(T *a,T n){
T temp;
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<n;i++){
printf("%d\n",a[i]);
}
}
template <typename T1>
void sortchar(T1 *a,int n){
printf("sortchar\n");
char temp;
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(strcmp( &a[j],&a[j+1]) >0 ){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(int i=0;i<n;i++){
printf("%c",a[i]);
}
}
int main(int argc,
const char * argv[])
{
int a[10]={2,3,4,11,5,7,8,6,9,0};
sort(a,10);
printf("-------\n");
char b[]={'d','b','c'};
sortchar(b,3);
printf("_______");
char y = 'y';
char z = 'z';
if(strcmp(&y,&z)>0){
printf("%c",y);
}else{
printf("%c",z);
}
getchar();
return 0;
}
相关文章推荐
- (使用STL自带的排序函数进行编程7.3.14)UVA 10062 Tell me the frequencies!(求一个字符出现的次数(包括字母和数字)&&按出现的次数降序)
- 字符类型数据按数字大小排序
- 面试题之金山(函数练习2)_字符排序(字母、数字及其它字符)ParseString
- List的排序。在一个list里面放入"1","2"..."10","11"...,"99999","100000",使用字符串大小的顺序与解析成的数字的大小进行排序
- 华为:现有一串字符串,请对字符串中的数字字符按照升序排序,其他非数字字符存储位置保持不变,将结果保存至output数组中
- oracle中混合数字与字符的字符串排序
- ListView 日期,数字,字符排序
- 先数字后字符的排序 PATINDEX 并且要支持简体 繁体 最好直接在mssql里实现
- php各种编码集详解和以及在什么情况下进行使用 发布:mdxy-dxy 字体:[增加 减小] 类型:转载 字符是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字等。 字符集是多个字符的集
- shell使用正则表达式来验证数字字符
- 2012华为编程比赛之字符串数字字符排序
- 二者都是将 byte[] 转换成字符串,一个区别在于 Convert.ToBase64String 使用 base 64 数字编码,所以它生成的全部是 ASCII 字符。
- 自定义函数模板和泛型排序函数的使用
- javaScript parseInt字符转化为数字函数使用小结
- 关于字符和数字类型的索引,Oracle如何实现内部自动转换以及索引使用的验证测试
- 抛砖引玉&#8212;&#8212;实现LISTVIEW点击COLUMN排序的代码,可实现按时间,字符,数字
- javaScript parseInt字符转化为数字函数使用小结
- 使用alt键和数字小键盘输入特殊字符
- oracle"字符串型数字"排序问题
- ibatis 动态列缓存问题;oracle行转列;oracle使用数字或特殊字符作列的别名