#include <cstdio>
#include <memory>
#include <utility>
template<typename T>
class myVector
{
public:
// 默认构造函数,创建一个大小为 0 的 vector
myVector() : _start(nullptr), _finish(nullptr), _endOfStorage(nullptr) {}
// 创建一个大小为 n,默认值为 T 的 vector
myVector(size_t n, const T& value = T())
: _start(nullptr), _finish(nullptr), _endOfStorage(nullptr) {
reserve(n);
while (n--) {
push_back(value);
}
}
~myVector() {
if (_start) {
delete[] _start;
_start = nullptr;
_finish = nullptr;
_endOfStorage = nullptr;
}
}
void reserve(size_t n); // 申请空间
void resize(size_t n, const T& value = T()); // 重新设置 vector 大小
void push_back(T &&value); // 插入一个右值
void push_back(const T& value); // 插入一个变量
// vector 总的可用空间
size_t capacity() const { return _endOfStorage - _start; }
size_t size() const { return _finish - _start; }
// 重载 [] 符号
T& operator[](size_t pos) { return _start[pos]; }
myVector<T>& operator=(const myVector<T>& v);
private:
T* _start; // 数组首地址
T* _finish; // 最后一个元素的下一个地址
T* _endOfStorage; // 空间的尾地址
};
template<typename T>
inline void myVector<T>::reserve(size_t n)
{
if (n > capacity()) {
size_t sz = size();
T* arr = new T[n];
if (_start) {
memcpy(arr, _start, sizeof(T) * sz);
delete[] _start;
}
_start = arr;
_finish = _start + sz;
_endOfStorage = _start + n;
}
}
template<typename T>
inline void myVector<T>::resize(size_t n, const T &value)
{
if (n > capacity())
reserve(n);
if (n > size()) {
while (_finish != _start + n) {
*_finish = value;
++_finish;
}
}
_finish = _start + n;
}
template<typename T>
inline void myVector<T>::push_back(T&& value)
{
if (_finish == _endOfStorage) {
size_t sz = _endOfStorage == nullptr ? 1 : 2 * capacity();
reserve(sz);
}
*_finish = value;
++_finish;
}
template<typename T>
inline void myVector<T>::push_back(const T &value)
{
if (_finish == _endOfStorage) {
size_t sz = _endOfStorage == nullptr ? 1 : 2 * capacity();
reserve(sz);
}
*_finish = value;
++_finish;
}
template<typename T>
inline myVector<T>& myVector<T>::operator=(const myVector<T>& v)
{
if (this != &v) {
delete[] _start;
size_t n = v.capacity();
size_t sz = v.size();
_start = new T[n];
memcpy(_start, v._start, sizeof(T) * sz);
_finish = _start + sz;
_endOfStorage = _start + n;
}
return *this;
}
测试:
int main()
{
int a = 2;
myVector vec(10, a);
for (int j = 0; j < 10; ++j) {
vec.push_back(j);
}
myVector<int> vec1;
vec1 = vec;
for (unsigned i = 0; i < vec1.size(); ++i) {
cout << vec1[i] << endl;
}
return 0;
}