vector和list的使用
vector为存储的对象分配一块连续的地址空间,因此对vector中的元素随机访问效率很高。在vecotor中插入或者删除某个元素,需要将现有元素进行复制,移动。如果vector中存储的对象很大,或者构造函数复杂,则在对现有元素进行拷贝时开销较大,因为拷贝对象要调用拷贝构造函数。对于简单的小对象,vector的效率优于list。vector在每次扩张容量的时候,将容量扩展2倍,这样对于小对象来说,效率是很高的。
list中的对象是离散存储的,随机访问某个元素需要遍历list。在list中插入元素,尤其是在首尾插入元素,效率很高,只需要改变元素的指针。
综上所述:
vector适用:对象数量变化少,简单对象,随机访问元素频繁
list适用:对象数量变化大,对象复杂,插入和删除频繁
最大的区别是,list是双向的,而vector是单向的。
list中的对象是离散存储的,随机访问某个元素需要遍历list。在list中插入元素,尤其是在首尾插入元素,效率很高,只需要改变元素的指针。
综上所述:
vector适用:对象数量变化少,简单对象,随机访问元素频繁
list适用:对象数量变化大,对象复杂,插入和删除频繁
最大的区别是,list是双向的,而vector是单向的。
2.模拟实现MyVector
#include<iostream>
#include<vector>
using namespace std;
#include<vector>
using namespace std;
template<class T>
class MyVector
{
public:
typedef T* Iterator;
typedef const T* ConstIterator;
class MyVector
{
public:
typedef T* Iterator;
typedef const T* ConstIterator;
MyVector(size_t n = 3) //构造函数,初始化给3个T类型容量
:_start(new T[n]), _finish(_start), _end_of_storage(_start + n)
{
}
MyVector(const MyVector<T>& v) //拷贝构造
{
if (v._finish - v._start > _finish - _start)
{
delete[] _start;
_start = new T[v._finish - v._start];
_finish = _start;
}
for (Iterator tmp = v._start; tmp != v._finish; tmp++)
{
*(_finish) = *tmp;
_finish++;
}
_end_of_storage = _finish;
}
void PushBack(const T& x)
{
CheckCapacity();
Insert(End(), x);
}
void PopBack()
{
Iterator pos = End();
Erase(--pos);
}
:_start(new T[n]), _finish(_start), _end_of_storage(_start + n)
{
}
MyVector(const MyVector<T>& v) //拷贝构造
{
if (v._finish - v._start > _finish - _start)
{
delete[] _start;
_start = new T[v._finish - v._start];
_finish = _start;
}
for (Iterator tmp = v._start; tmp != v._finish; tmp++)
{
*(_finish) = *tmp;
_finish++;
}
_end_of_storage = _finish;
}
void PushBack(const T& x)
{
CheckCapacity();
Insert(End(), x);
}
void PopBack()
{
Iterator pos = End();
Erase(--pos);
}
void Insert(Iterator pos, const T& x)
{
CheckCapacity();
for (Iterator tmp = End(); tmp != pos; tmp--)
{
*tmp = *(tmp - 1);
}
*pos = x;
_finish++;
}
{
CheckCapacity();
for (Iterator tmp = End(); tmp != pos; tmp--)
{
*tmp = *(tmp - 1);
}
*pos = x;
_finish++;
}
void Erase(Iterator pos)
{
Iterator end = End();
for (Iterator tmp = pos; tmp != (--end); tmp++)
{
*tmp = *(tmp + 1);
}
_finish--;
}
{
Iterator end = End();
for (Iterator tmp = pos; tmp != (--end); tmp++)
{
*tmp = *(tmp + 1);
}
_finish--;
}
size_t Size()
{
return _finish - _start;
}
Iterator Begin()
{
return _start;
}
Iterator End()
{
return _finish;
}
private:
void CheckCapacity()
{
if (_finish == _end_of_storage)
{
Iterator new_start = new T[2 * Size()]; //一次扩容原来的两倍
Iterator new_finish = new_start;
for (Iterator i = Begin(); i != End(); i++, new_finish++)
{
*new_finish = *i;
}
delete[] _start;
_start = new_start;
_finish = new_finish;
_end_of_storage = _start + 2 * Size();
}
}
private:
Iterator _start;
Iterator _finish;
Iterator _end_of_storage;
};
{
return _finish - _start;
}
Iterator Begin()
{
return _start;
}
Iterator End()
{
return _finish;
}
private:
void CheckCapacity()
{
if (_finish == _end_of_storage)
{
Iterator new_start = new T[2 * Size()]; //一次扩容原来的两倍
Iterator new_finish = new_start;
for (Iterator i = Begin(); i != End(); i++, new_finish++)
{
*new_finish = *i;
}
delete[] _start;
_start = new_start;
_finish = new_finish;
_end_of_storage = _start + 2 * Size();
}
}
private:
Iterator _start;
Iterator _finish;
Iterator _end_of_storage;
};
void TestVector()
{
MyVector<int> v;
v.PushBack(1);
v.PushBack(2);
v.PushBack(3);
v.PushBack(4);
MyVector<int>::Iterator it;
for (it = v.Begin(); it != v.End(); it++)
{
cout << *it << " ";
}
cout << endl;
v.PopBack();
v.PopBack();
for (it = v.Begin(); it != v.End(); it++)
{
cout << *it << " ";
}
cout << endl;
MyVector<int> v1(v);
for (it = v1.Begin(); it != v1.End(); it++)
{
cout << *it << " ";
}
cout << endl;
}
3.模拟实现MyList
#include<iostream>
#include<cassert>
using namespace std;
#include<cassert>
using namespace std;
template <class T>
struct LinkNode
{
typedef LinkNode<T> node;
LinkNode(T n = 0)
:_data(n), _prev(0), _next(0)
{}
T _data;
node * _prev;
node * _next;
};
struct LinkNode
{
typedef LinkNode<T> node;
LinkNode(T n = 0)
:_data(n), _prev(0), _next(0)
{}
T _data;
node * _prev;
node * _next;
};
template <class T, class Ptr, class Ref>
class LinkIterator
{
public:
typedef LinkIterator<T, Ptr, Ref> Self;
typedef LinkIterator<T, T*, T&> Iterator;
typedef LinkNode<T> node;
LinkIterator(node* x)
:_node(x)
{}
LinkIterator()
{}
LinkIterator(const Self& it)
{
_node = it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
Ref operator*()
{
return _node->_data;
}
:_node(x)
{}
LinkIterator()
{}
LinkIterator(const Self& it)
{
_node = it._node;
}
bool operator==(const Self& it)
{
return _node == it._node;
}
bool operator!=(const Self& it)
{
return _node != it._node;
}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
{
return &(_node->_data);
}
Self& operator++()
{
_node = _node->_next;
return *this;
}
{
_node = _node->_next;
return *this;
}
Self operator++(int)
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
Self& operator--()
{
_node = _node->_prev;
return *this;
}
{
_node = _node->_prev;
return *this;
}
Self operator--(int)
{
Self tmp(*this);
_node = _node->_prev;
return tmp;
}
node* _node;
{
Self tmp(*this);
_node = _node->_prev;
return tmp;
}
node* _node;
};
template <class T>
class Link
{
public:
template <class T>
class Link
{
public:
typedef LinkIterator<T, T*, T&> Iterator;
typedef Link<T> link;
typedef LinkNode<T> node;
typedef node* node_type;
Link()
:_head(new node(T()))
{
_head->_next = _head;
_head->_prev = _head;
}
~Link()
{
Clear();
delete _head;
_head = NULL;
}
void PushBack(T x) //尾插
{
Insert(End(), x);
}
void PushFront(T x) //头插
{
Insert(Begin(), x);
}
typedef Link<T> link;
typedef LinkNode<T> node;
typedef node* node_type;
Link()
:_head(new node(T()))
{
_head->_next = _head;
_head->_prev = _head;
}
~Link()
{
Clear();
delete _head;
_head = NULL;
}
void PushBack(T x) //尾插
{
Insert(End(), x);
}
void PushFront(T x) //头插
{
Insert(Begin(), x);
}
Iterator Insert(Iterator pos, const T& x) // 在pos前插入值t的元素,返回新添加元素的迭代器
{
node_type NewNode = BuyNewNode(x);
node_type cur = pos._node;
NewNode->_next = cur;
cur->_prev->_next = NewNode;
NewNode->_prev = cur->_prev;
cur->_prev = NewNode;
return Iterator(NewNode);
}
{
node_type NewNode = BuyNewNode(x);
node_type cur = pos._node;
NewNode->_next = cur;
cur->_prev->_next = NewNode;
NewNode->_prev = cur->_prev;
cur->_prev = NewNode;
return Iterator(NewNode);
}
void Insert(Iterator pos, size_t n, const T &t)//在pos前插入n个值为t的元素
{
for (size_t i = 0; i < n; i++)
{
Insert(pos, t);
}
}
void Insert(Iterator pos, Iterator b, Iterator e)//在pos前插入[b,e)范围的元素
{
for (Iterator tmp = b; tmp != e; tmp++)
{
Insert(pos, tmp._node->_data);
}
}
node* BuyNewNode(T x = 0)
{
node*tmp = new node(x);
tmp->_next = tmp;
tmp->_prev = tmp;
return tmp;
}
{
for (size_t i = 0; i < n; i++)
{
Insert(pos, t);
}
}
void Insert(Iterator pos, Iterator b, Iterator e)//在pos前插入[b,e)范围的元素
{
for (Iterator tmp = b; tmp != e; tmp++)
{
Insert(pos, tmp._node->_data);
}
}
node* BuyNewNode(T x = 0)
{
node*tmp = new node(x);
tmp->_next = tmp;
tmp->_prev = tmp;
return tmp;
}
/**********删除相关操作*******************/
Iterator Erase(Iterator it)//删除it所指向的元素,返回所删除元素的下一个元素对应的迭代器
{
assert(it != End());
node_type cur = it._node;
node_type del = cur;
cur = cur->_next;
cur->_prev = del->_prev;
del->_prev->_next = cur;
delete del;
del = NULL;
return Iterator(cur);
}
void Clear()//删除容器内的所有元素
{
node_type cur = _head->_next;
while (cur != _head)
{
node* del = cur;
cur = cur->_next;
delete del;
del = NULL;
}
}
void PopBack()//删除容器内最后一个有效的元素
{
Erase(--End());
}
void PopFront()//删除容器内第一个有效的元素
{
Erase(Begin());
}
/***************访问相关*******************/
Iterator Begin()
{
return Iterator(_head->_next);
}
Iterator Erase(Iterator it)//删除it所指向的元素,返回所删除元素的下一个元素对应的迭代器
{
assert(it != End());
node_type cur = it._node;
node_type del = cur;
cur = cur->_next;
cur->_prev = del->_prev;
del->_prev->_next = cur;
delete del;
del = NULL;
return Iterator(cur);
}
void Clear()//删除容器内的所有元素
{
node_type cur = _head->_next;
while (cur != _head)
{
node* del = cur;
cur = cur->_next;
delete del;
del = NULL;
}
}
void PopBack()//删除容器内最后一个有效的元素
{
Erase(--End());
}
void PopFront()//删除容器内第一个有效的元素
{
Erase(Begin());
}
/***************访问相关*******************/
Iterator Begin()
{
return Iterator(_head->_next);
}
Iterator End()
{
return Iterator(_head);
}
{
return Iterator(_head);
}
T& Front()
{
return _head->_next->_data;
}
{
return _head->_next->_data;
}
T& Back()
{
return _head->_prev->_data;
}
bool Empty() const
{
return _head->_next == _head;
}
{
return _head->_prev->_data;
}
bool Empty() const
{
return _head->_next == _head;
}
size_t Size()
{
size_t count = 0;
for (Iterator it = Begin(); it != End(); it++)
{
count++;
}
return count;
}
{
size_t count = 0;
for (Iterator it = Begin(); it != End(); it++)
{
count++;
}
return count;
}
private:
node_type _head;
};