C++STL:模拟实现STL库中vector类
vector的模拟实现
c++代码
去博客设置页面,选择一款你喜欢的代码片高亮样式,下面展示同样高亮的 代码片
.
#pragma onece
namespace mrr
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
/*vector(const vector<T>& v)
{
_start = new T[v.capacity()];
_finish = _start;
_endofstorage = _start + v.capacity();
for (size_t i = 0; i < v.size(); i++)
{
*_finish = v[i];
++_finish;
}
}*/
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(v.capacity());//避免push_back开空间的消耗
for (const auto& e : v)
push_back(e);
}
/*vector<T>& operator=(const vector<T>& v)
{
if (this != &v)
{
delete[] _start;
_start = new T[v.capacity];
memcpy(_start, v._start, sizeof(T)*v.size());
}
return *this;
}*/
//swap(v1,v2)三次深拷贝 使用v1.swap(v2)
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
void swap(vector<T>& v)
{
::swap(_start, v._start);
::swap(_finish, v._finish);
::swap(_endofstorage, v._endofstorage);
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
iterator begin()
{
return _start;
}
const_iterator begin() const
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator end() const
{
return _finish;
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
//按字节拷贝浅拷贝 指针指向同一块空间,释放两次会出现问题
//memcpy(tmp, _start, sizeof(T)*sz);
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];//调用的是T的operator= 深拷贝
}
delete[] _start;
}
_start = tmp;
_finish = tmp + sz;
_endofstorage = tmp + n;
}
}
void resize(size_t n, const T& val = T())//给一个T类型缺省值
{
if ( n < size())
{
_finish = _start + n;
}
else
{
if ( n > capacity())
{
reserve(n);
}
while (_finish < _start + n)
{
*_finish = val;
++_finish;
}
}
}
void push_back(const T& x)
{
/*if (_finish == _endofstorage)
{
size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
reserve(newcapacity);
}
*_finish = x;
++_finish;*/
insert(_finish, x);
}
void pop_back()
{
/*assert(_start < _finish);
--_finish;*/
erase(_finish-1);
}
void insert(iterator pos, const T& x)
{
assert(pos <= _finish);
if (_finish == _endofstorage)
{
size_t n = pos - _start;
size_t newcapacity = capacity() == 0 ? 2 : capacity() * 2;
reserve(newcapacity);
// 增容原来pos失效 为了处理迭代器失效pos指向旧空间的问题
pos = _start + n;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
}
iterator erase(iterator pos)
{
assert(pos < _finish);
iterator it = pos;
while (it < _finish)
{
*it = *(it + 1);
++it;
}
--_finish;
return pos;
}
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
const T& operator[](size_t i) const
{
assert(i < size());
return _start[i];
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
void print_vector(const vector<int>& v)
{
vector<int>::const_iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
}
void test_vector1()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(6);
v.push_back(7);
v.insert(v.begin(), 0);
cout << v.size() << endl;
cout << v.capacity() << endl;
print_vector(v);
vector<int>::iterator it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
it=v.erase(it);
else
++it;
}
/*vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;*/
for (auto& e : v)
{
cout << e << " ";
}
cout << endl;
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
}