集合的模拟实现(函数模板)

Posted hello_OK

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合的模拟实现(函数模板)相关的知识,希望对你有一定的参考价值。

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

三个模板函数如下:

int addSet(T * myset, T elem,int len)

int deleSet(T * myset, T elem, int len)

int findElem(T * myset, T elem, int len)

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

主函数有如下数据成员 :

int intSet[100]

double douSet[100]

String StrSet[100] 分别是int类型、double类型、String的数组集合。

int intLen, douLen, strLen分别是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!

------------------------------------------------------------------------------------------------------

                   参考代码

------------------------------------------------------------------------------------------------------

 

 

  1 #include<iostream>
  2 using namespace std;
  3 //实现add函数模版 
  4 template<typename classType>
  5 int add(classType set[],classType n,int place)//传人数组和元素
  6 {
  7     int i,flag=0;
  8     for(i=0;i<place;i++)
  9     {
 10         if(set[i]==n)//已经存在的情况  
 11         {
 12             cout<<n<<" is already exist!"<<endl;
 13             flag=1;
 14             break;
 15         }
 16     }
 17     if(i==100)   cout<<"Full Set."<<endl;//数组已满的情况 
 18     else  if(flag==0)//正常情况 
 19     {
 20         place++;
 21         set[i]=n;
 22         cout<<i<<endl;
 23     }
 24     return place;
 25 }
 26 //实现delete
 27 template<typename classType>
 28 int deleted(classType set[],classType n,int place)
 29 {
 30     int i,flag=0,j;
 31     for(i=0;i<place;i++)
 32     {
 33         if(set[i]==n)
 34         {
 35             flag=1;
 36             cout<<i<<endl;
 37             break;
 38         }
 39     }
 40     if(flag)
 41     {
 42         place--;
 43         for(j=i;j<place;j++)
 44         {
 45             set[j]=set[j+1];
 46         }
 47     }
 48     else cout<<n<<" is not exist!"<<endl;
 49     return place;
 50 }
 51 //实现find
 52 template<typename classType>
 53 void find(classType set[],classType n,int place)
 54 {
 55     int i,flag=0;
 56     for(i=0;i<place;i++)
 57     {
 58         if(set[i]==n)
 59         {
 60             cout<<i<<endl;
 61             flag=1;
 62             break;
 63         }
 64     }
 65     if(flag==0)   cout<<n<<" is not exist!"<<endl;
 66 }
 67 int main()
 68 {
 69     //数组模拟集合 
 70     int intSet[100];
 71     double douSet[100];
 72     string StrSet[100];
 73     //记录集合长度 
 74     int intLen,douLen,StrLen;
 75     //add.find 
 76     int classType,workType;
 77     int intPlace=0,douPlace=0,strPlace=0;
 78     cin>>classType;
 79     while(classType!=0)
 80     {
 81         switch(classType)
 82         {
 83             case 1://int
 84                     {
 85                         int helloIn;
 86                         cin>>workType>>helloIn;
 87                         switch(workType)
 88                         {
 89                             //插入 
 90                             case 1:intPlace=add(intSet,helloIn,intPlace);break;
 91                             //删除         
 92                             case 2:intPlace=deleted(intSet,helloIn,intPlace);break;
 93                             //查找        
 94                             case 3:find(intSet,helloIn,intPlace);break;    
 95                         }
 96                         break;
 97                     }
 98             case 2://double
 99                     {
100                         double helloDou;
101                         cin>>workType>>helloDou;
102                         switch(workType)
103                         {
104                             //插入 
105                             case 1:douPlace=add(douSet,helloDou,douPlace);break;
106                             //删除         
107                             case 2:douPlace=deleted(douSet,helloDou,douPlace);break;
108                             //查找        
109                             case 3:find(douSet,helloDou,douPlace);break;
110                         }
111                         break;
112                     }
113             case 3://string        
114                     {
115                         string helloStr;
116                         cin>>workType>>helloStr;
117                         switch(workType)
118                         {
119                             //插入 
120                             case 1:strPlace=add(StrSet,helloStr,strPlace);break;
121                             //删除         
122                             case 2:strPlace=deleted(StrSet,helloStr,strPlace);break;
123                             //查找        
124                             case 3:find(StrSet,helloStr,strPlace);break;                        
125                         }
126                         break;
127                     }
128         }
129         cin>>classType;
130     }    
131     return 0;
132 }

 

以上是关于集合的模拟实现(函数模板)的主要内容,如果未能解决你的问题,请参考以下文章

C#常用代码片段备忘

调用模板化成员函数:帮助我理解另一个 *** 帖子中的代码片段

unordered_map和unordered_set的模拟实现

代码片段 - Golang 实现集合操作

map和set的模拟实现

Delphi7:快捷键