本文旨在完成vector的增删查改的内容
#pragma once
#include <iostream>
#include <vector>
#include <algorithm>
#include<assert.h>
using namespace std;
namespace vt
{
template <typename T>
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
//拷贝构造
vector(const vector<T>& v)
{
reserve(v.capacity());
for (auto e : v)
{
push_back(e);
}
}
vector(int n, const T val = T())
{
reserve(n);
for (int i = 0;i < n;i++)
{
push_back(val);
}
}
// 类模板的成员函数
// 函数模板 -- 目的支持任意容器的迭代器区间初始化
template <class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
++first;
}
}
void swap(vector<T>& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_end_of_storage, v._end_of_storage);
}
// v1 = v3
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
// 强制编译器生成默认的
vector() = default;
~vector()
{
if (_start)
{
delete[] _start;
_start = _finish = _end_of_storage = nullptr;
}
}
iterator begin()
{
return _start;
}
const_iterator begin()const
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator end()const
{
return _finish;
}
size_t size()
{
return _finish-_start;
}
size_t size()const
{
return _finish - _start;
}
size_t capacity()
{
return _end_of_storage - _start;
}
size_t capacity() const
{
return _end_of_storage - _start;
}
void push_back(const T& x)
{
if (_finish == _end_of_storage)
{
int newcapacity = (capacity() == 0) ? 4 : 2 * capacity();
reserve(newcapacity);
}
*_finish = x;
++_finish;
}
void pop_back()
{
_finish--;
_end_of_storage--;
}
T& operator[](int n)
{
assert(n< size());
return _start[n];
}
void reserve(int n)
{
if (n > capacity())
{
int len = size();
T* tmp = new T[n];
for (int i = 0; i < len; i++)
{
tmp[i] = _start[i];
}
_start = tmp;
_finish = _start + len;
_end_of_storage = _start + n;
}
}
iterator insert(iterator pos, const T& x)
{
assert(pos >= _start);
assert(pos <= _finish);
if (_finish == _end_of_storage)
{
size_t len = pos - _start;
int newcapacity = (capacity() == 0) ? 4 : 2 * capacity();
reserve(newcapacity);
pos = _start + len;
}
iterator end1 = end() - 1;//end()-1?
while (end1>=pos)
{
*(end1 + 1) = *end1;
end1--;
}
*pos = x;
_finish++;
return pos;
}
iterator find(iterator it1, iterator it2, const T& x)
{
while (it1 != it2)
{
if (*it1 == x)
{
return it1;
}
it1++;
}
cout << x << " is no find"<<endl;
return _finish;
}
void erase(iterator pos)
{
iterator end = _finish-1;
while (pos < end)
{
*pos = *(pos + 1);
pos++;
}
_finish--;
}
private:
iterator _start = nullptr;//指向动态分配的数组的起始位置
iterator _finish = nullptr;//指向当前存储的最后一个元素的下一个位置(“逻辑结束”)
iterator _end_of_storage = nullptr;//指向数组分配内存的末尾(“物理结束”)
};
}
运行测试代码
#include"vectorteset.h"
using namespace std;
void vector_test1()
{
vt::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.pop_back();
for (auto e : v)
{
cout << e << ' ';
}
}
void vector_test2()
{
vt::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vt::vector<int>::iterator it1 = v.begin();
vt::vector<int>::iterator it2 = v.begin();
vt::vector<int>::iterator it3= v.find(it1,it2,6);
v.insert(it3,5);
for (auto e : v)
{
cout << e << ' ';
}
}
void vector_test3()
{
vt::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
vt::vector<int>::iterator it1 = v.begin();
vt::vector<int>::iterator it2 = v.begin();
vt::vector<int>::iterator it3 = v.find(it1, it2, 1);
v.erase(it3);
for (auto e : v)
{
cout << e << ' ';
}
}
void vector_test4()
{
vt::vector<int> v1(10,10);
vt::vector<int> v2 = v1;
vt::vector<int> v3(v1);
}
int main()
{
vector_test4();
return 0;
}