#define _CRT_SECURE_NO_WARNINGS
#pragma once
#include <iostream>
#include <assert.h>
using namespace std;
struct _TrueType // 实现类型萃取
{
bool Get()
{
return true;
}
};
struct _FalseType
{
bool Get()
{
return false;
}
};
template <class T>
struct TypeTraits
{
typedef _FalseType __IsPODType;
};
template <>
struct TypeTraits<int>
{
typedef _TrueType __IsPODType;
};
template<>
struct TypeTraits<char>
{
typedef _TrueType __IsPODType;
};
template <class T> // 简单迭代器
class VectorIterator
{
public:
VectorIterator(T* ptr = NULL) :
_pPtr(ptr)
{}
VectorIterator(const VectorIterator& d) :
_pPtr(d._pPtr)
{}
T& operator*()
{
return *_pPtr;
}
VectorIterator& operator++()
{
++_pPtr;
return *this;
}
VectorIterator operator++(int)
{
VectorIterator temp(*this);
++_pPtr;
return temp;
}
VectorIterator& operator--()
{
--_pPtr;
return *this;
}
VectorIterator operator--(int)
{
VectorIterator temp(*this);
--_pPtr;
return temp;
}
bool operator==(const VectorIterator& v)
{
return v._pPtr == _pPtr;
}
bool operator!=(const VectorIterator& v)
{
return v._pPtr != _pPtr;
}
private:
T* _pPtr;
};
template<class T>
class Vector
{
public:
typedef VectorIterator<T> Iterator;
Vector()
:_start(new T[3])
, _finish(_start)
, _endOfStorage(_start + 3)
{}
Vector(const T* array, size_t size)
:_start(new T[size])
, _finish(_start + size)
, _endOfStorage(_start + size)
{
if (TypeTraits<T>::__IsPODType().Get()) //判断是否为内置类型 如果是 使用memcpy效率高 不是 使用for循环
memcpy(_start, array, size*sizeof(T));
else
{
for (size_t i = 0; i < size; ++i)
_start[i] = array[i];
}
}
Vector(const Vector& v)
:_start(new T[v.Capacity()])
, _finish(_start + v.Size())
, _endOfStorage(_start + v.Capacity())
{
if (TypeTraits<T>::__IsPODType().Get())
memcpy(_start, v._start, v.Size()*sizeof(T));
else
for (size_t i = 0; i < size; ++i)
_start[i] = v._start[i];
}
Vector& operator=(const Vector& s)
{
if (this != &s)
{
if (_start)
delete[] _start;
_start = new T[s.Capacity()];
_finish = _start + s.Size();
_endOfStorage = _start + s.Capacity();
if (TypeTraits<T>::__IsPODType().Get())
memcpy(_start, s._start, s.Size()*sizeof(T));
else
for (size_t i = 0; i < s.Size(); ++i)
_start[i] = s._start[i];
}
}
~Vector()
{
if (_start)
{
delete[] _start;
_start = NULL;
_finish = NULL;
_endOfStorage = NULL;
}
}
Iterator Begin()
{
return _start;
}
Iterator End()
{
return _finish;
}
///Modify
void PushBack(const T& data)
{
_CheckCapacity();
*_finish = data;
++_finish;
}
void PopBack()
{
if (!Empty())
{
--_finish;
}
}
void Insert(size_t pos, const T& data)
{
_CheckCapacity();
assert(pos >= 0 && pos < Size());
if (TypeTraits<T>::__IsPODType().Get())
memmove(_start + pos, _start + pos - 1, (Size() - pos + 1)*sizeof(T));
else
for (size_t i = Size(); i > pos; --i)
{
_start[i] = _start[i - 1];
}
_start[pos] = data;
_finish++;
}
void Erase(size_t pos)
{
assert(pos >= 0 && pos < Size());
if (TypeTraits<T>::__IsPODType().Get())
memmove(_start + pos - 1, _start + pos , (Size() - pos)*sizeof(T));
else
for (size_t i = pos; i < Size() - 1; ++i)
_start[i] = _start[i + 1];
_finish--;
}
//capacity
size_t Size()const
{
return _finish - _start;
}
size_t Capacity()const
{
return _endOfStorage - _start;
}
bool Empty()const
{
return _start == _finish;
}
void Resize(size_t newSize, const T& data = T())
{
if (newSize < Size())
{
_finish = _start + newSize;
}
else if (newSize > Size() && newSize < Capacity())
{
while (_finish < _start + newSize)
*_finish++ = data;
}
else
{
T* temp = new T[newSize];
if (TypeTraits<T>::__IsPODType().Get())
{
memcpy(temp, _start, Size()*sizeof(T));
}
else
{
for (size_t i = 0; i < Size(); ++i)
temp[i] = _start[i];
}
if (TypeTraits<T>::__IsPODType().Get())
{
memcpy(temp + Size(), _start, newSize*sizeof(T));
}
else
{
for (size_t i = Size(); i < newSize; ++i)
temp[i] = data;
}
delete[] _start;
_start = temp;
_finish = _start + newSize;
_endOfStorage = _finish;
}
}
//Acess///
T& operator[](size_t index)
{
assert(_start);
return _start[index];
}
const T& operator[](size_t index)const
{
assert(_start);
return _start[index];
}
T& Front()
{
assert(_start);
return *_start;
}
const T& Front()const
{
assert(_start);
return *_start;
}
T& Back()
{
assert(_start);
return _start[Size() - 1];
}
const T& Back()const
{
assert(_start);
return _start[Size() - 1];
}
void Clear()
{
if (_start)
_finish = _start;
}
friend ostream& operator<<(ostream& _cout, const Vector<T>& d)
{
for (size_t i = 0; i < d.Size(); ++i)
{
_cout << d._start[i] << " ";
}
return _cout;
}
private:
void _CheckCapacity()
{
if (_finish == _endOfStorage)
{
T* temp = new T[Capacity() * 2 + 3];
for (size_t i = 0; i < Size(); ++i)
temp[i] = _start[i];
_finish = temp + Size();
_endOfStorage = temp + Capacity() * 2 + 3;
delete[] _start;
_start = temp;
}
}
private:
T* _start;
T* _finish;
T* _endOfStorage;
};
void Test()
{
Vector<int> v;
v.PushBack(1);
v.PushBack(2);
v.PushBack(3);
v.PushBack(4);
v.PushBack(5);
v.PushBack(6);
cout << v << endl;
cout << *(v.Begin())<< endl;
cout << *(--v.End()) << endl;
}
void TestVector()
{
int arr[5] = { 1, 2, 3, 4, 5 };
Vector<int> v(arr, sizeof(arr) / sizeof(arr[0]));
cout << v << endl;
cout << v.Size() << endl;
v.PushBack(6);
v.PushBack(7);
cout << v.Size() << endl;
cout << v.Capacity() << endl;
v.PopBack();
v.PopBack();
v.PopBack();
cout << v << endl;
cout << v.Size() << endl;
cout << v.Capacity() << endl;
v.Resize(10);
cout << v.Size() << endl;
cout << v.Capacity() << endl;
v[1] = 0;
cout << v.Back() << endl;
cout << v.Front() << endl;
cout << v << endl;
v.Insert(9, 9);
cout << v << endl;
v.Erase(10);
cout << v << endl;
}
Vector 简单模拟实现
最新推荐文章于 2022-09-23 11:55:58 发布