目录
前言
一、头文件 .h文件
总结
前言
本文是模拟实现vector部分功能的代码,可以直接拿去使用
一、头文件 .h文件
#include<assert.h>
#include<iostream>
using namespace std;
namespace zz
{
template<class T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
vector()//默认构造
{}
vector(const vector<T>& v)//拷贝构造
{
reserve(v.size());
for (auto& e : v)
{
push_back(e);
}
}
~vector()//析构
{
if (_start)
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
}
void clear()
{
_finish = _start;
}
const vector<T> operator=(const vector<T>& v)
{
if (this != &v)
{
clear();
reserve(v.size());
for (auto& e : v)
{
push_back(e);
}
}
return *this;
}
template <class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first!=last)
{
push_back(*first);
++first;
}
}
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);
}
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin() const
{
return _start;
}
const_iterator end() const
{
return _finish;
}
bool empty()
{
return _start == _finish;
}
void reserve(size_t n)
{
if (n > capacity())
{
size_t old_size = size();
T* tmp = new T[n];
//memcpy(tmp, _start, size() * sizeof(T));如果是vector<string>这里就是浅拷贝,要换成下边赋值的写法
for (size_t i = 0; i < old_size; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
_start = tmp;
_finish = tmp + old_size;
_end_of_storage = tmp + n;
}
}
T& operator[](size_t i)
{
assert(i < size());
return _start[i];
}
size_t size()const
{
return _finish - _start;
}
size_t capacity()const
{
return _end_of_storage - _start;
}
void push_back(const T& x)
{
if (_finish != _end_of_storage)
{
*_finish = x;
++_finish;
}
else
{
reserve(capacity() == 0 ? 4 : 2 * capacity());
*_finish = x;
++_finish;
}
}
void pop_back()
{
assert(!empty());
--_finish;
}
iterator insert(iterator pos, const T& v)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _end_of_storage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : 2 * capacity());
pos = _start + len;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end+1)=*end;
--end;
}
*pos = v;
++_finish;
return pos;
}
//也会造成迭代器失效
void earse(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator it = pos + 1;
while (it!=end())
{
*(it - 1) = *it;
it++;
}
--_finish;
}
T& operator[](size_t i)const
{
assert(i < size());
return _start[i];
}
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=nullptr;//指向数据块的开始
iterator _finish=nullptr;//指向有效数据的尾
iterator _end_of_storage=nullptr;//指向存储容量的尾
};
template <class T>
void print_vector(const vector<T>& v)
{
//不加typename的话编译器不能从没有实例化的类模板里边取东西,编译器无法区分const_iterator是类型还是静态成员变量
typename vector<T>::const_iterator it = v.begin();//这里也可以写成auto it=v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << " ";
}
}
总结
以上就是vector容器模拟实现的代码,实现了基本的常用的功能,一些很少用的没有实现,纯手打。希望亲们点点发财的小手点个赞!!!!!