vector&list

来源:互联网 发布:影响黄金的重要数据 编辑:程序博客网 时间:2024/05/17 03:23

一、vector&list的使用和区别

STL(标准模板库)是一个C++的软件库,也是C++标准程序库的一部分。vector和list相当于容器,这些容器应该都是STL里面的一个类。而vector封装数组,list封装链表。

vector

#include<vector输出>

vector<int>a(10);     //类型为int,数量为10

a[1] = {1};     //第二个元素赋值为1

cout<<a[1]<<endl;    //输出第二个元素

vector<int>::iterator pr = a.begin();     //迭代器指向第一个元素

pr++;

cout<<*pr<<endl;     //输出该元素的值

cout<<a.size()<<endl;     //数组元素数量

a.front();    //数组第一个元素的引用

a.back();    //数组元素最后一个元素的引用

二、模拟实现MyVector

vector可以把它理解为一个顺序表或者数组。只是STL里的vector是由三个迭代器来维护的:_str(数组存放开始的位置),finsh(数据存放结束位置的下一个),_endofstorage(容量的最后一个位置)。vector里的迭代器其实就是指针。

#include<iostream>
#include<vector>
#include<list>
#include<assert.h>
#include<string.h>


using namespace std;


template<class T>
class MyVector
{
public:
MyVector()
:_data(NULL)
,_size(0)
,_capacity(0)
{}
MyVector(const MyVector<T> &s)
{
_data = new T[s._size];
for(size_t i = 0;i < _size;++i)
{
_data[i] = s._data[];
}
_size = s._data;
_capacity = s._capacity;
}
~MyVector()
{
if(_data)
{
delete[] _data;
_data = NULL;
_size = 0;
_capacity = 0;
}
}
void PushBack(const T& data)
{
CheckCapacity();
_data[_size++] = data;
}
void PopBack()
{
assert(_size);
--_size;
}


void Insert(size_t n,const T& data)
{
assert(n < _size1);
CheckCapacity();
T cur = _size - 1;
for(size_t i = n;i < _size;++i)
{
_data[cur+1] = _data[cur];
cur--;
}
_data[n] = data;
++_size;
}
void Erase(size_t n)
{
assert(n < _size);
for(size_t i = n-1;i < _size;++i)
{
_data[i] = _data[i+1];
}
--_size;
}
void CheckCapacity()
{
if(_size == _capacity)
{
size_t NewCapacity = _capacity*2+3;
T* pData = new T[NewCapacity];
for(size_t i= 0;i < _size;++i)
{
pData[i] = _data[i];
}
delete[] _data;
_data = pData;
_capacity = NewCapacity;
}
}
void Print()
{
assert(_size);
for(size_t i = 0;i < _size;++i)
{
cout <<_data[]<< " ";
}
cout<<endl;
}
void clear()
{
_size = 0;
}
void Swap(MyVector<T> v)
{
swap(_data,v._data);
swap(_size,v._size);
swap(_capacity,v._capacity);
}
T& operator[](size_t pos)
{
assert(pos<_size);
return _data[pos];
}
size_t Size()
{
return _size;
}
size_t Capacity()
{
return _capacity
}
bool Empty()
{
return (_size == 0);
}
private:
T* _data;
size_t _size;
size_t _capacity;
};


lise:特点

双向链表。

只支持双向顺序访问。

在list中任何位置进行插入/删除操作速度都很快。

代码如下:

#include<iostream>
#include<vector>
#include<list>
#include<assert.h>
using namespace std;
template<class T>
struct ListNode
{
ListNode(const T& data = T())
:_pre(NULL)
,_next(NULL)
,_data(data)
{}
ListNode<T>* _pre;
ListNode<T>* _next;
T _data;
};
template<class T,class Ref>
class ListIterator
{
template<class T>
friend class MyList;
typedef ListIterator<T,Ref>self;
public:
ListIterator()
:_pNode(NULL)
{}
ListIterator(ListNode<T>* pNode)
:_pNode
{}
Ref operator*()
{
return _pNode->_data;
}
self& operator++()
{
_pNode = _pNode->_next;
return *this;
}
self operator++(int)
{
self tmp(*this);
_pNode = _pNode->_next;
return tmp;
}
self operator--()
{
_pNode = _pNode->_pre;
return *this;
}
self& operator--(int)
{
self tmp(*this);
_pNode = _pNode->_pre;
return tmp;
}
bool operator == (const self& s)
{
return _pNode == s._pNode;
}
bool operator != (const self& s)
{
return _pNode != s._pNode;
}
private:
ListNode<T>* _pNode;
};
template<class T>
class MyList
{
public:
typedef ListIterator<T,T&>Iterator;
public:
Iterator begin()
{
return Iterator(_pHead->_next);
}
Iterator end()
{
return Iterator(_pHead);
}
MyList()
{
CreateHead();
}
MyList(size_t n,const T& data)
{
CreateHead();
for(size_t = i;i < n;i++)
{
PushBack(data);
}
}
~MyList()
{
Clear();
delete _pHead;
_pHead = NULL:
}
void Clear()
{
ListNode<T>* pCur = _pHead->_next;
ListNode<T>* pPre = NULL;
while(pCur != _pHead)
{
pPre = pCur;
pCur = pCur->_next;
delete pPre;
}
_pHead->_next = _pHead;
_pHead->_pre = _pHead;
}
void PopBack()
{
if(_pHead == _pHead->_next)
{
assert(flase);
return;
}
ListNode<T>* pTmp = _pHead->_pre;
pTmp->_pre->_next = _pHead;
_pHead->_pre = pTmp->_pre;
delete pTmp;
}
void PushBack(const T& data)
{
ListNode<T>* pTail= _pHead->_next;
ListNode<T>* pTmp = new ListNode<T>(data);
pTail->_next = pTmp;
pTmp->_next = _pHead;
pTmp->_pre = pTail;
_pHead->_pre = pTmp;
}
void PushFront(const T& data)
{
ListNode<T>* pTmp = _pHead->_next;
ListNode<T>* pNew = new ListNode<T>(data);
_pHead->_next = pNew;
pNew->_next = pTmp;
pTmp->_pre = pNew;
pNew->_pre = _pHead;
}
void popfront()
{
ListNode<T>* pTmp = _pHead->_next;
_pHead->_next = pTmp->_next;
pTmp->_next->_pre = _pHead;
delete pTmp;
}
void Insert(const Iterator position,const T& data)
{
ListNode<T>* pTmp = _pHead->_next;
while(pTmp != _pHead)
{
if(pTmp->_data == data)
break;
pTmp = pTmp->_next;
}
ListNode<T>* pNew = new ListNode<T>(data);
pNew->_next = pTmp->_next;
pNew->_pre = pTmp;
pTmp->_next = pNew;
}
void Erase(const Iterator position)
{
ListNode<T>* pCur = _pHead->_next;
while(pCur != _pHead)
{
if(pCur == position._pNode)
{
break;
}
pCur = pCur->_next;
}
pCur->_pre->_next = pCur->_next;
pCur->_next->_pre = pCur->_pre;
delete pCur;
}
bool Empty()
{
return _pHead->_next == _pHead;
}
size_t Size() const
{
size_t count = 0;
ListNode<T>* pCur = _pHead->_next;
while(pCur != _pHead)
{
pCur = pCur->_next;
count++;
}
return count;
}
void Print()
{
ListNode<T>*pTmp = _pHead->_next;
while(pTmp != _pHead)
{
cout<<pTmp->_data<<;
pTmp = pTmp->_next;
}
cout<<endl;
}
protected:
void CreateHead()
{
_pHead = new ListNode<T>;
_pHead->_next = _pHead;
_pHead->_pre = _pHead;
}




private:
ListNode<T>* _pHead;
};












原创粉丝点击