如何自己实现模板类(简单案例——顺序表)
2016-08-18 10:07
531 查看
可以将模板类SeqList的定义及成员函数的实现代码全部写到SeqList.h头文件中,则在实例化该类后可进行基本的线性表操作
具体的代码怎么写,其实核心就是 template<class T> 其中 T 就是一个未知的类型,在写头文件时,变量的类型都用T代替。用户在使用此模板时应该把要使用的变量类型填入进去(可以是基本类型 如 int char 等等 也可以是用户自定义的复杂类型 如 结构体、类)
下面是SeqList.h头文件的写法(这里把实现代码也写在了头文件中)
#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED
#include <iostream>
using namespace std;
const int MAX = 1005;
template <class T>
class SeqList{
public:
SeqList(){length = 0;}//无参构造函数
SeqList(const T a[], int n);//有参构造函数,使用含有n个元素的数组a初始化
int GetSeqList(){return length;}//获取顺序表的长度
void PrintSeqList();//按顺序打印表中的元素
void Insert(int i, T x);//在顺序表的第i个位置插入值为x的新元素
T Delete(int i);//删除顺序表第i个元素,并返回该元素的值
T Get(int i);//获取顺序表第i个元素
int Locate(T x);//查找顺序表中值为x的元素,找到后返回其位置
private:
T date[MAX];
int length;
};
template<class T>
SeqList<T>::SeqList(const T a[], int n)
{
if(n > MAX) throw "数组长度超过线性表最大长度" ;
for(int i = 0; i < n; i++)
date[i] = a[i];
length = n;
}
template<class T>
void SeqList<T>::PrintSeqList()
{
cout << "按序号一次遍历线性表中的各个数据元素:" << endl;
for(int i = 0; i < length; i++)
cout << date[i] << " ";//若数据类型为复杂类型,应当对 << 进行重载
cout << endl;
}
template<class T>
void SeqList<T>::Insert(int i, T x)
{
if(length >= MAX) throw "上溢异常";
if(i < 1 || i > length + 1) throw "位置异常";
for(int j = length; j >= i; j--)
date[j] = date[j-1];
date[i-1] = x;
length++;
}
template<class T>
T SeqList<T>::Delete(int i)
{
if(length == 0) throw "下溢异常";
if(i < 1 || i > length) throw "位置异常";
T x = date[i-1];
for(int j = i; j < length; j++)
date[j-1] = date[j];
length--;
return x;
}
template<class T>
T SeqList<T>::Get(int i)
{
if(i < 1 || i > length) throw "查找位置非法";
return date[i-1];
}
template<class T>
int SeqList<T>::Locate(const T x)
{
for(int i = 0; i < length; i++)
if(x == date[i]) return i + 1;
return 0;//查找失败
}
#endif // SEQLIST_H_INCLUDED
下面是使用该头文件
#include <iostream>
#include "SeqList.h"
using namespace std;
int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7};
SeqList<int> List(a, 7);
List.PrintSeqList();
List.Insert(1, 0);
List.PrintSeqList();
int x = List.Delete(8);
cout << "删除元素:" << x << endl;
List.PrintSeqList();
int p = List.Locate(4);
cout << "元素4的位置:" << p << endl;
return 0;
}
具体的代码怎么写,其实核心就是 template<class T> 其中 T 就是一个未知的类型,在写头文件时,变量的类型都用T代替。用户在使用此模板时应该把要使用的变量类型填入进去(可以是基本类型 如 int char 等等 也可以是用户自定义的复杂类型 如 结构体、类)
下面是SeqList.h头文件的写法(这里把实现代码也写在了头文件中)
#ifndef SEQLIST_H_INCLUDED
#define SEQLIST_H_INCLUDED
#include <iostream>
using namespace std;
const int MAX = 1005;
template <class T>
class SeqList{
public:
SeqList(){length = 0;}//无参构造函数
SeqList(const T a[], int n);//有参构造函数,使用含有n个元素的数组a初始化
int GetSeqList(){return length;}//获取顺序表的长度
void PrintSeqList();//按顺序打印表中的元素
void Insert(int i, T x);//在顺序表的第i个位置插入值为x的新元素
T Delete(int i);//删除顺序表第i个元素,并返回该元素的值
T Get(int i);//获取顺序表第i个元素
int Locate(T x);//查找顺序表中值为x的元素,找到后返回其位置
private:
T date[MAX];
int length;
};
template<class T>
SeqList<T>::SeqList(const T a[], int n)
{
if(n > MAX) throw "数组长度超过线性表最大长度" ;
for(int i = 0; i < n; i++)
date[i] = a[i];
length = n;
}
template<class T>
void SeqList<T>::PrintSeqList()
{
cout << "按序号一次遍历线性表中的各个数据元素:" << endl;
for(int i = 0; i < length; i++)
cout << date[i] << " ";//若数据类型为复杂类型,应当对 << 进行重载
cout << endl;
}
template<class T>
void SeqList<T>::Insert(int i, T x)
{
if(length >= MAX) throw "上溢异常";
if(i < 1 || i > length + 1) throw "位置异常";
for(int j = length; j >= i; j--)
date[j] = date[j-1];
date[i-1] = x;
length++;
}
template<class T>
T SeqList<T>::Delete(int i)
{
if(length == 0) throw "下溢异常";
if(i < 1 || i > length) throw "位置异常";
T x = date[i-1];
for(int j = i; j < length; j++)
date[j-1] = date[j];
length--;
return x;
}
template<class T>
T SeqList<T>::Get(int i)
{
if(i < 1 || i > length) throw "查找位置非法";
return date[i-1];
}
template<class T>
int SeqList<T>::Locate(const T x)
{
for(int i = 0; i < length; i++)
if(x == date[i]) return i + 1;
return 0;//查找失败
}
#endif // SEQLIST_H_INCLUDED
下面是使用该头文件
#include <iostream>
#include "SeqList.h"
using namespace std;
int main()
{
int a[7] = {1, 2, 3, 4, 5, 6, 7};
SeqList<int> List(a, 7);
List.PrintSeqList();
List.Insert(1, 0);
List.PrintSeqList();
int x = List.Delete(8);
cout << "删除元素:" << x << endl;
List.PrintSeqList();
int p = List.Locate(4);
cout << "元素4的位置:" << p << endl;
return 0;
}
相关文章推荐
- 基于NanoPi的声音传感器驱动开发
- 平行四边形导航,背景颜色渐变动画(不支持IE6/7/8)
- HDU 4445 Crazy Tank 高中物理知识忘得差不多了
- Stanford机器学习---第六讲. 怎样选择机器学习方法、系统
- IntelliJ IDEA and Scala tutorial
- 常见浏览器的兼容问题
- STM32F1外部中断的使用
- 在TFS上,使用tf workspace命令删除其他用户的工作区
- (LeetCode)Isomorphic Strings --- 同构字符串
- LeetCode:Longest Palindromic Substring 最长回文子串
- Scala的核心力量和黄金定律
- 自定义ActionBar,在BaseActivity如何降低耦合
- 发现一个免费邮件代发平台,sendpost.cn
- Android混淆——混淆代码总结
- aaaaaaaaaaaaaaa
- JS 学习(四)对象
- iOS - OC Category 分类
- atoi函数的实现
- 麻省理工告诉我们男女配对的真相
- 数组排序sort()