vector.h
#pragma once
#include<assert.h>
#include<iostream>
namespace dk
{
template<class T>
class vector
{
public:
//迭代器
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
//构造函数 析构函数
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
template<class InputIterator>
vector(InputIterator first, InputIterator last)
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
reserve(last - first);
while (first != last)
{
push_back(*first);
++first;
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endofstorage, v._endofstorage);
}
//v1(v2)拷贝构造 深拷贝
vector(const vector<T>& v)
: _start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
vector<T> tmp(v.begin(), v.end());
// v是对v2的引用,创建tmp后,开辟空间对v的
//内容进行拷贝,最后交换this(v1的引用)和tmp
//完成拷贝构造
this->swap(tmp);
}
//v1 = v2
vector<T>& operator = (vector<T> v)//v是对v2的拷贝
//所以直接交换,返回*this是v1
{
this->swap(v);
return *this;
}
~vector()
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
// capcacity
size_t capacity()
{
return _endofstorage - _start;
}
size_t size()
{
return _finish - _start;
}
T& operator[] (size_t pos)
{
assert(pos < size());
return _start[pos];//_start 类似于string中的_str ,char*的指针
}
//扩容
void reserve(size_t n)
{
if (n > capacity())
{
size_t sz = size();
T* tmp = new T[n];
//int char 内置类型 调memcpy
//自定义类型如(string)用 for + operator=
//memcpy(tmp, _start, sizeof(T)*size());
for (size_t i = 0; i < sz; ++i)
{
tmp[i] = _start[i];
}
delete[] _start;
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
void resize(size_t n, const T& val = T())
{
if (n > capacity())
{
reserve(n);
}
if (n < size())
{
_finish = _start + n;
}
else
{
while (_finish != _start + n)
{
*_finish = val;
++_finish;
}
}
}
void check_capacity()//判断容量是否够,不够则扩容
{
if (_finish == _endofstorage)
{
size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
reserve(newcapacity);
}
}
//尾插
void push_back(const T& x)
{
check_capacity();
*_finish = x;
++_finish;
}
//尾删
void pop_back()
{
assert(_finish > _start);
--_finish;
}
//任意位置插入
//insert这里pos是实参的拷贝,insert内部pos改变,不能引起
//实参的改变,所以给一个返回值,解决迭代器失效的问题
/*void */iterator insert(iterator pos, const T& x)
{
assert(pos >= _start && pos <= _finish);
size_t posi = pos - _start;
//check_capacity后,若需要增容,则调用reserve
//此时开辟了一个新的空间,pos还留在原来的空间
//所以pos = posi + _start;找到新空间的pos位置
check_capacity();
pos = posi + _start;
iterator end = _finish - 1;
while (end >= pos)
{
*(end - 1) = *end;
--end;
}
*pos = x;
--end;
return pos;//返回值可以解决迭代器失效
}
iterator erase(iterator pos)
{
assert(pos >= _start && pos < _finish);
iterator it = pos + 1;
while (it != _finish)
{
*(it - 1) = *it;
++it;
}
--_finish;
return pos;
}
private:
iterator _start;//指向数据块的开始
iterator _finish;//指向有效数据的尾
iterator _endofstorage;//指向存储容量的尾
};
}
test.c
#include<iostream>
#include<vector>
#include<string>
using namespace std;
#include"vector.h"
//void test_vector1()
//{
// dk::vector<int>v;
// v.push_back(1);
// v.push_back(2);
// v.push_back(3);
// v.push_back(4);
// v.push_back(5);
//
// for (size_t i = 0; i < v.size(); ++i)
// {
// v[i] *= 2;
// cout << v[i] << " ";
// }
// cout << endl;
// for (auto e : v)
// {
// cout << e << " ";
// }
// cout << endl;
//}
void test_vector2()
{
dk::vector<string> v;
v.push_back("1111");
v.push_back("1111");
v.push_back("1111");
v.push_back("1111");
v.push_back("1111");
for (const auto& e : v)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
test_vector2();
return 0;
}