c++模板类的用法

RTRTRTRTRTRTRTRTRTRT

//顺序表的实现 模板类
#include<iostream>
#include<string>
using namespace std;
const int MaxSize=100;
bool error;
template<class T>
class SeqList
{
public:
SeqList(){length=0;}; //无参构造函数
SeqList(T a[],int n); //有参构造函数
~SeqList(){}; //析构
void Insert(int i,T x); //在i位置插入x
int GetLength(){return length;}; //求表长
T Del(int i); //删除第i个元素
T Get(int i); //按位查找 取第i个元素
int Locate(T x); //按值查找 求x为第几个元素
void PrintList(); //遍历并输出
private:

T data[MaxSize];
int length;
};
//有参构造函数
template<class T>
SeqList<T>::SeqList(T a[],int n)
{
int i;
if (n>MaxSize) {cout<<"上溢"<<endl; error=true;}
else for(i=0;i<n;i++) data[i]=a[i];
}
//在i位置插入x
template<class T>
void SeqList<T>::Insert(int i,T x)
{
int j;
if (length>=MaxSize) {cout<<"表满,无法插入"<<endl; error=true;}
else if(i>length+1||i<1) {cout<<"位置异常"<<endl; error=true;}
else
{
for(j=length-1;j>=i-1;j--)
data[j+1]=data[j];
data[i-1]=x;
length++;
};

}
//删除第i个元素
template<class T>
T SeqList<T>::Del(int i)
{
int x,j;
if(i>length||i<1) {cout<<"位置异常"<<endl; error=true;return 0;}
else
{
x=data[i-1];
for(j=i-1;j<=length-1;j++)
data[j]=data[j+1];
length--;
return x;
}
}
//按位查找 取第i个元素
template<class T>
T SeqList<T>::Get(int i)
{
if(i>MaxSize||i<1) {cout<<"位置异常"<<endl; error=true;return -1;}
else return data[i-1];
}
//按值查找 求x为第几个元素
template<class T>
int SeqList<T>::Locate(T x)
{
int i;
for(i=0;i<length;i++)
if (data[i]==x) return i+1; //相同的值?
return 0;
}
//遍历并输出
template<class T>
void SeqList<T>::PrintList()
{
int i;
cout<<"-----------------------------------------------------------------------";
cout<<endl;
cout<<"|";
for(i=0;i<length;i++)
cout<<" "<<data[i]<<" |";
cout<<endl;
cout<<"-----------------------------------------------------------------------"<<endl;
}
int main()
{
cout<<"顺序表的实现(模板类)"<<endl;
cout<<"************************************"<<endl;
cout<<"* 1.插入 *"<<endl;
cout<<"* 2.按位查找 取第i个元素 *"<<endl;
cout<<"* 3.按值查找 求x为第几个元素 *"<<endl;
cout<<"* 4.删除 *"<<endl;
cout<<"* 5.输出顺序表 *"<<endl;
cout<<"* 6.输出表长 *"<<endl;
cout<<"* 7/help 输出此表 *"<<endl;
cout<<"* 8/exit.退出 *"<<endl;
cout<<"************************************"<<endl;

int flag,ins_loc,x,tab,len;
flag=0;
SeqList<int>List;
while (flag==0)
{
error=false;
cout<<"Please input the command(input 7 to get the command list):"<<endl;
cin>>tab;
switch(tab)
{
case 1:
{
cout<<"Please input the insert location:"<<endl;
cin>>ins_loc;
cout<<"Please input the insert num:"<<endl;
cin>>x;
List.Insert(ins_loc,x);
break;
}
case 2:
{
cout<<"Please input the location:"<<endl;
cin>>ins_loc;
x=List.Get(ins_loc);
if(!error)
cout <<"The number is:"<<x<<endl;
break;
}
case 3:
{
cout<<"Please input the number you want to find in the list:"<<endl;
cin>>x;
ins_loc=List.Locate(x);
if(!error)
cout<<"The number's location is:"<<ins_loc<<endl;
break;
}
case 4:
{
cout<<"Please input the location you want to delete:"<<endl;
cin>>ins_loc;
List.Del(ins_loc);
break;
}
case 5:
{
List.PrintList();
break;
}
case 6:
{
if(!error)
len=List.GetLength();
cout<<"The length is:"<<len<<endl;
break;
}
case 7:
{
cout<<"************************************"<<endl;
cout<<"* 1.插入 *"<<endl;
cout<<"* 2.按位查找 取第i个元素 *"<<endl;
cout<<"* 3.按值查找 求x为第几个元素 *"<<endl;
cout<<"* 4.删除 *"<<endl;
cout<<"* 5.输出顺序表 *"<<endl;
cout<<"* 6.输出表长 *"<<endl;
cout<<"* 7/help 输出此表 *"<<endl;
cout<<"* 8/exit.退出 *"<<endl;
cout<<"************************************"<<endl;
break;
}
case 8:
{
flag=1;
break;
}
default:
{
cout<<"The command is no found!"<<endl;
break;
}
}
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
相似回答