您的位置:首页 > 编程语言 > C语言/C++

【c++】模板实现vector和list

2017-03-02 13:31 525 查看

前面我们也知道模板是泛型编程的基础,是实现类型无关的代码,是一种代码复用的重要手段。

一:模板实现Vector:

.h文件
#pragma once
#include<iostream>
#include<assert.h>
#include<string>
using namespace std;
template<typename T>
class Vector
{
public:
//构造
Vector()
:_data(NULL)
, _size(0)
, _capacity(0)
{}
//拷贝构造
Vector(const Vector<T>&v)
{
_data = new T[v._size];
//memcpy
//memcpy(_data, v._data,_size+1)
for (size_t i = 0; i < v._size; ++i)
{
_data[i] = v._data[i];
}
_size = v._size;
_capacity = v._capacity;
}
//赋值运算符的重载
Vector<T>&operator = (Vector<T>&v)
{
swap(this->_data, v._data);
swap(this->_size, v._size);
swap(this->_capacity, v._capacity);
return *this;
}
//析构函数
~Vector()
{
if (_data)
{
delete[] _data;
_data = NULL;
}
_size = _capacity = 0;
}
size_t  Size()
{
return _size ;
}
int&Back()
{
return _data[_size - 1];
}
void Print()
{
for (size_t i = 0; i < _size; ++i)
{
cout << _data[i] << " ";
}
cout << endl;
}
void PushBack(const T&x)
{
_checkcapacity();
Insert(_size, x);
}
void PopBack()
{
Erase(_size);
}
void PushFront(const T&x)
{
_checkcapacity();
for (size_t i = _size; i > 0; --i)
{
_data[i] = _data[i-1];
}
_data[0] = x;
++_size;
}
void PopFront()
{
for (size_t i =0; i < _size; ++i)
{
_data[i] = _data[i+1];
}
--_size;
}
//实现随机位置的插入
void Insert(size_t pos, const T&x)
{
assert(pos >= 0 && pos <= _size);
_checkcapacity();

for (size_t i = pos; i < _size; ++i)
{
_data[pos + 1] = _data[pos];
}
_data[pos] = x;
++_size;
}
//实现随机位置的删除
void Erase(size_t pos)
{
assert(pos);

size_t end = _size;
while (end!= pos)
{
_data[pos] = _data[pos + 1];
--end;
}
--_size;
}
protected:
void _checkcapacity()
{
if (_size >= _capacity)
{
size_t _newcapacity = _capacity * 2 + 3;
T*tmp = new T[_newcapacity];
if (_data)
{
memcpy(tmp, _data, sizeof(T)*_size);
/*for (size_t i = 0; i < _size; ++i)
{
tmp[i] = _data[i];
}*/
delete _data;
}
_data = tmp;
_capacity = _newcapacity;
}
}
protected:
T* _data;
size_t _capacity;
size_t _size;
};
void TestVector()
{
Vector<int> v;
v.PushFront(1);
v.PushFront(2);
v.PushFront(3);
v.Print();

v.PopBack();
v.PopBack();
v.PopBack();
v.Print();

v.PushFront(4);
v.PushFront(5);
v.PushFront(6);
v.Print();

v.PopFront();
v.PopBack();
v.PopBack();
v.Print();

Vector<string> v2;
v2.PushBack("V1111111111111111111111111111111111111");
v2.Print();
}

 容器适配器 允许 模板参数 是模板类的类型

//适配器
template <typename T,template<class> class Containter =Vector>
class Stack
{
public:
void Push(const T&x)//尾插
{
_con.PushBack(x);
}
void Pop()//尾删
{
_con.PopBack();
}
T&Top()//返回栈顶
{
return _con.Back();
}
T&Top() const
{
return _con.Back();
}
bool Empty()//是否为空
{
return _con.Size() == 0;
}
bool Empty() const
{
return _con.Size() == 0;
}
size_t Size() const //堆栈大小
{
return _con.Size();
}
protected:
Containter <T>_con;
};

void TestStack()
{
Stack<int,Vector> s;
s.Push(7);
s.Push(8);
s.Push(9);

while (!s.Empty())
{
cout << s.Top() << " ";
s.Pop();
}
cout << endl;
}

二:模板实现list:

list.h文件:
#pragma once
#include<iostream>

#include<assert.h>
using namespace std;
//定义一个结构体
template<class T>
struct ListNode
{
T _data;
ListNode<T>* _next;
ListNode<T>* _prev;

ListNode(const T&x = T())
:_data(x)
,_prev(NULL)
,_next(NULL)
{}
};
//带有头结点的双向循环链表
template<class T>
class List
{
typedef ListNode<T>  Node;
public:
List()
{
_head = new Node;
_head->_next = _head;
_head->_prev = _head;
}
List(const List<T>&l)
{
_head = new Node();
_head->_next = _head;
_head->_prev = _head;

Node* cur = (l._head)->_next;
while (cur != l._head)
{
this->PushBack(cur->_data);
cur = cur->_next;
}
}
List<T> & operator=(List<T>l)
{
swap(_head, l._head);
return *this;
}
~List()
{
Clear();
delete _head;
_head = NULL;
}
void Clear()
{
Node* cur = _head->_next;
while (cur != _head)
{
Node* next = cur->_next;
delete cur;
cur = next;
}

_head->_next = _head;
_head->_prev = _head;
}
void PushBack(const T&x)
{
Insert(_head, x);
}
void PopBack()
{
Erase(_head->_prev);
}
void PushFront(const T&x)
{
Insert(_head->_next,x);
}
void PopFront()
{
Erase(_head->_next);
}
void Insert(Node *pos, const T&x)
{
assert(pos);

Node* tmp = new Node(x);
Node* cur = pos;
Node* prev = cur->_prev;

prev->_next = tmp;
tmp->_prev = prev;

tmp->_next = cur;
cur->_prev = tmp;
}
void Erase(Node*pos)
{
assert(pos&&pos != _head);
Node *prev = pos->_prev;
Node*next = pos->_next;

prev->_next = next;
next->_prev = prev;
delete pos;
}
T&Back()//返回队尾
{
assert(_head->_next != _head);
return _head->_prev->_data;
}
T&Front()//返回队头
{
assert(_head->_next != _head);
return _head->_next->_data;
}
size_t Size()//队列元素的个数
{
size_t count = 0;
Node*cur = _head->_next;
while (cur!=_head)
{
++count;
cur = cur->_next;
}
return count;
}

void Print()
{
Node* cur = _head->_next;
while (cur != _head)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
protected:
Node *_head;
};

void TestList()
{
List<int> l;
l.PushBack(1);
l.PushBack(2);
l.PushBack(3);
l.Print();

l.PopBack();
l.PopBack();
l.PopBack();
l.Print();

l.PushFront(4);
l.PushFront(5);
l.PushFront(6);
l.Print();
}


 容器适配器 允许 模板参数 是模板类的类型

//适配器
template < class T,class Container = List<T>>
//队列的操作在尾部插入,在头部删除
class Queue
{
public:
void Push(const T& x)//尾插
{
_con.PushBack(x);
}
void Pop()
{
_con.PopFront();
}
T& Front()
{
return _con.Front();
}
T& Back()
{
_con.Back();
}
bool Empty()
{
return _con.Size() == 0;
}
size_t Size()
{
return _con.Size();
}
protected:
Container _con;
};

void TestQueue()
{
Queue<int> q;
q.Push(7);
q.Push(8);
q.Push(9);
while (!q.Empty())
{
cout << q.Front() << " ";
q.Pop();
}
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: