C++ function template (1) Basic syntax of function template, precautions for function template, and case list of function template—array sorting

C++ function template (1)

Yesterday, I finally finished learning the function template chapter, and sorted out a very good case of function template. Today, I started to sort out the notes that I took this week in order. There are a lot of memorizations, so I will sort it out today. The basic syntax of the function template, the precautions of the function template, and the function template case-array sorting, these three parts.

01. The concept of template-template is
just a framework with strong versatility, but it cannot be used directly.
Role: to establish a general function, the function return value type and formal parameter type, can not be specified specifically, represented by a virtual type.
Purpose: Improve reusability and parameterize types.
02, the basic syntax of function templates

template<typename T>

template—Declares to create a template.
typename—Indicates that the symbol behind it is a data type, which can be replaced by class.
T—General data type, the name can be replaced, usually in uppercase letters.
Specific test case:

#include<iostream>
using namespace std;

//函数模板
//交换两个整型函数
void swapInt(int &a,int &b)
{
    
    
	int temp =a;
	a=b;
	b=temp;
}

//交换两个浮点型函数
void swapDouble(double &a,double &b)
{
    
    
	double temp=a;
	a=b;
	b=temp;

}

//因为有太多数据类型,一个一个写太过麻烦,所以引出函数模板的应用
template<typename T>//函数模版,后面紧跟一个T,就是先声明,后面如果出现T了先不要报错
void myswap(T &a,T &b)//同样先声明一个函数,但不确定数据类型,用T来代表
{
    
    
	T temp=a;
	a=b;
	b=temp;
}

//写一个利用模板的输出函数
void test03()
{
    
    

	int a=10;
	int b=11;
	//两种输出方式
	//1、自动判断法
	//myswap(a,b);
	//2、提示法
	myswap<int>(a,b);
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;
	
}

//写一个赋值输出函数
void test01()
{
    
    
	int a=11;
	int b=30;
	swapInt(a,b);
	cout<<"交换后a的值"<<a<<endl;
	cout<<"交换后b的值"<<b<<endl;
}


//再写一个浮点型输出函数
void test02()
{
    
    
	double d=3.1415926535;
	double t=3.3333333333;
	swapDouble(d,t);
	cout<<"交换后d的值"<<d<<endl;
	cout<<"交换后t的值"<<t<<endl;
}

int main()
{
    
    
	//test01();
	//test02();
	test03();


system("pause");
return 0;

}

03. Precautions for function template
① Automatic type derivation, the consistent data type T must be derived before it can be used.
②The template must determine the T data type before it can be used.
Specific function test implementation:

#include<iostream>
using namespace std;
//先把上次写的交换的函数给写出来
template<typename T>//正常写函数但之前加上模板的固定格式,,并且注意T代表的是通用相当于可以代表任意的
void myswap(T&a,T&b)
{
    
    
T temp=a;
   a=b;
   b=temp;
}

//自动类型推导输出型,必须推导出一致的数据类型T才可使用
void test01()
{
    
    
	int a=10;
	int b=20;
	char c='c';
	myswap(a,b);
	//myswap(a,c);//错误,因为推导不出一致的数据类型T
	cout<<"a="<<a<<endl;
	cout<<"b="<<b<<endl;
}
//模板必须确定T数据类型,才可以使用
//验证
template<typename T>
void func()
{
    
    
cout<<"func 调用"<<endl;

}

//输出这个函数
void test02()
{
    
    
//func();没有用到T的数据类型,所以没办法调用该函数输出
	func<int>();//确定了T的数据类型(虽然T的类型可以随意定义,因为func函数是一个空的函数)
}

int main()
{
    
    
	test01();
	test02();
system("pause");
return 0;
}

04. Function template case-Array sorting
Realize the general function of sorting arrays:
rule → from small to large
algorithm → select
test → char array, int array
specific code implementation:

#include<iostream>
#include<string>
using namespace std;

//目的:实现通用的对数组进行排序的函数
//规则:从小到大
//算法:选择
//测试:char 数组、int数组
//函数交换模板
template<typename T>
void myswap(T a,T b)
{
    
    
int temp =a;
a=b;
b=temp;
}

//选择排序函数模板
template<typename T>
void mysort(T arr,int len )
{
    
    
for(int i=0; i<len;i++)
{
    
    
//假设i项为最小项
	int min=i;
	for(int j=i+1;j<len;j++)
	{
    
    
	//将第i项与第j项进行比较
		//
		if(arr[min]>arr[j])
	 	{
    
    
		//交换两个数组的下标
		myswap(arr[i],arr[j]);
		}
	}
}

}
//输出函数模板
template<typename T>
void myprint(T arr[],int len )//len一定是int型,如果也写成T则会造成不一致现象无法输出
{
    
    
	for(int i=0;i<len;i++)
	{
    
    
	cout<<arr[i]<<" ";
	}
	cout<<endl;
}

//测试函数
void test01()
{
    
    
	char charArr[]="acbed";
    int len=sizeof(charArr)/sizeof(char);
	mysort(charArr,len);
	myprint(charArr,len);
}

int main()
{
    
    
	test01();

system("pause");
return 0;
}

end-minor problems
that appear 1. Pay attention to the number of double digits
2. The function is declared in advance. Int main is called to write the code
3. Cout output, there is an undefined identifier or the function structure problem
4. Test the writing of the char array: char charArr[ ]=" "
5. Array length calculation: int num=sizeof(charArr)/sizeof(charr)

Okay, let's sort out so much first.

Guess you like

Origin blog.csdn.net/qq_45252077/article/details/107876431