#pragma once
#include<iostream>
#include<cstring>
#include<assert.h>
using namespace std;
namespace RS {
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(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
_start = new T[v.capacity()];
memcpy(_start, v._start, sizeof(T)* v.size());
_finish = _start + v.size();
_endofstorage = _start + v.capacity();
}
~vector() {
if (_start) {
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
}
vector(size_t n,const T& val=T())
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
resize(n, val);
}
vector(int n, const T& val = T())
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
resize(n, val);
}
template<class InputIterator>
vector(InputIterator fist, InputIterator last)
{
while (fist != last) {
push_back(*fist);
fist++;
}
}
iterator begin() {
return _start;
}
iterator end() {
return _finish;
}
const_iterator begin()const {
return _start;
}
const_iterator end()const {
return _finish;
}
void reserve(size_t n) {
if (n > capacity()) {
T* temp = new T[n];
size_t sz = size();
if (_start) {
//memcpy(temp,_start,size(T)*sz);
for (int i = 0; i < sz; i++) {
//T是 string 这样的类 调用的是string的赋值重载 实现string类的深拷贝
temp[i] = _start[i];
}
delete[] _start;
}
_start = temp;
_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;
}
size_t size() const {
return _finish - _start;
}
size_t capacity()const {
return _endofstorage - _start;
}
T& operator[](size_t pos) {
assert(pos < size());
return _start + pos;
}
const T& operator[](size_t pos)const {
assert(pos < size());
return _start + pos;
}
void insert(iterator pos, const T& x) {
//扩容之后pos的位置没发生改变 而原来的vector改变了 所以迭代器失效了 解决了内部迭代器失效
assert(pos >= _start && pos <= _finish);
if (_finish == _endofstorage) {
size_t len = pos - _start;
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos) {
*(end + 1) = *end;
end--;
}
*pos = x;
++_finish;
}
void erase(iterator pos) {
assert(pos >= _start && pos < _finish);
T* move = pos + 1;
while (move < _finish) {
*(move - 1) = *move;
move = move + 1;
}
_finish--;
}
void resize(size_t n,const T& val=T() ) {
//C++对 T() 进行升级可以认为内置类型也有默认构造
if (n < size()) {
_finish = _start + n;
}
else {
reserve(n);
while (_finish != _start + n) {
*_finish= val;
_finish++;
}
}
}
vector<T>& operator=(vector<T>& v) {
}
//void pop_back() {
//erase(--end());
//}
public:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
void test() {
vector<int> v;
for (int i = 1; i < 5; i++)
v.push_back(i);
for (auto e : v) {
cout << e << " ";
}
v.erase(v.end() - 1);
auto it = v.begin();//删最后一个元素迭代器就失效了
cout << endl;
for (auto e : v) {
cout << e << " ";
}
}
void test2() {
vector<int> v;
v.resize(10, 0);
vector<int> v1(v);
for (auto& e : v) {
cout << e << " ";
}
cout << endl;
for (auto& e : v1) {
cout << e << " ";
}
}
void test3() {
vector<string> v;
v.push_back("hello work !!!!!!!!!!!!!!!!!!!!!");
for (auto& e : v) {
cout << e << " ";
}
}
void test4() {
vector<int> v(10, 0);
for (auto& e : v) {
cout << e<<" ";
}
}
}
int main() {
RS::test4();
return 0;
}