C++Template

输出结果:

Result of Int:5
Result of Double:5.2


非类型形参

template<class T,int maxSize>
class TmpStack
{
public:
	TmpStack();
	void push(T const &);//入栈
	void pop();//出栈
	T top() const;
	bool isEmpty()const //返回是否为空
	{
		return numOfElems == 0;
	}
	bool isFull()const //返回是否满
	{
		return numOfElems == maxSize;
	}
	
private:
	T elems[maxSize];//包含元素的数组
	int numOfElems;//当前元素数量

};
template<class T, int maxSize>
TmpStack<T, maxSize>::TmpStack() :numOfElems(0)
{
}

template<class T, int maxSize>
void TmpStack<T, maxSize>::push(T const &element)
{
	if (numOfElems == maxSize)
		throw out_of_range("TmpStack<>::push:Stack if full!");
	elems[numOfElems] = element;
	++numOfElems;
}

template<class T, int maxSize>
void TmpStack<T, maxSize>::pop()
{
	if (numOfElems == 0)
		throw out_of_range("TmpStack<>::pop:Stack if empty!");
	--numOfElems;
}

template<class T, int maxSize>
T TmpStack<T, maxSize>::top() const
{
	if (numOfElems == 0)
		throw out_of_range("TmpStack<>::top:Stack if empty!");
	return elems[numOfElems-1];
}

int _tmain(int argc, _TCHAR* argv[])
{
	TmpStack<int, 20> intStackInstance;
	intStackInstance.push(7);
	std::cout << intStackInstance.top() << std::endl;   
	intStackInstance.pop();

	TmpStack<string, 15> stringStackInstance;
	stringStackInstance.push("string1");
	std::cout << stringStackInstance.top() << std::endl;

	return 0;
}

输出结果:

7

string1


比较大小

template<typename Type>
Type getMax(const Type a, const Type b) 
{
	return a>b? a:b;
}
int _tmain(int argc, _TCHAR* argv[])
{
	cout << getMax(2.3, 3.4) << endl;
	cout << getMax(2, 3) << endl;
	
	return 0;
}

带多个模板类型形参

template<class T1,class T2,class T3>
class MySumClass
{
public:
	MySumClass(){};
	~MySumClass(){};
	double sumTotal(T1 a, T2 b, T3 c);
	
private:

};
template<class T1, class T2, class T3>
double MySumClass<T1, T2, T3>::sumTotal(T1 a, T2 b, T3 c)
{
	return a + b + c;
}


int _tmain(int argc, _TCHAR* argv[])
{
	MySumClass<int, short, double> mySumClassInstance;
	cout << mySumClassInstance.sumTotal(2, 3, 4.5) << endl;

	return 0;
}

输出结果:

9.5



C++ templates are a powerful feature of the C++ programming language that allow generic programming. Templates enable the creation of functions and classes that can work with different data types without the need for separate implementations for each data type. Templates are defined using the keyword "template" followed by a list of template parameters enclosed in angle brackets "< >". The template parameters can be either type parameters or non-type parameters, depending on whether they represent a data type or a value. For example, a type parameter might be used to specify the data type of a container class, while a non-type parameter might be used to specify the size of an array. Here is an example of a simple function template that returns the maximum of two values: ```c++ template<typename T> T max(T a, T b) { return a > b ? a : b; } ``` In this example, the "typename" keyword is used to indicate that T is a type parameter. The function can be used with any data type for which the ">" operator is defined. Templates can also be used to define class templates, which are similar to regular classes but can work with different data types. Here is an example of a simple class template for a stack: ```c++ template<typename T> class Stack { public: void push(T value); T pop(); private: std::vector<T> data_; }; template<typename T> void Stack<T>::push(T value) { data_.push_back(value); } template<typename T> T Stack<T>::pop() { T value = data_.back(); data_.pop_back(); return value; } ``` In this example, the class template is defined with a single type parameter T. The member functions push and pop are defined outside the class definition using the scope resolution operator "::". Templates are a powerful tool that can greatly simplify code and make it more reusable. However, they can also be complex and difficult to debug. It is important to use templates judiciously and to thoroughly test them with a variety of data types.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值