Vector相关接口的实现
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
namespace L
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
//类模板的成员函数,还可以是函数模板
vector(InputIterator first, InputIterator last)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
while (first != last)
{
push_back(*first);
++first;
}
}
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
/*this->reserve(v.capacity());
for (auto e : v)
{
push_back(e);
}*/
_start = new T[v.capacity()];
//memcpy(_start, v._start, sizeof(T)*v.size());//深层次深浅拷贝
/* 总结:T是内置类型,它们增容或者拷贝中,用memcpy没问题
但是T是深拷贝的自定义类型(string),它们增容或拷贝就有问题
vs下,string结构跟g++不同,vs在string本身对象中加了一个buf
字节少存在buf里,字节多用ptr指针指向*/
for (size_t i = 0; i < v.size(); i++)
{
_start[i] = v._start[i];//调用的operator= 复制
}
_endofstorage = _start + v.capacity();
_finish = _start + v.size();
}
//传统写法
/*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());
_endofstorage = _start + v.capacity();
_finish = _start + v.size();
}
return *this;
}*/
void swap(vector<T>&v)
{
::swap(_start, v._start);
::swap(_finish, v._finish);
::swap(_endofstorage, v._endofstorage);
}
//现代写法
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
~vector()
{
if (_start)
{
delete[] _start;
}
_start = _finish = _endofstorage = nullptr;
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin()const
{
return _start;
}
const_iterator end()const
{
return _finish;
}
size_t capacity()const
{
return _endofstorage - _start;
}
size_t size()const
{
return _finish - _start;
}
bool empty()const
{
return _start == _finish;
}
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
void resize(size_t n, T val = T())
{
if (n <size())
{
_finish = _start + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while (_finish < _start + n)
{
*_finish = val;
_finish++;
}
}
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sz*sizeof(T));
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
void push_back(const T&x)
{
if (_finish == _endofstorage)
{
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
}
*_finish = x;
_finish++;
}
void pop_back()
{
assert(!empty);
--_finish;
}
void insert(iterator& pos, const T& x)
{
if (_finish == _endofstorage)
{
size_t len = pos - _start;
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
//更新pos解决增容后失效问题
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
_finish ++;
pos += 1;//解决迭代器失效
}
iterator erase(iterator pos)
{
iterator it = pos + 1;
while (it != _finish)
{
*(it - 1) = *it;
++it;
}
_finish--;
return pos;
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
template <class T>
void PrintVector(const T& v)
{
T ::const_iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
it++;
}
cout << endl;
}
}
指针
使用memcpy拷贝问题
1、memcpy是内存的二进制格式拷贝,将一段内存空间中内容原封不动的拷贝到另外一段内存空间中
2.如果拷贝的是自定义类型的元素,memcpy即高效又不会出错,但如果拷贝的是自定义类型元素,并且自定义类型元素中涉及到资源管理时,就会出错,因为memcpy的拷贝实际是浅拷贝。
结论:如果对象中涉及到资源管理时,千万不能使用memcpy进行对象之间的拷贝,因为memcpy是浅拷贝,否则可能会引起内存泄漏甚至程序崩溃。