当前位置:网站首页>Function template

Function template

2022-07-05 04:33:00 On the bald Road

Apply to The body of the function is the same , The number of function parameters is the same , And different types of situations ; Pay attention to the difference between heavy load and heavy load .

How to write it :template<typename T>

                General function definition

example :

#include<iostream>
using namespace std;
template<typename T>
T max1(T a, T b) {
	if (a > b)
	b = a;
	return b;
}
int main() {
	int a = max1(5, 4);
	cout << a << endl;
	double b = max1(5.44, 6.31);
	cout << b;
	return 0;

}

原网站

版权声明
本文为[On the bald Road]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140636192928.html