容器vector的底层实现是由数组(连续的线性空间)实现的,veector是动态分配内存空间的,也就是说,随着元素的加入,它的内部机制会自行扩充空间以便于容纳新元素。因此vector的底层实现技术的核心是对大小的控制以及重新分配内存空间时对数据的移动效率。因为vector的底层实现是由数组来实现的,所以vector的迭代器就是原生指针。vector的简单实现如下:
我把迭代器的内嵌的相应的型别封装的起来。
template<typename T>
struct IteratorType {
typedef T Value_type; //迭代器所指之物的型别
typedef T* Pointer;
typedef T* Iterator; //vector的迭代器
typedef T& Reference;
typedef size_t Size_type; //vector容器的大小
typedef size_t Difference_type; //迭代器之间的距离
};
#include"IteratorType.h"
#pragma once
template<typename T>
class Vector {
public:
Vector() :start(nullptr), finish(nullptr), end_finish(nullptr) {};
Vector(typename IteratorType<T>::Size_type n, const T& value);
explicit Vector(typename IteratorType<T>::Size_type);
~Vector();
public:
typename IteratorType<T>::Iterator begin()const;
typename IteratorType<T>::Iterator end()const;
typename IteratorType<T>::Size_type size()const;
typename IteratorType<T>::Size_type capacity()const;
typename IteratorType<T>::Reference operator[](const typename IteratorType<T>::Size_type n)const;
typename IteratorType<T>::Reference front()const;
typename IteratorType<T>::Reference back()const;
void pop_back();
void push_back(const T& key);
bool empty()const;
void clear()noexcept;
typename IteratorType<T>::Iterator erase(typename IteratorType<T>::Iterator position);
typename IteratorType<T>::Iterator insert(typename IteratorType<T>::Iterator position,const T& value);
private:
typename IteratorType<T>::Iterator start; //使用空间的头 满足左闭右开
typename IteratorType<T>::Iterator finish; //使用空间的尾 满足左闭右开
typename IteratorType<T>::Iterator end_finish; //可用空间(容量)的尾 满足左闭右开
private:
typename IteratorType<T>::Pointer construct(typename IteratorType<T>::Size_type n, const T& value) noexcept;
typename IteratorType<T>::Pointer construct(typename IteratorType<T>::Size_type n)noexcept;
void destory(typename IteratorType<T>::Pointer buff) noexcept;
void destory(typename IteratorType<T>::Pointer first, typename IteratorType<T>::Pointer last)noexcept;
};
#include"Vector.h"
#include<iostream>
#include"Vector.h"
#include<iostream>
template<typename T>
typename IteratorType<T>::Pointer Vector<T>::construct(typename IteratorType<T>::Size_type n,const T& value)noexcept {
auto tmp = new T[n]();
for (int i = 0; i < n; ++i) {
tmp[i] = value;
}
if (tmp == nullptr) {
std::cout << "Out of MemorySpace!!" << std::endl;
}
else {
start = tmp;
finish = start + n;
end_finish = 2 * finish + 1;
}
}
template<typename T>
typename IteratorType<T>::Pointer Vector<T>::construct(typename IteratorType<T>::Size_type n)noexcept {
auto tmp = new T[n]();
if (tmp == nullptr) {
std::cout << "Out of MemorySpace!!" << std::endl;
}
else {
start = tmp;
finish = start + n;
end_finish = 2 * finish + 1;
}
}
template<typename T>
void Vector<T>::destory(typename IteratorType<T>::Pointer buff)noexcept {
if (buff != nullptr) {
delete buff;
--finish;
}
}
template<typename T>
void Vector<T>::destory(typename IteratorType<T>::Pointer first, typename IteratorType<T>::Pointer last)noexcept {
while (first != last) {
destory(first);
++first;
--finish;
}
}
template<typename T>
Vector<T>::Vector(typename IteratorType<T>::Size_type n, const T& value) {
construct(n, value);
}
template<typename T>
Vector<T>::Vector(typename IteratorType<T>::Size_type) {
construct(n);
}
template<typename T>
Vector<T>::~Vector() {
destory(start, finish);
}
template<typename T>
typename IteratorType<T>::Iterator Vector<T>::begin()const {
return start;
};
template<typename T>
typename IteratorType<T>::Iterator Vector<T>::end()const {
return finish - 1;
};
template<typename T>
typename IteratorType<T>::Size_type Vector<T>::size()const {
return finish - start;
};
template<typename T>
typename IteratorType<T>::Size_type Vector<T>::capacity()const {
return end_finish - start;
};
template<typename T>
typename IteratorType<T>::Reference Vector<T>::operator[](const typename IteratorType<T>::Size_type n)const {
return *(start + n-1);
};
template<typename T>
typename IteratorType<T>::Reference Vector<T>::front()const {
return *start;
};
template<typename T>
typename IteratorType<T>::Reference Vector<T>::back()const {
return *(finish - 1);
};
template<typename T>
void Vector<T>::pop_back() {
if (!empty()) {
auto endelemit = end();
destory(endelemit);
}
else
std::cout << "Vector is Empty!" << std::endl;
}
template<typename T>
void Vector<T>::push_back(const T& key) {
if (finish != end_finish) {
*finish = key;
++finish;
}
else {
auto first = begin();
auto last = end();
auto n = size();
auto new_tmp=construct(2 * n + 1);
while (first != last) {
new_tmp[i] = std::move(*first);
++first;
}
start = new_tmp;
finish = first + 1;
end_finish = 2 * n + 1;
}
}
template<typename T>
bool Vector<T>::empty()const {
if (start == finish)
return true;
else
return false;
}
template<typename T>
void Vector<T>::clear()noexcept {
delete[] start;
}
template<typename T>
typename IteratorType<T>::Iterator Vector<T>::erase(typename IteratorType<T>::Iterator position) {
if (position + 1!= end()) {
//重新分配内存,进行移动,返回删除元素的下一个元素的迭代器
//更新 start finish end_finish
}
else {
return end();
}
}
template<typename T>
typename IteratorType<T>::Iterator Vector<T>::insert(typename IteratorType<T>::Iterator position,const T& value) {
if (finish + 1 != end_finish) {
//重新分配内存,进行移动,在position前插入value,返回插入元素的迭代器
//更新 start finish end_finish
}
else {
//重新分配动态内存,然后进行数据的移动,更新 start finish end_finish
// 在position前插入value,返回插入元素的迭代器
}
}