vector的文档介绍
使用STL的三个境界:能用,明理,能扩展 ,那么下面学习vector,我们也是按照这个方法去学习
#include
#include
#include
using namespace std;
namespace zsc
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
iterator end()
{
return _finsh;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finsh;
}
template<class InputIterator>
vector(InputIterator frist, InputIterator last)
{
while (frist != last)
{
push_back(frist);
frist++;
}
}
vector(size_t n, const T& val = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(val);
}
}
vector(int n, const T& val = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(val);
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finsh, v._finsh);
std::swap(_endofstorage, v._endofstorage);
}
vector<T>& operator=(vector<T> tmp)
{
swap(tmp);
return *this;
}
size_t size()
{
return _finsh - _start;
}
size_t capacity()
{
return _endofstorage - _start;
}
vector()
{}
~vector()
{
delete[] _start;
_start = _finsh = _endofstorage = nullptr;
}
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
void reserve(size_t n)
{
if (n > capacity())
{
T* tmp = new T[n];
size_t sz = size();
if (_start)
{
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finsh = _start + sz;
_endofstorage = _start + n;
}
}
void resize(size_t n, const T& val = T())
{
if (n <= size())
{
_finsh = _start + n;
}
else
{
reserve(n);
while (_finsh < _start + n)
{
*_finsh = val;
++_finsh;
}
}
}
void insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finsh);
if (_finsh == _endofstorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator end = _finsh - 1;
while (end >= pos)
{
*(end + 1) = *(end);
--end;
}
*pos = x;
++_finsh;
}
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finsh);
iterator it = pos + 1;
while (it < _finsh)
{
*(it - 1) = *it;
++it;
}
--_finsh;
return pos;
}
void push_back(const T& x)
{
if (_finsh == _endofstorage)
{
size_t sz = size();
size_t cp = capacity() == 0 ? 4 : capacity() * 2;
T* tmp = new T[cp];
if (_start)
{
memcpy(tmp, _start, sizeof(T) * sz);
delete[] _start;
}
_start = tmp;
_finsh = _start + sz;
_endofstorage = _start + cp;
}
*_finsh = x;
++_finsh;
}
private:
iterator _start = nullptr;
iterator _finsh = nullptr;
iterator _endofstorage = nullptr;
};
void test1()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
v.push_back(6);
cout << "push_back and pop_back :";
for (auto i : v)
cout << i << " ";
cout << endl;
v.insert(v.begin() + 2, 30);
cout << "insert:";
for (auto x : v)
cout << x << " ";
cout << endl;
v.erase(v.end() - 1);
cout << "erase:";
for (auto x : v)
cout << x << " ";
cout << endl;
v.reserve(20);
cout << "reserve(20): size:" << v.size() << " capacity:" << v.capacity() << endl;
v.resize(100);
cout << "resize(100): size:" << v.size() << " capacity:" << v.capacity() << endl;
}
}
int main()
{
zsc::test1();
return 0;
}