vectorList.cpp
#include <exception>
#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <vector>
using namespace std;
//一个线性表的抽象类
template <class T>
class linearList
{
public:
virtual ~linearList(){}
virtual bool empty() const = 0;//返回true,当且仅当线性表为空
virtual int size() const = 0; //返回线性表的元素个数
virtual T& get(int theIndex) const = 0;//返回索引为 theIndex 的元素
//virtual int indexOf(const T& theElement) const = 0;//返回元素 theElement 第一次出现时的索引
virtual void erase(int theIndex) = 0;//删除索引为 theIndex 的元素
virtual void insert(int theIndex, const T& theElement) = 0;//把 theElement 插入线性表中索引为 theIndex 的位置上
virtual void output(ostream& out) const = 0; //把线性表插入输出流 out
};
//利用 vector 实现的基于数组的线性表
template<class T>
class vectorList : public linearList<T>
{
public:
//由构造函数,复制构造函数和析构函数
vectorList(int initialCapacity = 10);
vectorList(const vectorList<T>&);
~vectorList(){delete element;};
//ADT方法
bool empty() const { return element->empty(); }
int size() const{ return (int) element->size(); }
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex,const T& theElement);
void output(ostream& out) const;
//增加方法
int capacity() const {return (int) element->capacity(); }
void half();
void meld(const vectorList<T>& a,const vectorList<T>& b);
void merge(const vectorList<T>& a,const vectorList<T>& b);
void split(const vectorList<T>& a, const vectorList<T>& b);
//线性表的起始和结束位置的迭代器
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<<" Mute be > 0";
throw logic_error(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)
{
//删除索引为 theIndex 的元素
//如果没有这个元素,则抛出异常
checkIndex(theIndex);
element->erase(begin() + theIndex);
}
template<class T>
void vectorList<T>::insert(int theIndex,const T& theElement)
{
checkIndex(theIndex);
element->insert(element->begin() + theIndex,theElement);
//几如果在重定向量长度时空间不足,那么可以抛出没有捕捉的异常
}
template<class T>
void vectorList<T>::checkIndex(int theIndex) const
{
// 在索引为 theIndex 处插入元素 theElement
if(theIndex < 0 || theIndex > size())
{
//无效索引
ostringstream s;
s<<"index = "<<theIndex<<" size = "<<size();
throw logic_error(s.str());
}
}
template<class T>
T& vectorList<T>::get(int theIndex) const
{
checkIndex(theIndex);
return (*element)[theIndex];
}
template<class T>
void vectorList<T>::output(ostream &out) const
{
for(auto value : *element)
{
out<<value<<" ";
}
}
template<class T>
void vectorList<T>::half()
{
int listSize = (int)element->size();
for(int i = 2; i < listSize;i += 2)
(*element)[i/2] = (*element)[i];
int newSize = (listSize + 1) / 2;
element->erase(element->begin() + newSize,element->end());
}
template<class T>
void vectorList<T>::meld(const vectorList<T> &a,const vectorList<T>&b)
{
int i = 0;
vector<T>* minvecp;
vector<T>* maxvecp;
if(a.element->size() > b.element->size())
{
minvecp = b.element;
maxvecp = a.element;
}
else
{
minvecp = a.element;
maxvecp = b.element;
}
while(i < 2 * minvecp->size())
{
if(i%2)
element->push_back((*a.element)[i/2]);
else
element->push_back((*b.element)[i/2]);
i++;
}
i = minvecp->size();
auto start= maxvecp->begin() + i;
element->insert(element->end(),start,maxvecp->end());
}
template<class T>
void vectorList<T>::merge(const vectorList<T>& a,const vectorList<T>& b)
{
auto ia = a.element->begin();
auto aEnd = a.element->end();
auto ib = b.element->begin();
auto bEnd = b.element->end();
while((ia < aEnd) && (ib < bEnd))
{
if(*ia <= *ib)
{
element->push_back(*ia);
ia++;
}
else
{
element->push_back(*ib);
ib++;
}
}
element->insert(element->end(),ia,aEnd);
element->insert(element->end(),ib,bEnd);
}
template<class T>
void vectorList<T>::split(const vectorList<T>& a, const vectorList<T>& b)
{
int i = 0;
while(i < size())
{
if(i%2 == 0)
{
a.element->push_back(get(i++));
}
else
{
b.element->push_back(get(i++));
}
}
}
template<class T>
ostream& operator<<(ostream & out, const vectorList<T>& list)
{
list.output(out);
return out;
}
int main()
{
cout << "----------------------insert end--------------------" << endl;
vectorList<double> list1;
vectorList<double> list2;
cout<<list1<<endl;
cout<<list2<<endl;
for(int i = 0;i < 10;i++)
{
list1.insert(i,i);
}
for(int i = 10;i < 22;i++)
{
list2.insert(i-10,i);
}
cout<<list1<<endl;
cout<<list2<<endl;
cout << "----------------------insert end--------------------" << endl;
cout << "----------------------half start--------------------" << endl;
cout<<list1<<endl;
list1.half();
cout<<list1<<endl;
list1.half();
cout<<list1<<endl;
cout << "----------------------half end--------------------" << endl;
cout << "----------------------meld start--------------------" << endl;
vectorList<double> list3;
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list3<<endl;
list3.meld(list1,list2);
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list3<<endl;
cout << "----------------------meld end--------------------" << endl;
cout << "----------------------merge start--------------------" << endl;
vectorList<double> list4;
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list4<<endl;
list4.merge(list1,list2);
cout<<list1<<endl;
cout<<list2<<endl;
cout<<list4<<endl;
cout << "----------------------merge end--------------------" << endl;
cout << "----------------------split start--------------------" << endl;
vectorList<double> list6;
vectorList<double> list7;
cout<<list2<<endl;
cout<<list6<<endl;
cout<<list7<<endl;
list2.split(list6,list7);
cout<<list2<<endl;
cout<<list6<<endl;
cout<<list7<<endl;
cout << "----------------------split end--------------------" << endl;
return 0;
}
測試輸出
xz@xiaqiu:~/study/algorithm/c++/1/build$ ./test
----------------------insert end--------------------
0 1 2 3 4 5 6 7 8 9
10 11 12 13 14 15 16 17 18 19 20 21
----------------------insert end--------------------
----------------------half start--------------------
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8
0 4 8
----------------------half end--------------------
----------------------meld start--------------------
0 4 8
10 11 12 13 14 15 16 17 18 19 20 21
0 4 8
10 11 12 13 14 15 16 17 18 19 20 21
10 0 11 4 12 8 13 14 15 16 17 18 19 20 21
----------------------meld end--------------------
----------------------merge start--------------------
0 4 8
10 11 12 13 14 15 16 17 18 19 20 21
0 4 8
10 11 12 13 14 15 16 17 18 19 20 21
0 4 8 10 11 12 13 14 15 16 17 18 19 20 21
----------------------merge end--------------------
----------------------split start--------------------
10 11 12 13 14 15 16 17 18 19 20 21
10 11 12 13 14 15 16 17 18 19 20 21
10 12 14 16 18 20
11 13 15 17 19 21
----------------------split end--------------------
xz@xiaqiu:~/study/algorithm/c++/1/build$