C++模板

C++模板,第1张

C++模板

模板:把类型当作未知量来处理,可以忽略类的影响

1 函数模板

模板的声明  :template //_Ty可以改,类型的代号  typename可以改为class 

调用函数模板:隐式: 正常函数传参

                         显示: 函数名<类型名>(参数)

函数模板的两种形态:普通函数当作函数模板

                                    类的成员函数当作函数模板

函数模板特殊写法:缺省,存在传常量写法

#include 
using namespace std;
//int Max(int a, int b)
//{
//	return a > b ? a : b;
//}
//string Max(string a, string b)
//{
//	return a > b ? a : b;
//}
//引入了模板
template //告诉编译器下面代码引入一个位置类型叫做_Ty
_Ty Max(_Ty a, _Ty b)
{
	return a > b ? a : b;
}
template 
void print(A1 a, A2 b)//多个位置类型的写法
{
	cout << "两个位置类型" << endl;
}
template 
void print(B1 a, B2 b)//多个位置类型的写法
{
	cout << a << endl << b << endl;
}
class student
{
public:
	template 
	void print(S a)
	{
		cout << a << endl;
	}
	template 
	void print1(S1 a);
protected:
	string name;
	int age;
};
template //在类外实现函数时template不能省略
void student::print1(S1 a)
{
	cout << a << endl;
}
template 
void printdata(T1 d1, T2 d2)
{
	cout << d1 << endl << d2 << endl;
}
void testpd()//缺省的写法
{
	//显示调用可以不传类型,但是参数不能少
	printdata("string", 4835);
	printdata("lkfn", 6151);
}
template 
void printArry(T3 arry)
{
	for (int i = 0; i < size; i++)
	{
		cout << arry[i];
	}
	cout << endl;
}
int main()
{
	//隐式调用
	cout << Max(8, 9) << endl;
	cout << Max("kjsdb", "snoish") << endl;
	//显示调用
	cout << Max("ndos", "ofhos") << endl;
	print("str", 99);
	//类中的成员函数是函数模板
	student A;
	A.print(3);
	A.print1(9);
	//函数模板只能传入常量
	int arry[3] = { 9,8,7 };
	printArry(arry);
	return 0;
}

2 类模板

template

class A

{

public:

protected:

}//只要被template修饰就是类模板

类模板的调用必须采用显示调用

类模板不是一个实际类型,所以所有用到类名的地方都要使用类名<未知类型>方式使用

在多文件中类模板的声明和实现要在一起

template 
class student
{
public:
	student() {}
	student(string name) :name(name) {}//构造函数正常写
	void print();
protected:
	string name;
};
//在类外实现
template 
void student::print()
{
	cout << "类模板" << endl;
}
template //类模板的继承
class book :public student
{
public:
	book(string name) :student(name) {}
};
template 
class Data
{
public:
	Data(T1 one, T2 two) :one(one), two(two) {}
	void print();
protected:
	T1 one;
	T2 two;
};
template 
void Data::print()
{
	cout << one << endl << two << endl;
}
int main()
{
//	student A;//报错
	studentA;//必须采用显示调用
	student book("jdfho");
	Data A("sfh'", 18);
	A.print();
	DataB(9, 8);
	B.print();
	return 0;
}

3 模板传入自定义类型

基本自定义类型

自定义类型也是一个模板

模板传入自定义类型,关键在于重载运算符

class student
{
public:
	student(string name, int age) :name(name), age(age) {}
	friend ostream& operator<<(ostream& out, student& S)
	{
		out << S.name << endl << S.age << endl;
		return out;
	}
protected:
	string name;
	int age;
};
template 
void print(T1 one)
{
	cout << one << endl;
}
int main()
{
	print(2);
	print("ldjfn");
	print(student("hhh", 18));
	return 0;
}

4 模板嵌套

template 
class student
{
public:
	student(s1 one, s2 two) :one(one), two(two) {}
	friend ostream& operator<<(ostream& out, student& S)
	{
		out << S.one << endl << S.two << endl;
		return out;
	}
protected:
	s1 one;
	s2 two;
};
template 
class Data
{
public:
	Data(s1 one, s2 two) :one(one), two(two) {}
protected:
	s1 one;
	s2 two;
};
void testF()
{
	Data, student>
		data(student("lksd", 18), student("klsafhk", 18));
}

template
void print(a data)
{
	cout << data << endl;
}

int main()
{
	//隐式调用
	print(student("jsaofh", 19));
	//显示调用
	print>(student("ojdsf", 20));
	//起别名
	using STty = student;
	print(STty("jshos", 18));
	return 0;
}

5 模板重载

模板和普通函数,调用函数类型一直到情况下,优先调用普通函数

两个模板同时成立时

6 模板特化

特殊化处理

  局部特化

  完全特化

template
class student
{
public:
    student(s1 one, s2 two) :one(one), two(two) {}
    void print()
    {
        cout << one << endl << two << endl;
    }
protected:
    s1 one;
    s2 two;
};
//局部特化,特殊化
//即将两个类型变为一个类型
template
class student
{
public:
    student(s1 one, s1 two) :one(one), two(two) {}
    void print()
    {
        cout << one << endl << two << endl;
    }
protected:
    s1 one;
    s1 two;
};
template<>
//完全特化
class student
{
public:
    student(string one, string two) :one(one), two(two);
    void print()
    {
        cout << one << endl << two << endl;
    }
protected:
    string one;
    string two;
};

int main()
{
    student A("lknf", 18);
    student B("ljdf", "kdfn");
    A.print();
    B.print();//完全特化的模板,折叠参数
    student C("ljdf", "padifh");
    C.print();
    return 0;
}

就到这里啦

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5659435.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存