C++ ----模版
来源:互联网 发布:webcrack软件下载 编辑:程序博客网 时间:2024/05/21 06:57
<span style="font-size:24px;">#include <stdio.h>#include <iostream>#include <string.h>using namespace std;template <typename T>void Swap(T& a,T& b){T t = a;a = b;b = t;}int main(){int a = 1;int b = 2;cout << "---------------自动类型推倒调用--------------------" << endl;Swap(a,b);cout << "a = " << a << endl;cout << "b = " << b << endl;float fa = 3;float fb = 4;Swap(fa,fb);cout << "fa = " << fa << endl;cout << "fb = " << fb << endl;char ca = 'a';char cb = 'b';cout << "---------------具体类型显示调用--------------------" << endl;Swap<char>(ca,cb);cout << "ca = " << ca << endl;cout << "cb = " << cb << endl; return 0;}</span>
#include <stdio.h>#include <iostream>#include <string.h>using namespace std;template <typename T>void Swap(T& a,T& b){T t = a;a = b;b = t;}template <typename T>void SelectSort(T array[],int length){for(int i=0;i<length;i++){T min = array[i];int index = i;for(int j=i+1;j<length;j++){if(array[j] < min){min = array[j];index = j;}}Swap(array[i],array[index]);}}template <typename T>void Display(T array[],int length){for(int i=0;i<length;i++){cout << array[i] << endl;}}int main(){cout << "----------整形序列排序----------" << endl;int array[] = {4,2,5,1,7,6};int len = sizeof(array)/sizeof(array[0]);SelectSort<int>(array,len);Display(array,len);cout << "----------字符序列排序----------" << endl;char ca[] = {'d','b','f','a','c'};len = sizeof(ca)/sizeof(ca[0]);SelectSort<char>(ca,len);Display(ca,len);return 0;}
1 0
- c语言图形模版
- 用C也能写模版
- 模版文件 target.c
- 邻接表模版c+
- Template模版实例(C++)
- 【C++】模版矩阵类
- C++template--函数模版
- 【c++】函数模版
- c++模版
- 【C++】认识模版函数
- C/C++模版
- C++——模版:函数模版
- C++——模版:类模版
- [C++] 模版双向鍊表
- 高精度运算模版C语言
- 简单理解 C ++ 中的模版
- [C++]模版特例化和模版偏特化
- 【C/C++】C++模版基础知识点
- maven的环境搭建以及maven搭建web项目
- android 无线调试
- 牛客网 | 链式A+B
- android——ObjectAnimator动画
- android——ObjectAnimator动画(一)
- C++ ----模版
- android——背景颜色渐变(梯度变化)
- Hadoop:MapReduce详解
- eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。
- Eclipse 常用快捷键 (动画讲解)(转载)
- android——fragment详解
- <算法导论>第二章 2.3设计算法
- android——生成或者下载的图片在相册中找不到
- Android:弄明白Touch事件分发机制