C++ 学习笔记 十

C++ 学习笔记 十,第1张

C++ 学习笔记 十

模板函数

 

#include
using namespace std;


template 
void show (T a)
{
cout<

模板 冒泡排序

#include
using namespace std;
template
void Bubbleshort(T arr[],int len,bool (*pfun)(T ,T))
{



	int i;
	int j;
	int n=1;
	int count=0;
	for(j=0;jb);
}
bool rule2(float a,float b)
{

		return (a 

模板 

#include
using namespace std;


template 
class point
{
public:
	T1 m_x;
	T2 m_y;
public:
	point(T1 x,T2 y);
public:
	void show();
};
template 
point::point(T1 x,T2 y)
{
	m_x=x;
	m_y=y;

}
template 
void point::show()
{
	cout< po(1,6);
	po.show();
	point p2(1,5.54);
	p2.show();
	system("pause");
return 0;
}

类模板队列

#include
using namespace std;

template
class queue
{
	struct Node{
	T value;
	Node* pnext;
	};
private:
	Node*pHead;
	Node*pEnd;
public:
	queue();
	~queue();
public:
	void pushback(T n);
	T pop();


};
template
queue::queue()
{

	pHead=NULL;
	pEnd=NULL;
	
}
template
queue::~queue()
{
	
	if(pHead!=NULL)
	{
		Node*Del=pHead;
	
		pHead=pHead->pnext;
		delete Del;
		Del=NULL;
	}

}
template
void queue::pushback(T n)
{
	Node* ptemp=new Node;
	ptemp->value=n;
	ptemp->pnext=NULL;
	if(pHead!=NULL)
	{
	pHead=ptemp;
		pEnd=ptemp;
	}
	else
	{
	pEnd->pnext=ptemp;
	pEnd=ptemp;
	}
}
template
T queue::pop()
{
	
	if(pHead!=NULL)
	{

	Node*pmark=pHead;
	T value =pmark->value;
	pHead=pHead->pnext;
	delete pmark;
	pmark=NULL;
	return value;
	}
	else
		return 0;
}

int main()
{

	queueq1;
	q1.pushback(1);
	q1.pushback(2);
	q1.pushback(3);
	q1.pushback(4);
	int n=q1.pop();



	system("pause");
return 0;
}

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

原文地址: https://outofmemory.cn/zaji/5703070.html

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

发表评论

登录后才能评论

评论列表(0条)

保存