#include<iostream>
using namespace std;
#include<string>
#include<assert.h>
template<class T>
class myVector {
public:
typedef T* iterator;
typedef const T* const_iterator;
public:
//构造和析构//
/
myVector()
:_start(nullptr)
,_finish(nullptr)
,_endOfStorage(nullptr){}
//构造并使用n个值为value的元素初始化
myVector(size_t n, const T& val = T()) //val是一个缺省参数,如果是内置类型默认值为0,如果是自定义类型,就是默认的构造函数
:_start(new T[n])
,_finish(_start)
,_endOfStorage(_start + n){
for (size_t i = 0; i < n; i++) {
*_finish++ = val;
}
}
//构造并使用n个值为value的元素初始化
myVector(int n, const T& val = T())
:_start(new T[n])
, _finish(_start)
, _endOfStorage(_start + n) {
for (int i = 0; i < n; i++) {
*_finish++ = val;
}
}
//区间构造
template<class inputIterator> //vector可以用来存储任意类型的数据,可以对任意类型初始化
myVector(inputIterator begin, inputIterator end) {
size_t n = distance(begin, end);
_start = new T[n];
_finish = _start;
_endOfStorage = _start + n;
while (begin != end) {
*_finish++ = *begin++;
}
}
//拷贝构造
myVector(const myVector<T>& v) //涉及资源管理,采取深拷贝
:_start(nullptr)
,_finish(nullptr)
,_endOfStorage(nullptr){
myVector<T> temp(v.cbegin(), v.cend()); //v是const修饰的,所以只能调用const修饰的迭代器
this->swap(temp);
}
//赋值运算符重载
myVector<T>& operator=(myVector<T> v) {
swap(v); //这里是传值,巧妙使用了拷贝构造
return *this;
}
//析构
~myVector() {
if (_start) {
delete[] _start;
_start = nullptr;
_finish = nullptr;
_endOfStorage = nullptr;
}
}
/
//迭代器//
iterator begin() {
return _start;
}
iterator end() {
return _finish;
}
const_iterator cbegin()const {
return _start;
}
const_iterator cend()const {
return _finish;
}
void swap(myVector<T>& v) {
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endOfStorage, v._endOfStorage);
}
///
//容量相关接口//
size_t size() const{
return _finish - _start;
}
size_t capacity() const{
return _endOfStorage - _start;
}
bool empty() {
return _start == _finish;
}
void resize(size_t n, const T& val = T()) {
if (n <= size()) {
_finish = _start + n;
}
else {
if (n > capacity()) {
//扩容
reserve(n);
}
for (auto i = end(); i < begin() + n; i++) {
*i = val;
}
_finish = _start + n;
}
}
void reserve(size_t n) {
size_t oldsize = size();
if (n > capacity()) {
//申请新空间
T* temp = new T[n];
if (_start) {
//拷贝元素
//注意:如果对象中涉及资源管理,一定不能使用memcpy进行对象之间的拷贝
//因为memcpy是浅拷贝,会引起内存泄露以及程序崩溃。
//错误:memcpy(temp, _start, sizeof(T) * oldsize);
for (size_t i = 0; i < oldsize; i++) {
temp[i] = _start[i];
}
//释放旧空间
delete[] _start;
}
_start = temp;
_finish = _start + oldsize;
_endOfStorage = _start + n;
}
}
///
//访问元素
T& operator[](size_t index) {
assert(index < size());
return _start[index];
}
const T& operator[](size_t index)const {
assert(index < size());
return _start[index];
}
//获取第一个元素
T& front() {
return _start[0];
}
const T& front()const {
return _start[0];
}
T& back() {
return *(_finish - 1);
}
const T& back()const {
return *(_finish - 1);
}
//
//修改元素
void push_back(const T& val) {
if (_finish == _endOfStorage) {
reserve(capacity() * 2 + 3);
}
*_finish++ = val;
}
void pop_back() {
if (_start != _finish) {
_finish--;
}
}
iterator insert(iterator pos, const T& val) {
if (pos < _start || pos > _finish) {
return pos;
}
if (_finish == _endOfStorage) {
reserve(capacity() * 2 + 3);
}
auto it = _finish - 1;
while (it >= pos) {
*(it + 1) = *it;
--it;
}
*pos = val;
++_finish;
return pos;
}
iterator erase(iterator pos) {
if (pos < _start || pos > _finish) {
return pos;
}
auto it = pos + 1;
while (it < _finish) {
*(it - 1) = *it;
++it;
}
--_finish;
return pos;
}
void clear() {
_finish = _start;
}
private:
template<class Iterator>
size_t distance(Iterator begin, Iterator end) {
size_t count = 0;
while (begin != end) {
++count;
++begin;
}
return count;
}
/
private:
iterator _start; //指向数据块的开始
iterator _finish; //指向有效数据的末尾
iterator _endOfStorage; //指向存储容量的尾
};
|