c ++ function template basics

navigation

1. function template and precautions
2. write the sort function template
3. the difference between a normal function and function template
4. Call an ordinary function and function template
limitations 5. The template
----------- ------------------------
1. function template
syntax: template <typename T>
typename class may also be used
wherein T is a general type, see the following two examples
Example 1:

#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;
}

//要写两个函数再调用
void test01()
{
	int a = 10,b = 20;
	double c = 1.1,d = 2.2;
	swapint(a,b);
	swapDouble(c,d);
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;
	cout<<"c = "<<c<<endl;
	cout<<"d = "<<d<<endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

The values ​​for the two different types of interchangeable us to write two functions, if the string type swap it, you have to write a function, this time we can use the template

Example 2:

#include <iostream>
using namespace std;
//函数模板
template<typename T>
void swapT(T &a,T &b)
{
	T temp = a;
	a = b;
	b = temp;
}
void test02()
{
	int a = 10,b = 20;
	double c = 1.1,d = 2.2;
	//1.自动类型推导
	swapT(a,b);
	//2.显示指定类型
	swapT<double>(c,d);
	cout<<"a = "<<a<<endl;
	cout<<"b = "<<b<<endl;
	cout<<"c = "<<c<<endl;
	cout<<"d = "<<d<<endl;
}

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

Here we write a template can be applied, there are two ways to apply the template
1. Automatic type inference - (the swap (A, B))
2. for a specific type - (swap <int> (a , b))

Notes
1. Automatic derivation type, we must derive consistent data types before you can use
2 template must determine T data types before you can use

Example 1 :( Note 1)

#include <iostream>
using namespace std;

//函数模板
template<typename T>
void swapT(T &a,T &b)
{
	T temp = a;
	a = b;
	b = temp;
}

//注意事项
void test()
{
	int a = 10;
	int b = 20;
	char c = 'a';
	swapT(a,b);  //正确
	//swapT(a,c);	//错误
}

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

Example 2 :( Note 2)

#include <iostream>
using namespace std;

//函数模板
template<typename T>
void func()
{
	cout<<"func()调用"<<endl;
}

//注意事项
void test()
{
	func<void>();  //这里必须加数据类型,不然无法调用
}

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

-----------------------------------
2. Use template to write the sort function

#include <iostream>
using namespace std;

//对不同数据类型数组进行排序
//从大到小,算法为选择排序
//类型char型,int型
template<class T>
//交换数值模板
void myswap(T &a,T &b)
{
	T temp = a;
	a = b;
	b = temp;
}

//排序算法模板
template<class T>
void mysort(T arr[],int len)
{
	for(int i=0;i<len;i++)
	{
		int max = i;
		for(int j=i+1;j<len;j++)
		{
			if(arr[j]>arr[max])
			{
				max = j;
			}
		}
		if(max != i)
		{
			//交换max与i下标
			myswap(arr[i],arr[max]);
		}
	}
}

//提供打印数组模板
template<class T>
void myprintArr(T arr[],int len)
{
	for(int i=0;i<len;i++)
	{
		cout<<arr[i]<<" ";
	}
}


void test01()
{
	//测试char数组
	char charArr[] = "badcfe";
	int len = sizeof(charArr)/sizeof(char);
	mysort(charArr,len);
	myprintArr(charArr,len);
}

void test02()
{
	//测试int数组
	int intArr[] = {7,2,5,4,3,1};
	int len = sizeof(intArr)/sizeof(int);
	mysort(intArr,len);
	myprintArr(intArr,len);
}

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

Some effort will be written using a template

-----------------------------------
3. The difference between a normal function and function template

1. ordinary function call may occur implicit type conversions
2. template automatic type inference function, an implicit type conversion may not occur
3. The function template for a specific type, an implicit type conversion may occur

Example:

#include <iostream>
using namespace std;

//普通函数
int func(int a,int b)
{
	return a+b;
}

//调用模板
template<class T>
T fun(T a, T b)
{
	return a+b;
}

void test02()
{
	int a =10,b =20;
	char c = 'a';
	//1.调用普通函数 可以会发生隐式类型转换
	cout<<func(a,b)<<endl;  
	cout<<func(a,c)<<endl;
	//2.用函数模板自动类型推导,不发生隐式类型转换
	cout<<fun(a,b)<<endl;
	//3.函数模板用显示指定类型,可以发生隐式转换
	cout<<fun<int>(a,c)<<endl;   //这里要提供指定类型
}

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

-----------------------------------
4. Call the rules of ordinary function and function template

1. If the function template ordinary function can be called, ordinary function call priority
2. template parameter list can be empty, forced call function template
3. The function template may be overloaded
4. If the function can produce a better match the template, the limited call a function template

Invoke Rule 1

#include <iostream>
using namespace std;

//普通函数
void myprint(int &a,int &b)
{
	cout<<"调用普通函数"<<endl;
}

//函数模板
template<class T>
void myprint(T &a,T &b)
{
	cout<<"函数模板调用"<<endl;
}

void test()
{
	int a=10,b=20;
	myprint(a,b);
}

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

The result is first call an ordinary function

Invoke Rule 2

#include <iostream>
using namespace std;

//普通函数
void myprint(int &a,int &b);
//{
//	cout<<"调用普通函数"<<endl;
//}

//函数模板
template<class T>
void myprint(T &a,T &b)
{
	cout<<"函数模板调用"<<endl;
}

void test()
{
	int a=10,b=20;
	myprint<>(a,b);  //使用空模板参数列表强制调用
}

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

Invoke Rule 3

If the function template overloading occurs, in which different parameters can be invoked

Invoke Rule 4

#include <iostream>
using namespace std;

//普通函数
void myprint(int a,int b)
{
	cout<<"调用普通函数"<<endl;
}

//函数模板	
template<class T>
void myprint(T a,T b)
{
	cout<<"函数模板调用"<<endl;
}

void test()
{
	char a = 'a',b = 'c';
	myprint(a,b);   
}

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

Here we will call a function template

-----------------------------------
limitations template 5.
If only some of the common types of single afferent data is possible, if the incoming type or an array, it is necessary to realize a method of using a specific

Example:

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

//写一个类
class Person
{
public:
	Person(string name,int age)
	{
		this->name = name;
		this->age = age;
	}

	string name;
	int age;
};

//模板 用于单个普通的类型,数组或者自定义就不行,应该用具体方法实现
template<class T>
bool compare(T a,T b)
{
	if(a==b)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//写一个person类的模板,里面传入的也要改为对应的类型
template<> bool compare(Person p1,Person p2)
{
	if(p1.name == p2.name && p1.age == p2.age)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void test()
{
	int a=10,b =10;
	bool ret = compare(a,b);
	//测试普通的类型
	if(ret)
	{
		cout<<"a == b"<<endl;
	}
	else
	{
		cout<<"a != b"<<endl;
	}

	//测试Person类 
	Person p1("小明",12);
	Person p2("小明",12);
	bool ret1 = compare(p1,p2);
	if(ret1)
	{
		cout<<"p1 == p2"<<endl;
	}
	else
	{
		cout<<"p1 != p2"<<endl;
	}
}

int main()
{
	test();
	system("pause");
	return 0;
}
Released eight original articles · won praise 2 · Views 144

Guess you like

Origin blog.csdn.net/cl939974883/article/details/104076235