#include<iostream>
#include<assert.h>
using namespace std;
namespace king {
template<class T>
class vector {
public:
typedef T* iterator;
vector()
: _start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{}
vector(vector<T>& v)
: _start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
_start = new T[v.capacity()];
for (int i = 0; i < v.size(); i++) {
_start[i] = v._start[i];
}
_finish = _start + v.size();
_end_of_storage = _start + v.capacity();
}
void swap(vector<T>& v) {
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_end_of_storage, v._end_of_storage);
}
vector<T>& operator=(vector<T> v) {
swap(v);
return *this;
}
~vector() {
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
iterator begin() {
return _start;
}
iterator end() {
return _finish;
}
T& operator[](size_t pos) {
assert(pos < size());
return _start + pos;
}
vector(size_t n, const T& val) {
resize(n, val);
}
void reserve(size_t n) {
if (n > capacity()) {
T* temp = new T[n];
if (_start) {
for (int i = 0; i < size(); i++) {
temp[i] = _start[i];
}
delete[] _start;
}
_finish = temp + size();
_start = temp;
_end_of_storage = _start + n;
}
}
void push_back(const T& x) {
if (_finish == _end_of_storage) {
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
}
*_finish = x;
_finish++;
}
size_t capacity()const {
return _end_of_storage - _start;
}
size_t size()const {
return _finish - _start;
}
template<class InputIterator>
vector( InputIterator first, InputIterator last) {
while (first != last) {
push_back(*first);
++first;
}
}
iterator insert(iterator pos, const T& x) {//外部的迭代器失效还解决不了 所以用的时候要注意了哦!
assert(pos >= _start && pos <= _finish);
if (_finish == _end_of_storage) {//扩容了要记录pos的相对位置,解决了迭代器失效的问题
size_t len = pos - _start;//记录pos 的相对位置
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
pos = _start + len;//通过len获得扩容后的pos的位置
}
iterator end = _finish - 1;
while (end >= pos) {
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
return pos;
}
void erase(iterator pos) {//erase以后迭代器失效了 不能访问
//vs做了强制检查,访问会直接报错
assert(pos >= _start && pos <= _finish);
iterator it = pos + 1;
while (it != _finish) {
*(it - 1) = *it;
it++;
}
--_finish;
}
void pos_back() {
erase(--end());
}
void resize(size_t n, const T& val = T())
{
if (n < size())
{
_finish = _start + n;
}
else {
reserve(n);
while (_finish != _start + n) {
*_finish = val;
++_finish;
}
}
}
private:
iterator _start;
iterator _finish;
iterator _end_of_storage;
};
void test() {
vector< string>v1;
v1.push_back("string");
v1.push_back("string");
v1.push_back("string");
v1.push_back("string");
vector<string>::iterator it = v1.begin();
vector<string> v(v1.begin(),v1.end());
for (auto& e : v) {
cout << e << " ";
}
// /*v.push_back(1);
// v.push_back(2);
// v.push_back(3);
// v.push_back(4);
// v.insert(v.end(),5);
// v.erase(v.begin());
// vector<int>::iterator it =v.begin();
// while (it != v.end()) {
// cout << ++( *it) << " ";
// it++;
// }
//}*/
/*v.push_back("11199222222222222222222222222222221");
v.push_back("1111112222222222222222222222222222222");
v.push_back("111512222222222222222222222222222221");
v.push_back("11222222222222222222222222222");
v.push_back("222222222222222222222222222222222222");
vector<string> v1(v);
vector<string> v2;
v2 = v1;
vector<string>::iterator it = v2.begin();
while (it != v2.end()) {
cout <<(*it) << " ";
it++;
}*/
}
}
int main() {
king::test();
}