集合的模拟实现(类模板) ——类模板的应用

集合的模拟实现(类模板) ——类模板的应用

我们可以用一个类来模拟集合及集合运算,add运算用以实现集合元素的增加,delete运算用于实现集合元素的删除,find运算用以实现集合元素的查找,但是目前集合元素类型未知,可以是int、char、double等基本数据类型,也可以是String、Time、Student等对象类型,要求采用类模板实现集合及集合运算,包括集合元素的增加、删除和查找的等基本功能。

集合模板类MySet包括数据如下:

T data[100];//用数组来存放所有的集合元素,最多不超过100个元素

int count;//表示目前集合中有多少个元素

包括成员函数如下:

构造函数若干个,集合运算函数如下:

int addSet( T elem)

int deleSet(T elem)

int findElem(T elem)

其中,addSet向集合中添加一个元素,deleSet从集合中删除一个元素,findElem判断elem是否是集合成员,三个函数分别返回元素插入位置,删除位置和存在位置。

主函数有如下数据成员 :

MySet<int> intSet;(反斜杠是转义字符,使用时去掉)

MySet<double> douSet;

MySet<string> strSet;

分别是int类型、double类型、String的集合。

完成上述类模板和主函数,主函数根据输入的信息,建立初始的三种不同类型的空集合对象,调用成员函数分别对intSet、douSet和StrSet执行相应的操作,并输出对应的集合信息。

输入格式:

每一行为一个集合操作,每行的第一个数字为集合元素类型,1为整型元素,2为浮点型元素,3为String类型,第二个数字为集合操作类型,1为插入,2为删除,3为查找,第三个为集合元素,集合元素类型视第一个数字给定的集合元素类型而定。输入0时标志输入结束。

输出格式:

输出当前操作的执行位置(插入位置、删除位置和存在位置)

删除操作时,如果元素X不存在,输出“X is not exist!”。

插入操作时,如果集合已满,输出“Full Set.”若元素已存在,输出“X is already exist!”

查找操作时,如果找不到元素,输出“X is not exist!”。

输入:

1 1 1

1 1 2

1 3 1

1 2 1

1 2 3

1 3 1

2 1 1.1

2 1 2.2

2 1 3.3

2 3 1.1

2 2 2.2

2 2 2.2

3 1 abc

3 1 bcd

3 3 abc

3 2 abc

3 3 abc

0

输出:

0

1

0

0

3 is not exist!

1 is not exist!

0

1

2

0

1

2.2 is not exist!

0

1

0

0

abc is not exist!

代码

#include<bits/stdc++.h>
using namespace std;

template<class T>
class Myset
{
	private:
		T data[105];
		int count;
	public:
		 Myset()
		 {
		 	count=0;
		 }
		 
		 int simple_find(T elem)
		 {
		 	for(int i=0;i<count;i++)
		 	{
		 		if(data[i]==elem)
		 		{
		 			return i;
				}
			 }
			 return -1;
		 }
	     void addSet( T elem)
	     {
	     	if(this->count>=100)
	     	  cout<<"Full Set."<<endl;
	     	else
	     	{
	     		if(simple_find(elem)==-1)
	     		{
	     			cout<<count<<endl;
	     			data[count++]=elem;
				}
	     		else
	     		{
	     			cout<<elem<<" is already exist!"<<endl;
				 }
			 }
		 }

         void deleSet(T elem)
         {
         	int pos=simple_find(elem);
         	if(pos==-1)
         	  cout<<elem<<" is not exist!"<<endl;
         	else
         	{    
         	    cout<<pos<<endl;
         		for(int i=pos;i<count-1;i++)
         		{
         			data[i]=data[i+1];
				 }
				 count--;
			 }
		 }

         void findElem(T elem)
         {
         	for(int i=0;i<this->count;i++)
         	{
         		if(data[i]==elem)
         		{
         			cout<<i<<endl;
         			return ;
				}		  
			}
			 cout<<elem<<" is not exist!"<<endl;
		 }
};
int main()
{
	Myset<int > inSet;
	Myset<string > strSet;
	Myset<double > douSet;
	
	int ops;
	while(1)
	{
		cin>>ops;
		if(ops==0)
		  break;
		  
		if(ops==1)
		{
			int choice,x;
			cin>>choice>>x;
			switch(choice)
			{
				case 1:
				    inSet.addSet(x);
					break;
				case 2:
					inSet.deleSet(x);
					break;
				case 3:
					inSet.findElem(x);
			}
		}
		
		else if(ops==2)
		{
			int choice;
			double x;
			cin>>choice>>x;
			switch(choice)
			{
				case 1:
				    douSet.addSet(x);
					break;
				case 2:
					douSet.deleSet(x);
					break;
				case 3:
					douSet.findElem(x);
			}
		}
		
		else if(ops==3)
		{
			int choice;
			string x;
			cin>>choice>>x;
			switch(choice)
			{
				case 1:
				    strSet.addSet(x);
					break;
				case 2:
					strSet.deleSet(x);
					break;
				case 3:
					strSet.findElem(x);
			}
		}
	}
	return 0;
}

类模板的结构

类的定义

template <class T>
class 类名{

};

类的实例化

类名 <数据类型> 变量名

类模板的意义

  • 增强代码的复用性

反思

  • 不要在一棵树上吊死,要多种几棵树。find可以多写一个(不要去强求只用一个,特别是当这个函数本身极具特殊性)。

参考

参考

原文地址:https://www.cnblogs.com/BeautifulWater/p/14825478.html