现在的位置: 首页 > 综合 > 正文

C++函数模板实例

2018年05月03日 ⁄ 综合 ⁄ 共 545字 ⁄ 字号 评论关闭

C++函数模板实例

#include <iostream>
using namespace std;

template<class T>											 //定义函数模板
void outputArray(const T*array, int count){
	for (int i=0; i<count;i++)
		cout<<array[i]<<"";
	cout<<endl;

}

int main(){														//定义主函数

	const int A_COUNT=8,B_COUNT=8,C_COUNT=20;
	int a[A_COUNT]={1,2,3,4,5,6,7,8};							//定义整形数组
	double b[B_COUNT]={1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8};		//定义都变了数组
	char c[]="Welcome to see you";								//定义字符数组

	cout<<"a array contains:"<<endl;
	outputArray(a,A_COUNT);										//调用函数模板
	cout<<"b array contains:"<<endl;
	outputArray(b,B_COUNT);										//调用函数模板
	cout<<"c array contains:"<<endl;
	outputArray(c,C_COUNT);										//调用函数模板

	return 0;
}

 

抱歉!评论已关闭.