vector类的成员通常定义为三个:
start:指向有效位置的起始位置(size的第一位)
finish:指向有效位置的最后一位(size的最后一位)
end_of_storage:指向空间最后一位(相当于capacity)
vector与string类不同的在于:
string类能够存放处理的数据类型有限;
vector则能存放处理各种数据类型。
这里则运用了之前写过的泛型编程的概念。通过指定参数的类型来存放处理不同类型的数据。
以下代码示例:
using namespace std;
//*************************************************************************************vector实现
template<class T>
class Vector
{
public:
//迭代器类型 T*
typedef T* iterator;
typedef const T* const_iterator;
//无参构造函数
Vector()
:_start(nullptr)
, _finish(nullptr)
, _endOfStorage(nullptr)
{}
//带参构造函数(缺省值是指定类型的内置类型0值)
Vector(size_t n, const T& val = T())
:_start(new T[n])
, _finish(_start + n)
, _endOfStorage(_start + n)
{
for (size_t i = 0; i < n; ++i)
_start[i] = val;
}
template <class inputIterator>
Vector(inputIterator first, inputIterator last)
:_start(nullptr)
, _finish(nullptr)
, _endOfStorage(nullptr)
{
//[first, last)
while (first != last)
{
pushBack(*first);
++first;
}
}
void pushBack(const T& val)
{
//检查容量
if (_finish == _endOfStorage)
{
//增容
size_t newC = _endOfStorage == nullptr ? 1 : 2 * capacity();
reserve(newC);
}
//插入
*_finish = val;
//更新size
++_finish;
}
void reserve(size_t n)
{
if (n > capacity())
{
//保存原始的size
size_t sz = size();
//开空间
T* arr = new T[n];
//拷贝内容
if (_start)
{
//自定义类型,浅拷贝:如果有资源,会产生资源被多次释放
//memcpy(arr, _start, sizeof(T) * sz);
//深拷贝
for (size_t i = 0; i < sz; ++i)
{
//如果是自定义类型,调用赋值运算符重载函数
//只要赋值运算符实现了深拷贝,此处即为深拷贝
arr[i] = _start[i];
}
delete[] _start;
}
//更新
_start = arr;
_finish = _start + sz;
_endOfStorage = _start + n;
}
}
void resize(size_t n, const T& val = T())
{
// n > capacity:增容
if (n > capacity())
{
reserve(n);
}
//size <= n <= capacity:[size, n-1] 填充val
if (n > size)
{
while (_finish != _start + n)
{
*_finish = val;
++_finish;
}
}
// n <= size 只修改size
_finish = _start + n;
}
void insert(iterator pos, const T& val = T())
{
//0.检查位置
if (pos >= _start && pos <= _finish)
{
//1.检查容量
if (_finish == _endOfStorage)
{
//保存元素的个数
size_t num = pos - _start;
//增容会导致迭代器失效
size_t n = _endOfStorage == nullptr ? 1 : 2 * capacity();
reserve(n);
//更新迭代器
pos = _start + num;
}
//2.移动元素:从后向前移动
iterator end = _finish;
while (end != pos)
{
*end = *(end - 1);
--end;
}
//3.插入
*pos = val;
//4.更新
++_finish;
}
}
//删除元素(防止迭代器失效问题)
iterator erase(iterator pos)
{
//检查位置有效性
if (pos < _finish && pos >= _start)
{
//移动元素:从前向后移动
iterator start = pos + 1;
while (start != _finish)
{
*(strat - 1) = *strat;
++start;
}
//更新
--_finish;
}
return pos;
}
void popBack()
{
erase(end() - 1);
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endOfStorage - _start;
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
T& operator[](size_t pos)
{
if (pos >= 0 && pos < size())
return _start[pos];
}
const T& operator[](size_t pos) const
{
if (pos >= 0 && pos < size())
return _start[pos];
}
/*Vector<T>& operator=(const Vector<T>& v)
{
if (this != &v)
{
delete[] _start;
size_t n = v.capacity();
_start = new T[n];
for (size_t i = 0; i < v.size(); ++i)
{
_start[i] = v[i];
}
_finish = _start + v.size();
_endOfStorage = _start + n;
}
return *this;
}*/
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(const Vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endOfStorage(nullptr)
{
size_t n = v.capacity();
_start = new T[n];
for (size_t i = 0; i < v.size(); ++i)
{
_start[i] = v[i];
}
_finish = _start + v.size();
_endOfStorage = _start + n;
}
~Vector()
{
delete[] _start;
_start = _finish = _endOfStorage = nullptr;
}
private:
iterator _start;
iterator _finish;
iterator _endOfStorage;
};
template <class T>
void printVector(const Vector<T>& vec)
{
Vector<T>::const_iterator cit = vec.begin();
while (cit != vec.end)
{
cout << *cit << " ";
++cit;
}
cout << endl;
}
template <class T>
void printForVector(const Vector<T>& vec)
{
for (const auto& e : vec)
{
cout << e << " ";
}
cout << endl;
}
template <class T>
ostream operator<<(ostream& out, const Vector<T>& v)
{
for (size_t i = 0; i < v.size(); ++I)
{
out << v[i] << " ";
}
out << endl;
}