头文件
在实现自定义reserve
函数和拷贝构造函数vector(const vector
需要注意一下空间的存储
我们可以通过一个赋值来解决这一问题
#pragma once
#include <iostream>
#include <assert.h>
#include <algorithm>
using namespace std;
namespace test_vector
{
template<class T>
class vector
{
public:
//迭代器就是原生的指针
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin()const
{
return _start;
}
const_iterator end()const
{
return _finish;
}
//返回容量
size_t capacity()const
{
return _end_of_storage - _start;
}
//返回现在有几个元素
size_t size()const
{
return _finish - _start;
}
//定义[]
T& operator[](size_t pos)
{
assert(pos < size());
return _start[pos];
}
const T& operator[](size_t pos)const
{
assert(pos < size());
return _start[pos];
}
//定义reserve
void reserve(size_t n)
{
if (n > capacity())
{
//提前保存上一个vector的size
size_t sz = size();
T* tmp = new T[n];
if (_start)
{
//memcpy(tmp, _start, sizeof(T) * size());
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[] _start;
}
_start = tmp;
_finish = _start + sz;
_end_of_storage = _start + n;
}
}
//定义resize
void resize(size_t n, const T& val = T())
{
if (n < size())
{
_finish = _start + n;
}
else
{
reserve(n);
while (_finish != _start + n)
{
insert(_finish, val);
}
}
}
//定义尾插
void push_back(const T& x)
{
if (_finish == _end_of_storage)
{
reserve(capacity() == 0 ? 4 : 2 * capacity());
}
*_finish = x;
++_finish;
}
//定义尾删
void pop_back()
{
assert(_finish > _start);
--_finish;
}
//定义插入
iterator insert(iterator pos, const T& x)
{
assert(pos <= _finish && pos >= _start);
if (_finish == _end_of_storage)
{
size_t i = pos - _start;
reserve(capacity() == 0 ? 4 : 2 * capacity());
pos = _start + i;
}
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
end--;
}
*pos = x;
++_finish;
return pos;
}
//自定义删除
iterator erase(iterator pos)
{
assert(pos >= _start);
assert(pos < _finish);
iterator i = pos + 1;
while (i < _finish)
{
*(i - 1) = *i;
i++;
}
--_finish;
return pos;
}
//自定义构造
vector()
:_start(nullptr)
,_finish(nullptr)
,_end_of_storage(nullptr)
{}
//带参拷贝构造v1(v)方法1
//vector(const vector& v)
// :_start(nullptr)
// ,_finish(nullptr)
// ,_end_of_storage(nullptr)
//{
// //reserve(v.capacity());
// /*size_t i = 0;
// while (i < v.size())
// {
// this->push_back(v._start[i]);
// ++i;
// }*/
// for (const auto& it : v)
// {
// this->push_back(it);
// }
//}
//带参拷贝构造v1(v)方法2
//vector(const vector& v)
// :_start(nullptr)
// , _finish(nullptr)
// , _end_of_storage(nullptr)
//{
// _start = new T[v.capacity()];//如果这;·里是v.size(),则下边的_end_of_storage = _start + v.size();
// memcpy(_start, v._start, sizeof(T) * v.size());
///* for (size_t i = 0; i < v.size(); i++)
//{
// tmp[i] = _start[i];
//}*/
// _finish = _start + v.size();
// _end_of_storage = _start + v.capacity();
//}
//任意类型也可转为vector类型
template <class InputIterator>
vector(InputIterator begin, InputIterator end)
:_start(nullptr)
,_finish(nullptr)
,_end_of_storage(nullptr)
{
while (begin != end)
{
push_back(*begin);
++begin;
}
}
//带参拷贝构造v1(v)方法3
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _end_of_storage(nullptr)
{
/*const_iterator begin = v.begin();
const_iterator end = v.end();
while (begin != end)
{
push_back(*begin);
begin++;
}*/
vector<T> tmp(v.begin(), v.end());
swap(tmp);
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_end_of_storage, v._end_of_storage);
}
vector(int n, const T& val = T())
:_start(nullptr)
,_finish(nullptr)
,_end_of_storage(nullptr)
{
reserve(n);
for (int i = 0; i < n; i++)
{
push_back(val);
}
}
// v2 = v
vector<T>& operator=(vector<T> v)
{
swap(v);
return* this;
}
//自定义析构
~vector()
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
private:
iterator _start;
iterator _finish;
iterator _end_of_storage;
};
void Tvector()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5);
for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] <<" ";
}
cout << endl;
//auto it = v.begin();
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
it++;
}
cout << endl;
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
}
void Func(const vector<int>& v)
{
vector<int>::const_iterator it = v.begin();
while (it != v.end())
{
//定义为const就不可以改变他的值了
//(*it)++;
cout << *it << " ";
it++;
}
cout << endl;
}
void Tvector2()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//v.push_back(5);
for (auto i : v)
{
i *= 2;
cout << i << " ";
}
cout << endl;
//v.pop_back();
auto p = find(v.begin(), v.end(),4);
//这里要考虑到是否查到的问题
if (p != v.end())
{
v.insert(p,44);
//这里要考虑的是,当内从扩容后,原先的p就会失效
//v.insert(p,444);
}
v.insert(v.begin(), 0);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
auto p1 = find(v.begin(), v.end(), 1);
v.erase(p1);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
}
void Tvector3()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
auto p = find(v.begin(), v.end(), 1);
v.erase(p);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
}
//删除vector中的所有偶数
void Tvector4()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(4);
auto it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
it = v.erase(it);
}
else
{
it++;
}
}
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
}
//在所有偶数位置之前插入该偶数的二倍
void Tvector5()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(4);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
auto it = v.begin();
while (it != v.end())
{
if (*it % 2 == 0)
{
it = v.insert(it, *it * 2);
++it;
++it;
}
else
{
++it;
}
}
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
}
//拷贝构造测试
void Tvector7()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
vector<int> v1(v);
v1[0] = v1[0] * 2;
for (auto i : v1)
{
cout << i << " ";
}
cout << endl;
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
string s("abcdefg");
for (auto i : s)
{
cout << i << " ";
}
cout << endl;
vector<int> v2(s.begin(),s.end());
for (auto i : v2)
{
cout << i << " ";
}
cout << endl;
}
//调试自定义赋值运算
void Tvector8()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vector<int> v2(v);//等价于:vector v2 = v;
v2[0] = 2;
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
for (auto i : v2)
{
cout << i << " ";
}
cout << endl;
v2 = v;
for (auto i : v)
{
cout << i << " ";
}
cout << endl;
for (auto i : v2)
{
cout << i << " ";
}
cout << endl;
vector<int> v3(10,1);
for (auto i : v3)
{
cout << i << " ";
}
cout << endl;
}
//调试resize
void Tvector9()
{
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.resize(2);
for (auto it : v)
{
cout << it << " ";
}
cout << endl;
v.resize(10);
for (auto it : v)
{
cout << it << " ";
}
cout << endl;
}
void Tvector10()
{
vector<string> v;
v.push_back("11111");
v.push_back("22222");
v.push_back("33333");
v.push_back("44444");
vector<string> v2(v);
v[0] = "66666";
v[1] = "66666";
v[2] = "66666";
v[3] = "66666";
for (auto it : v)
{
cout << it << " ";
}
cout << endl;
for (auto it : v2)
{
cout << it << " ";
}
cout << endl;
v2.pop_back();
v2.pop_back();
v2.pop_back();
v2.pop_back();
v2.pop_back();
for (auto it : v2)
{
cout << it << " ";
}
cout << endl;
}
}
测试文件
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
#include <vector>
using namespace std;
#include "vector.h"
//#include
int main()
{
test_vector::Tvector10();
return 0;
}
//库vector测试
//void test_vector1()
//{
// vector v1;
// vector v2(10, 1);
// vector v3(v2);//拷贝构造
//}
//
//void test_vector2()
//{
// vector v1;
// v1.push_back(1);
// v1.push_back(2);
// v1.push_back(3);
// v1.push_back(4);
//
// //下标+[]
// for (size_t i = 0; i < v1.size(); i++)
// {
// v1[i]++;
// }
//
// for (size_t i = 0; i < v1.size(); i++)
// {
// cout << v1[i] << " ";
// }
// cout << endl;
//
// //迭代器
// vector::iterator it = v1.begin();
// while (it != v1.end())
// {
// (*it)--;
// cout << *it <<" ";
// it++;
// }
// cout << endl;
//
// for (auto e : v1)
// {
// cout << e << " ";
// }
//
// cout << endl;
//}
//
//void test_vector3()
//{
// vector v1;
// v1.push_back(1);
// v1.push_back(2);
// v1.push_back(3);
// v1.push_back(4);
//
// for (size_t i = 0; i < v1.size(); i++)
// {
// cout << v1[i] << " ";
// }
// cout << endl;
//
// vector::iterator pos = find(v1.begin(), v1.end(), 3);
//
// if (pos != v1.end())//因为如果查找失败返回的是v1.end()
// {
// //v1.insert(pos, 30);
// v1.erase(pos);
// }
//
// for (auto e : v1)
// {
// cout << e << " ";
// }
//
// cout << endl;
//
// //范围for循环iterator原型
//
// /*for (auto i = v1.begin(); i != v1.end(); i++)
// {
// cout << *i << " ";
// }*/
//
// cout << endl;
//
// cout << endl;
//}
//
//void test_vector4()
//{
// vector v1;
// v1.push_back(10);
// v1.push_back(1);
// v1.push_back(32);
// v1.push_back(12);
// v1.push_back(2);
// v1.push_back(4);
// v1.push_back(5);
//
// for (auto e : v1)
// {
// cout << e << " ";
// }
// cout << endl;
//
// less ls;
// sort(v1.begin(), v1.end(),ls);
// for (auto e : v1)
// {
// cout << e << " ";
// }
// cout << endl;
//
// sort(v1.begin(), v1.end());
// for (auto e : v1)
// {
// cout << e << " ";
// }
// cout << endl;
//
// //greater gt;
//
// //sort(v1.begin(), v1.end(),gt);
// //定义匿名方式
// sort(v1.begin(), v1.end(),greater());
// for (auto e : v1)
// {
// cout << e << " ";
// }
// cout << endl;
//}
//
//void test_vector5()
//{
// vector strv;
// string s1("hello1234");
// //sort(s1.begin(), s1.end(), greater());
// strv.push_back(s1);
// strv.push_back(string("w"));
// strv.push_back("a");
// for (const auto& str : strv)
// {
// cout << str << " ";
// }
// cout << endl;
//
// /*for (auto i = const& strv.begin(); i != strv.end(); i++)
// {
// cout << *i << " ";
// }*/
// cout << endl;
//}
//int main()
//{
// //test_vector1();
// test_vector5();
// return 0;
//}