vector作为STL成员之一,在实际中也使用广泛。所有了解实现一个简单的vector也有助于我们更好的认识vector及其底层实现。
#pragma once
#include<iostream>
#include<assert.h>
using namespace std;
namespace cls
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector() = default;
vector(const vector<T>& v)
{
reserve(v.size());
for (auto& e : v)
{
push_back(e);
}
}
vector(int n, const T& x = T())
{
reserve(n);
while (n--)
{
push_back(x);
}
}
template<class InputIterator>
vector(InputIterator first,InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
const iterator begin() const
{
return _start;
}
const iterator end() const
{
return _finish;
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _end_of_storage - _start;
}
bool empty()
{
return _start == _finish;
}
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
void reserve(size_t n)
{
if (_start + n > _end_of_storage)
{
size_t oldsize = size();
T* temp = new T[n];
size_t i = 0;
for (i = 0; i < oldsize; ++i)
{
temp[i] = _start[i];
}
delete[] _start;
_start = temp;
_finish = _start + oldsize;
_end_of_storage = _start + n;
}
}
void push_back(const T& x)
{
if (_finish == _end_of_storage)
{
reserve(size() == 0 ? 4 : 2 * capacity());
}
*_finish = x;
++_finish;
}
void pop_back()
{
assert(!empty());
--_finish;
}
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 clear()
{
_finish = _start;
}
~vector()
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
void swap(vector<T>& x)
{
std::swap(_start,x._start);
std::swap(_finish,x._finish);
std::swap(_end_of_storage,x._end_of_storage);
}
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;
}
}
}
iterator insert(iterator pos , const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _end_of_storage)
{
size_t len = pos - _start;
reserve(2*capacity());
pos = _start + len;
}
iterator end = _finish;
while (end > pos)
{
*(end) = *(end - 1);
--end;
}
*pos = x;
++_finish;
return pos;
}
void erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator begin = pos;
while (begin < _finish - 1)
{
*begin = *(begin + 1);
++begin;
}
--_finish;
}
private:
iterator _start = nullptr;
iterator _finish = nullptr;
iterator _end_of_storage = nullptr;
};
template<class T>
void PrintVector(const vector<T>& x)
{
typename vector<T>::const_iterator it = x.begin();
while (it != x.end())
{
cout << *it << ' ';
++it;
}
cout << endl;
}
template<class T>
void PrintContainer(const T& x)
{
for (auto e : x)
{
cout << e << ' ';
}
cout << endl;
}
void Test()
{
/*vector<int> v;
cout << v.size() << endl;*/
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(10);
v.push_back(10);
vector<int> v1 = v;
v1.push_back(100);
v = v1;
v1.erase(v1.begin());
v1.insert(v1.begin(),5);
v1[0] = 200;
v1.pop_back();
v1.resize(12);
vector<int> v2(5,10);
PrintVector(v);
PrintContainer(v1);
PrintContainer(v2);
}
}
这里注意不能把模版实现在两个不同的文件,这样会报链接错误,实现在同一个文件就行了。希望对大家有所帮助。