[C++]使用vector描述线性表定义及基本操作
#ifndef VECTORLIST_H
#define VECTORLIST_H
#include<iostream>
#include"linearlist.h"
#include<vector>
#include<myexceptions.h>
using namespace std; template<class T>
class vectorList : public linearList<T>
{
public:
//构造函数
vectorList(int initialCapacity = 0); //复制构造函数
vectorList(const vectorList<T>&); //析构函数
~vectorList()
{
delete element;
} /*
* 类linearList中抽象方法的实现
*/
//判断是否表空
bool empty() const
{
return element->empty();
} //返回表内元素个数
int size() const
{
return (int)element->size();
} //返回索引为theIndex的元素
T& get(int theIndex) const; //返回元素theElement的索引
int indexOf(const T &theElement) const; //删除索引为theIndex的元素
void erase(int theIndex); //在索引为theIndex的位置插入元素theElement
void insert(int theIndex, const T &theElement); //将线性表元素放入输出流
void output(ostream &out) const; /*
* 增加的方法
*/
int capacity() const
{
return (int)element->capacity();
} /*
* 线性表的起始和结束位置的迭代器
*/
typedef typename vector<T>::iterator iterator;
iterator begin(){return element->begin();}
iterator end(){return element->end();}
protected:
void checkIndex(int theIndex) const;
vector<T>* element;//存储线性表元素的向量
}; /*
* 构造函数和复制构造函数的实现
*/
template<class T>
vectorList<T>::vectorList(int initialCapacity)
{
//构造函数
if(initialCapacity < 1)
{
ostringstream s;
s<<"Initial capacity = "<<initialCapacity<<"Must be > 0";
throw illegalParameterValue(s.str);
}
element = new vector<T>;//创建向量为0的空向量
element->reserve(initialCapacity);//vector的容量从0增加到initialCapacity } template<class T>
vectorList<T>::vectorList(const vectorList<T> &theList)
{
//复制构造函数
element = new vector<T>(*theList.element);
} //删除元素
template<class T>
void vectorList<T>::erase(int theIndex)
{
checkIndex(theIndex);
element->erase(begin()+theIndex);
} //插入元素
template<class T>
void vectorList<T>::insert(int theIndex, const T &theElement)
{
if(theIndex < 0 || theIndex > size())
{
//无效索引
ostringstream s;
s<<"index = "<<theIndex <<" size = "<<size();
throw illegalIndex(s.str());
}
element->insert(element->begin()+theIndex,theElement);
} #endif // VECTORLIST_H
#ifndef MYEXCEPTIONS_H
#define MYEXCEPTIONS_H #include <string> using namespace std; // illegal parameter value
class illegalParameterValue
{
public:
illegalParameterValue(string theMessage = "Illegal parameter value")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // illegal input data
class illegalInputData
{
public:
illegalInputData(string theMessage = "Illegal data input")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // illegal index
class illegalIndex
{
public:
illegalIndex(string theMessage = "Illegal index")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // matrix index out of bounds
class matrixIndexOutOfBounds
{
public:
matrixIndexOutOfBounds
(string theMessage = "Matrix index out of bounds")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // matrix size mismatch
class matrixSizeMismatch
{
public:
matrixSizeMismatch(string theMessage =
"The size of the two matrics doesn't match")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // stack is empty
class stackEmpty
{
public:
stackEmpty(string theMessage =
"Invalid operation on empty stack")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // queue is empty
class queueEmpty
{
public:
queueEmpty(string theMessage =
"Invalid operation on empty queue")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // hash table is full
class hashTableFull
{
public:
hashTableFull(string theMessage =
"The hash table is full")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // edge weight undefined
class undefinedEdgeWeight
{
public:
undefinedEdgeWeight(string theMessage =
"No edge weights defined")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
}; // method undefined
class undefinedMethod
{
public:
undefinedMethod(string theMessage =
"This method is undefined")
{message = theMessage;}
void outputMessage() {cout << message << endl;}
private:
string message;
};
#endif // MYEXCEPTIONS_H
#ifndef LINEARLIST_H
#define LINEARLIST_H // LINEARLIST_H
// abstract class linearList
// abstract data type specification for linear list data structure
// all methods are pure virtual functions #include <iostream> using namespace std; template<class T>
class linearList
{
public:
virtual ~linearList() {};
virtual bool empty() const = 0;
// return true iff list is empty
virtual int size() const = 0;
// return number of elements in list
virtual T& get(int theIndex) const = 0;
// return element whose index is theIndex
virtual int indexOf(const T& theElement) const = 0;
// return index of first occurence of theElement
virtual void erase(int theIndex) = 0;
// remove the element whose index is theIndex
virtual void insert(int theIndex, const T& theElement) = 0;
// insert theElement so that its index is theIndex
virtual void output(ostream& out) const = 0;
// insert list into stream out
};
#endif
[C++]使用vector描述线性表定义及基本操作的更多相关文章
- 模仿std::vector写线性表的几点感想
数据结构还是很早之前学的了,当时才刚学过C语言,实现得都很简单,最近决定重新打牢基础,于是重新开始实现书上的数据结构和算法. 模仿C++ Primer的StrVec以及std::vector,使用模板 ...
- 《数据结构与STL-第二章 线性表》读书笔记
线性表 定义 线性表(linear list)是由零个或多个相同类型的数据元素构成的有限序列. 存储结构 顺序存储 最简单的存储方法是顺序存储法,即把线性表的数据元素按照逻辑次序顺序地放在一组地址连续 ...
- 动态分配的顺序线性表的十五种操作—C语言实现
线性表 定义:是最常用的,也是最简单的数据结构,是长度为n个数据元素的有序的序列. 含有大量记录的线性表叫文件 记录:稍微复杂的线性表里,数据元素为若干个数据项组成,这时把一个数据元素叫记录 结构特点 ...
- 线性表之顺序存储结构(C语言动态数组实现)
线性表的定义:N个数据元素的有限序列 线性表从存储结构上分为:顺序存储结构(数组)和 链式存储结构(链表) 顺序存储结构:是用一段连续的内存空间存储表中的数据 L=(a1,a2,a3....an) 链 ...
- 数据结构与算法(C/C++版)【绪论/线性表】
声明:数据结构与算法系列博文参考了<天勤高分笔记>.<王道复习指导>.C语言中文网.非商业用途,仅为学习笔记总结! 第一章<绪论> 一.基本概念及入门常识 /// ...
- TOJ 1214: 数据结构练习题――线性表操作
描述 请你定义一个线性表,可以对表进行"在某个位置之前插入一个元素"."删除某个位置的元素"."清除所有元素"."获取某个位置的元 ...
- 线性表&顺序线性表
第二章 线性表 参考文献:[数据结构(C语言版)].严蔚敏 本篇章仅为个人学习数据结构的笔记,不做任何用途. 2.1 线性结构的特点 (1). 存在唯一的一个被称为"第一个"的数据 ...
- c语言数据结构学习心得——线性表
线性表:具有相同数据类型的n(n>0)个数据元素的有限序列. 主要有顺序存储和链式存储. 顺序存储: 特点:地址连续,随机/存取,顺序存储. 建立:首地址/存储空间大小(数组),表长. 方式:静 ...
- C++线性表的链式存储结构
C++实现线性表的链式存储结构: 为了解决顺序存储不足:用线性表另外一种结构-链式存储.在顺序存储结构(数组描述)中,元素的地址是由数学公式决定的,而在链式储存结构中,元素的地址是随机分布的,每个元素 ...
随机推荐
- nodejs-npm模块管理器
JavaScript 标准参考教程(alpha) 草稿二:Node.js npm模块管理器 GitHub TOP npm模块管理器 来自<JavaScript 标准参考教程(alpha)> ...
- Apache2配置文件解读
每次碰到都不知道具体的作用,所以来分析一下 配置文件结构 apache2在启动的时候自动读取/etc/apache2/apache2.conf文件的配置信息,不同的配置项按功能分布在不同的文件中,然后 ...
- 简化版chmod
我们知道对文件访问权限的修改在Shell下可通过chmod来进行 例如 可以看到v.c文件从无权限到所有者可读可写可执行.群组和其他用户可读可执行 chmod函数原型 int chmod(const ...
- 100个Shell脚本——【脚本4】自定义rm命令
[脚本4]自定义rm命令 linux系统的rm命令太危险,一不小心就会删除掉系统文件. 写一个shell脚本来替换系统的rm命令,要求当删除一个文件或者目录时,都要做一个备份,然后再删除.下面分两种情 ...
- linux shell学习之shell流程控制
在linux shell编程中,流程控制结构与语句,也算是shell脚本中的重点了,不了解的朋友,跟随脚本小编一起来学习下吧. linux控制流结构学习. 一,shell控制流结构 1.控制结构 ...
- Jmeter初级入门教程
<jmeter:菜鸟入门到进阶>系列 创建一个简单的自动化脚本 创建线程组[Thread Group]: 右击[TestPlan]选择[Add]--[Thread(Users)]--[Th ...
- ActiveMQ(三)——理解和掌握JMS(1)
一.JMS基本概念 JMS是什么JMS Java Message Service,Java消息服务,是JavaEE中的一个技术. JMS规范JMS定义了Java中访问消息中间件的接囗,并没有给予实现, ...
- 三维引擎导入obj模型不可见总结
最近有客户试用我们的三维平台,在导入模型的时候,会出现模型全黑和不可见的情况.上一篇文章说了全黑的情况.此文说下不可见的情况. 经过测试,发现可能有如下两种情况. 导入的模型不在镜头视野内 导入的模型 ...
- 代码仓库gogs的基本配置使用
目录 一.基本功能介绍 主板说明 页面说明 用户设置 二.仓库 新建仓库 迁移仓库 仓库介绍 三.组织和团队 创建新组织 创建团队 一.基本功能介绍 主板说明 图中1表示自己个人账户下的仓库(所有权属 ...
- shell脚本 用户登录服务器发送钉钉提醒
一.企业微信配置 1.获取AgentId(AppID).Secret .CropID.部门ID 创建一个企业微信应用获取到AgentId(AppID).Secret 2.获取CropID,点击 &qu ...