栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++之模板

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++之模板

1、函数模板 1、函数模板的语法

作用:建立一个通用的函数,其函数返回值类型和形式参数可以不具体制定,用一个虚拟的类型去表示
语法:template
解释:
template ---- 声明创建模板
typename ----表明后面的符号是一种类型
T ----通用的数据类型
示例:

#include
using namespace std;
template
void mySwap(T &a,T &b)
{
	T temp=a;
	a = b;
	b = temp;
}

int main()
{
	int a = 10;
	int b = 20;
	//mySwap(a, b);
	mySwap(a, b);
	cout << a << endl;
	cout << b << endl;
	system("pause");
	return 0;
}
2、函数模板的注意事项

注意事项:

  • 自动类型推导,必须推导出一致的数据类型

  • 模板必须要确定T的数据类型才可以使用

#include
using namespace std;
template
void mySwap(T &a,T &b)
{
	T temp=a;
	a = b;
	b = temp;
}
template
void fun()
{
	cout << "void fun()的调用" << endl;
}
void test()
{
	fun();
}
int main()
{
	int a = 10;
	int b = 20;
	mySwap(a, b);
	char c = 'c';
	mySwap(a, b);
	//mySwap(a, c);
	cout << a << endl;
	cout << b << endl;
	system("pause");
	return 0;
}
3、函数模板的案例-实现排序
#include
using namespace std;
template
void mySwap(T &a,T &b)
{
	T temp=a;
	a = b;
	b = temp;
}
template
void mySort_Max_To_Min(T arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		int max = i;
		for (int j = i + 1; j < len; j++)
		{
			if (arr[max] < arr[j])
			{
				max = j;
			}
		}
		if (max != i)
		{
			mySwap(arr[max], arr[i]);
		}
	}
}
template
void mySort_Min_To_Max(T arr[], int len)
{
	for (int i = 0; i < len; i++)
	{
		int max = i;
		for (int j = i + 1; j < len; j++)
		{
			if (arr[max] > arr[j])
			{
				max = j;
			}
		}
		if (max != i)
		{
			mySwap(arr[max], arr[i]);
		}
	}
}
int main()
{
	int a = 10;
	int b = 20;
	//mySwap(a, b);
	//char c = 'c';
	//mySwap(a, b);
	mySwap(a, c);
	//cout << a << endl;
	//cout << b << endl;
	char charArray[] = "badcfe";
	int arraySize = sizeof(charArray) / sizeof(charArray[0]);
	mySort_Max_To_Min(charArray, arraySize);
	
	mySort_Min_To_Max(charArray, arraySize);
	for (int i = 0; i < arraySize; i++)
	{
		cout << charArray[i];
	}
	cout << endl;
	system("pause");
	return 0;
}
4、普通函数和函数模板的区别
  • 普通函数调用时可以发生自动类型转换(隐式类型转换)
  • 函数模板调用时,自动类型推导,不会发生隐式类型转换
  • 函数模板调用时,显示指定类型,可以发生隐式类型转换
5、普通函数和函数模板的调用规则

1、如果普通函数和函数模板都可以实现,优先调用普通函数
2、可以通过空模板参数列表来强制调用函数模板
3、模板函数也可以发生重载
4、如果函数模板可以产生更好的匹配效果(不要进行隐式类型转换),优先调用函数模板

#include
using namespace std;
void fun(int a, int b)
{
	cout << "普通函数调用:" << endl;
}
template
void fun(T a, T b)
{
	cout << "模板函数调用:" << endl;
}
template
void fun(T a, T b, T c)
{
	cout << "重载模板调用" << endl;
}
int main()
{
	
	//fun(a, b);
	//fun<>(a, b);
	//fun(a, b, 100);
	char a = 'a';
	char c = 'c';
	fun(a, c);
	system("pause");
	return 0;
}
6、函数模板的局限性

对于自定义的数据类型,模板无法正常使用,必须需要重载

#include
using namespace std;
class Person
{
public:
	Person(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
	string name;
	int age;

};
template
bool myCompare(T& a, T& b)
{
	if (a == b)
	{
		return true;
	}
	else
	{
		return false;
	}
}
template<>bool myCompare(Person& p1, Person& p2)
{
	if (p1.name == p2.name&&p1.age==p2.age)
	{
		return true;
	}
	else
	{
		return false;
	}
 }
void test01()
{
	int a = 20;
	int b = 20;
	
	bool ret= myCompare(a, b);
	if (ret)
	{
		cout << "相等" << endl;
	}
	else
	{
		cout << "不相等" << endl;
	}
}
void test02()
{
	Person p1("Tom", 20);
	Person p2("Tom", 20);
	bool ret = myCompare(p1,p2);
	if (ret)
	{
		cout << "相等" << endl;
	}
	else
	{
		cout << "不相等" << endl;
	}

}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}
2、类模板 1、类模板语法

类模板作用:建立一个通用的类,类中的成员数据类型不具体制定,用一个虚拟的类型来代表
语法:template 类

#include
using namespace std;
template
class Person
{
public:
	Person(nameType name, ageType age)
	{
		this->name = name;
		this->age = age;
	}
	nameType name;
	ageType age;

};
void test()
{
	Person p1("孙悟空", 9999);
	cout << "姓名:" << p1.name << " 年龄:" << p1.age << endl;
}
int main()
{
	test();
	system("pause");
	return 0;
}
2、类模板与函数模板区别

1、类模板必须显示指定类型
2、类模板可以有默认参数

#include
using namespace std;
template
class Person
{
public:
	Person(nameType name, ageType age)
	{
		this->name = name;
		this->age = age;
	}
	nameType name;
	ageType age;

};
void test()
{
	Person p1("孙悟空", 9999);//只能指定数据类型
	cout << "姓名:" << p1.name << " 年龄:" << p1.age << endl;
}
void test2()
{
	Person p1("孙悟空", 9999);//有默认数据类型int
	cout << "姓名:" << p1.name << " 年龄:" << p1.age << endl;
}
int main()
{
	test2();
	system("pause");
	return 0;
}
3、类模板中成员函数的创建时机
  • 普通函数的成员函数在一开始就可以创建
  • 类模板的成员函数需要调用时才创建
#include
using namespace std;
class Person1
{
public:
	void fun1()
	{
		cout << "void fun1()调用:" << endl;
	}
};
class Person2
{
public:
	void fun2()
	{
		cout << "void fun2()调用:" << endl;
	}
};
template
class myClass
{
public:
	T obj;
	void f1()
	{
		obj.fun1();
	}
	void f2()
	{
		obj.fun2();
	}

};
int main()
{
	myClassp;
	//p.f1();
	p.f2();

	system("pause");
	return 0;
}
4、类模板对象做函数参数

1、指定参数类型
2、参数模板化
3、整个类模板化

#include
using namespace std;
#include
template

class Person
{
public:
	Person(NameType name,AgeType age)
	{
		this->name = name;
		this->age = age;
	}
	void showPerson()
	{
		cout << "姓名:" << this->name << " 年龄:" << this->age << endl;
	}

	NameType name;
	AgeType age;
};
//1、指定传入类型
void printPerson01(Person&p)
{
	p.showPerson();
}
void test01()
{
	Personp("孙悟空", 999);
	printPerson01(p);
}
//2、参数模板化
template
void printPerson02(Person&p)
{
	p.showPerson();
	cout << "T1的类型:" << typeid(nameType).name() << endl;
	cout << "T2的类型:" << typeid(ageType).name() << endl;//这是一个函数
}
void test02()
{
	Personp("猪八戒", 998);
	printPerson02(p);
}
//3、整个类都模板化
template
void printPerson03(T& p)
{
	p.showPerson();
	cout << "T的类型:" << typeid(T).name() << endl;
}
void test03()
{
	Personp("唐僧", 30);
	printPerson03(p);
	
}
int main()
{
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989703.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号