C++模板(函数模板、类模板)

C++模板: C++提供一种模板的机制来减少代码重复。
比如:对于同一样函数使用不同的数据类型,int,double,char等。
C++模板属于“元编程”的范畴。

C++ 模板函数:

//模板函数
template<typename T>
void swap(T& t1, T& t2)
{
	T temp = t1;
	t1 = t2;
	t2 = temp;
}

template<typename T>
T add(const T& t1, const T& t2)
{
	return t1 + t2;
}
int main()
{
	cout << add(2, 3) << endl;       //隐式使用模板
	cout << add(2.4, 3.5) << endl;

	cout << add<int>(2, 3) << endl;  //显式使用模板
	cout << add<double>(12.34, 5) << endl;
	cout << add<double>(12.9, 67.8) << endl;
	return 0;
}

Tips:模板的关键字可以用class或者typename

template<class T>template<typename T> 

两者表达的意思是一样的

•在模板函数的定义中,T代表数据类型。
•模板的声明和定义必须在同一个文件中
•C语言的宏定义也可以实现函数模板的功能,区别在于宏没有类型检查,函数模板有类型检查。

C++模板类

//类模板--------------并不是真正的类
template<class T>   //泛型类
class SeqList
{
public:
	SeqList(size_t _capacity = 10)
		:array(new T[_capacity])
		,capacity(_capacity)
		,size(0)
	{
		//构造函数
	}

	SeqList(const SeqList<T>& s)
	{
		//拷贝构造
		this->array = s.array;
		this->capacity = s.capacity;
		this->size = s.size;
	}

	SeqList<T>& operator=(const SeqList<T>& s)
	{
		//赋值运算符重载
		this->array = s.array;
		this->capacity = s.capacity;
		this->size = s.size;
	}

	~SeqList()
	{
		if (array)
		{
			delete[] array;
			capacity = 0;
			size = 0;
		}
	}
/
	size_t _size()const       //顺序表元素个数
	{
		return size;
	}

	size_t _capacity()const   //顺序表容量
	{
		return capacity;
	}

	bool empty()const        //顺序表是否为空
	{
		return 0 == size;
	}

	void push_back(const T& data)  //尾插
	{
		array[size++] = data;
	}

	void pop_back()                //尾删
	{
		if (empty())
		{
			return;
		}
		size--;
	}
/
	T& operator[](size_t index)        
	{
		assert(index < size);
		return this->array[index];
	}
	const T& operator[](size_t index)const   
	{
		assert(index < size);
		return this->array[index];
	}



private:
	T* array;           //连续空间
	size_t capacity;    //容量
	size_t size;        //有效元素

};




int main()
{
	SeqList<int> s1;   //10个整型连续空间
	s1.push_back(10);
	s1.push_back(2);
	s1.push_back(3);
	s1.push_back(4);
	s1.push_back(5);
	s1.push_back(6);
	s1.push_back(7);
	s1.push_back(8);
	cout << s1._capacity() << endl;
	cout << s1._size() << endl;
	s1.pop_back();
	cout << s1._capacity() << endl;
	cout << s1._size() << endl;
	cout << "s1[0] = " << s1[0] << endl;
	s1[0] = 1;
	cout << "s1[0] = " << s1[0] << endl;



	//SeqList<double> s2;
	//s2.push_back(1.0);
	//s2.push_back(2.0);
	//s2.push_back(3.0);
	//s2.push_back(4.0);
	//cout << s2._capacity() << endl;
	//cout << s2._size() << endl;
	//s2.pop_back();
	//cout << s2._capacity() << endl;
	//cout << s2._size() << endl;

	//const SeqList<int> s3;   //无意义
	//cout << s3[0] << endl;

	return 0;
}

Tips:上述代码中,生成s1和s3调用的是同一个函数,而生成s2则调用的是另一个函数
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

枳洛淮南✘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值