目录
1.Vector.h
#include <iostream>
#include <cassert>
#include <algorithm>
using namespace std;
template <class T>
class Vector
{
public:
typedef T *iterator;
typedef const T *const_iterator;
iterator begin() const
{
return _start;
}
iterator end() const
{
return _finish;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endofstorage - _start;
}
T &operator[](size_t i)
{
assert(i < size());
return _start[i];
}
const T &operator[](size_t pos) const
{
assert(pos < size());
return _start[pos];
}
Vector<T> &operator=(Vector<T> v)
{
swap(v);
return *this;
}
Vector() {}
Vector(const Vector<T> &v)
{
reserve(v.capacity());
for (auto &e : v)
{
push_back(e);
}
}
Vector(initializer_list<T> il)
{
reserve(il.size());
for (auto &e : il)
{
push_back(e);
}
}
template <class InputIterator>
Vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
// 防止push整型时失效
Vector(int n, const T &val = T())
{
reserve(n);
for (int i = 0; i < n; i++)
{
push_back(val);
}
}
Vector(size_t n, const T &val = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(val);
}
}
~Vector()
{
if (_start)
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
}
void reserve(size_t n)
{
if (n > capacity())
{
T *temp = new T[n];
size_t _size = size();
for (size_t i = 0; i < _size; ++i)
{
temp[i] = _start[i];
}
delete[] _start;
_start = temp;
_finish = _start + _size;
_endofstorage = _start + n;
}
}
void resize(size_t n, T val = T())
{
if (n > size())
{
reserve(n);
while (_finish != _endofstorage)
{
*_finish = val;
_finish++;
}
}
else
{
// 忽视后面的元素,直接修改_finish的指向
_finish = _start + n;
}
}
void push_back(const T &value)
{
if (_finish == _endofstorage)
{
reserve(capacity() == 0 ? 4 : capacity() * 2);
}
*_finish = value;
++_finish;
}
void pop_back()
{
assert(!empty());
--_finish;
}
// 检查容器是否为空
bool empty()
{
return _start == _finish;
}
// 清理容器
void clear()
{
// 让起点与终点重合
_start = _finish = _endofstorage = nullptr;
}
iterator insert(iterator position, const T &val)
{
// 检查插入位置是否在范围之中
assert(position <= _finish && position >= _start);
// 如果_finish==_endofstorage,那么需要对容器进行扩容
if (_finish == _endofstorage)
{
// 储存扩容前的相对位置
size_t len = position - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
// 扩容了需要对position进行更新
position = _start + len;
}
iterator it = _finish - 1;
while (it >= position)
{
*(it + 1) = *it;
it--;
}
*position = val;
_finish++;
return position;
}
iterator erase(iterator position)
{
// 清除的部位必须在范围之内
assert(position < _finish && position >= _start);
iterator it = position + 1;
while (it <= _finish)
{
*(it - 1) = *it;
it++;
}
_finish--;
return position;
}
void swap(Vector<T> &temp)
{
std::swap(_start, temp._start);
std::swap(_finish, temp._finish);
std::swap(_endofstorage, temp._endofstorage);
}
private:
iterator _start = nullptr;
iterator _finish = nullptr;
iterator _endofstorage = nullptr;
};
template <class T>
void print_Vector(const Vector<T> &v)
{
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
}
2.Test.cpp
#include "Vector.h"
void test_Vector1()
{
Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(4);
v1.push_back(4);
print_Vector(v1);
Vector<double> v2;
v2.push_back(1.1);
v2.push_back(2.2);
v2.push_back(3.1);
print_Vector(v2);
v2.insert(v2.begin(), 11.11);
print_Vector(v2);
v2.insert(v2.begin(), 11.11);
print_Vector(v2);
v2.insert(v2.begin(), 11.11);
print_Vector(v2);
v2.insert(v2.begin(), 11.11);
print_Vector(v2);
v2.insert(v2.begin(), 11.11);
print_Vector(v2);
v2.erase(v2.begin());
print_Vector(v2);
v2.erase(v2.begin() + 4);
print_Vector(v2);
}
void test_Vector2()
{
// C++在引入了模板之后对于内置类型也进行了升级,对于int而言,其默认构造函数的初始化结果是0
// int i = 1;
// int j = int();
// int k = int(2);
Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(4);
v1.push_back(4);
print_Vector(v1);
v1.resize(10);
print_Vector(v1);
v1.resize(3);
print_Vector(v1);
}
void test_Vector3()
{
Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(4);
v1.push_back(4);
print_Vector(v1);
v1.pop_back();
Vector<int> v2(v1);
print_Vector(v2);
Vector<int> v3;
v3.push_back(10);
v3.push_back(20);
v3.push_back(30);
v1 = v3;
print_Vector(v1);
print_Vector(v3);
}
void test_Vector4()
{
Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
print_Vector(v1);
Vector<int> v2(v1.begin() + 1, v1.end() - 1);
print_Vector(v2);
string str("abcd");
Vector<int> v3(str.begin(), str.end());
print_Vector(v3);
}
void test_Vector5()
{
Vector<int> v1(10, 1);
print_Vector(v1);
Vector<int> v2(10, 1);
print_Vector(v2);
Vector<char> v3(10, 'a');
print_Vector(v3);
}
void test_Vector6()
{
auto x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << typeid(x).name() << endl;
cout << sizeof(x) << endl;
initializer_list<int> y = {1, 2, 3, 4, 5, 6, 7};
// 单参数的构造函数,隐式类型转换
string str = "11111"; // 构造 + 拷贝构造 -> 优化 直接构造
const string &str1 = "11111"; // 构造临时对象,引用的是临时对象
Vector<string> v;
v.push_back(str);
v.push_back(string("22222"));
v.push_back("33333");
int i = 1;
// 不推荐 -- C++11
// int j = { 1 };
int k{ 1 };
// 跟上面类似
// 隐式类型转换+优化
// Vector<int> v1 = { 1,2,3,4,5,6,7,8,9,10 };
Vector<int> v1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
// 直接构造
Vector<int> v2({10, 20, 30, 40});
for (auto e : v2)
{
cout << e << " ";
}
cout << endl;
}
void test_Vector7()
{
Vector<string> v;
v.push_back("11111");
v.push_back("22222");
v.push_back("33333");
v.push_back("44444");
v.push_back("55555");
for (auto &e : v)
{
cout << e << " ";
}
cout << endl;
}
void test_Vector8()
{
Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.push_back(6);
v1.push_back(7);
v1.push_back(8);
print_Vector(v1);
// insert以后,it就失效了,不要使用了
Vector<int>::iterator it = v1.begin() + 3;
v1.insert(it, 40);
print_Vector(v1);
it = v1.begin() + 3;
cout << *it << endl;
}
void test_Vector9()
{
// std::Vector<int> v1;
Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(4);
v1.push_back(5);
// v1.push_back(4);
// 删除偶数 -- 迭代器失效以后,不要直接使用,如果要使用按规则重新更新后使用
// std::Vector<int>::iterator it = v1.begin();
Vector<int>::iterator it = v1.begin();
// cout << typeid(it).name() << endl;
while (it != v1.end())
{
if (*it % 2 == 0)
{
it = v1.erase(it);
}
else
{
++it;
}
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
int main()
{
// test_Vector1();
// test_Vector2();
// test_Vector3();
// test_Vector4();
// test_Vector5();
// test_Vector6();
// test_Vector7();
// test_Vector8();
test_Vector9();
return 0;
}