文章目录
- vector的介绍
- vector的模拟实现
- 成员变量
- Member functions
- constructor(构造函数)
- destructor(析构函数)
- operator=
- Iterators
- begin
- end
- Capacity
- size
- capacity
- reserve
- resize
- Element access
- operator[]
- Modifiers
- push_back
- pop_back
- insert
- erase
- swap
- 完整版实现代码
- vector.h
- test.cpp
vector的介绍
vector是C++ STL库中一个重要的容器,它分为以下几个部分(我们也将在vs2022编译环境下实现以下几个部分)
Member functions
Iterators
Capacity
Element access
Modifiers
Allocator
Non-member function overloads
Template specializations
vector的模拟实现
接下来我们要模拟实现每个部分中最重要也是最常见的几个接口(其中的Allocator、Non-member function overloads、Template specializations这三部分因为涉及到有的知识还未学到,或者不常用的情况所以没有实现)
vector被实现位类模板
成员变量
从这里可以看出迭代器是T类型指针
Member functions
constructor(构造函数)
其中每个构造函数中的形参alloc是向内存池申请空间,是编译器为了方便管理自己实现了一个内存池,而我们并不用考虑这些,所以不用管,有兴趣的可以去了解一下。
destructor(析构函数)
这里如果delete的是空指针也是合法的
operator=
Iterators
begin
end
这里的begin和end分为普通迭代器和const修饰的迭代器,普通迭代器返回值可读可写,const修饰的迭代器返回值只可读不可写
Capacity
size
capacity
reserve
resize
Element access
operator[]
还有front和back但是有了operator[]实现的意义不大,所以没有实现
Modifiers
push_back
pop_back
insert
erase
swap
完整版实现代码
vector.h
#pragma once
namespace lzf
{
template < class T >
class vector
{
public:
typedef T* iterator;
typedef const T* const_iterator;
void swap(vector<T>& v)
{
swap(_start, v._start);
swap(_finish, v._finish);
swap(_endofstorage, v._endofstorage);
}
//构造一个空类
vector()
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{}
//拷贝构造
vector(const vector<T>& v)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
/*iterator temp = new T[v.size()];
memcpy(temp, v._start, sizeof(T) * v.size());
_start = temp;
_finish = _start + v.size();
_endofstorage = _start + v.capacity();*/
vector temp(v);
swap(temp);
}
//类模板
template <class InputIterator>
vector(InputIterator first, InputIterator last)
:_start(nullptr)
, _finish(nullptr)
, _endofstorage(nullptr)
{
/*size_t sz = last - first;
iterator temp = new T[sz];
memcpy(temp, first, sizeof(T) * sz);
_start = temp;
_finish = _start + sz;
_endofstorage = _start + sz;*/
while (first != last)
{
push_back(*first);
++first;
}
}
vector<T>& operator=(vector<T> v)
{
swap(v);
return *this;
}
~vector()
{
if (_start)
{
delete[] _start;
_start = _finish = _endofstorage = nullptr;
}
}
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator begin()const
{
return _start;
}
const_iterator end()const
{
return _finish;
}
size_t size()const
{
return _finish - _start;
}
size_t capacity()const
{
return _endofstorage - _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);
}
void reserve(size_t n)
{
if (_start + n > _endofstorage)
{
size_t sz = size();
iterator temp = new T[n];
if (_start)
{
memcpy(temp, _start, sizeof(T) * sz);
delete[] _start;
}
_start = temp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
void resize(size_t n, const T& val = T())
{
if (n < size())
{
_finish = _start + n;
}
else
{
if (_start + n > _endofstorage)
{
size_t sz = size();
iterator temp = new T[n];
if (_start)
{
memcpy(temp, _start, sizeof(T) * sz);
delete[] _start;
}
_start = temp;
_finish = _start + sz;
_endofstorage = _start + n;
}
while (_finish != _endofstorage)
{
*_finish = val;
_finish++;
}
}
}
void push_back(const T& x)
{
if (_finish == _endofstorage)
{
//扩容
size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
/*size_t sz = size();
iterator temp = new T[newcapacity];
memcpy(temp, _start, sizeof(T) * sz);
delete[] _start;
_start = temp;
_finish = _start + sz;
_endofstorage = _start + newcapacity;*/
reserve(newcapacity);
}
//插入数据
*_finish = x;
++_finish;
}
void pop_back()
{
assert(_finish > _start);
--_finish;
}
iterator insert(iterator pos, const T& x)
{
//检测插入位置是否正常
assert(pos <= _finish);
assert(pos >= _start);
if (_finish == _endofstorage)
{
//扩容
size_t sz = pos - _start;
size_t newcapacity = capacity() == 0 ? 4 : 2 * capacity();
reserve(newcapacity);
pos = _start + sz;
}
//插入数据
iterator end = _finish - 1;
while (end >= pos)
{
*(end + 1) = *end;
--end;
}
*pos = x;
++_finish;
return pos;
}
iterator erase(iterator pos)
{
//检测删除位置是否正常
assert(pos < _finish);
assert(pos >= _start);
//插入数据
iterator begin = pos;
iterator end = _finish - 1;
while (begin != end)
{
*begin = *(begin + 1);
++begin;
}
_finish = begin;
return pos;
assert(pos >= _start);
///*assert(pos < _finish);
//iterator begin = pos + 1;
//while (begin < _finish)
//{
// *(begin - 1) = *begin;
// ++begin;
//}
//--_finish;*/
return pos
}
private:
iterator _start;
iterator _finish;
iterator _endofstorage;
};
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(5);
for (auto e : v1)
{
cout << e << " ";
}
//cout << endl;
}
void test_vector2()
{
vector<int>v1;
vector<int>v2;
v1.reserve(100);
cout << v1.capacity() << endl;
v2.resize(100);
cout << v2.capacity() << endl;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
//vector<int> v3(v1);
vector<int> v3(v1.begin() + 1, v1.end() - 1);
for (auto e : v3)
{
cout << e << " ";
}
}
void test_vector3()
{
vector<int>v1;
vector<int>v2;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
//v1.push_back(5);
vector<int> v3(v1.begin() + 1, v1.end() - 1);
for (auto e : v3)
{
cout << e << " ";
}
cout << endl;
v3.insert(v3.begin(), 4);
v3.insert(v3.end(), 4);
for (auto e : v3)
{
cout << e << " ";
}
cout << endl;
vector<int>::iterator pos = find(v3.begin(), v3.end(), 2);
//vector<int>::iterator pos = find(v3.begin(), v3.end(), 4);
cout << *pos;
//v3.erase(pos);
//vector<int>::iterator pos1 = find(v3.begin(), v3.end(), 4);
//v3.erase(pos1);
//for (auto e : v3)
//{
// cout << e << " ";
//}
cout << endl;
// 要求删除v1所有的偶数
/*vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
if (*it % 2 == 0)
{
v1.erase(it);
}
++it;
}*/
}
void test_vector4()
{
vector<int>v1;
//vector<int>v2;
v1.push_back(1);
v1.push_back(2);
//v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
// 要求删除v1所有的偶数
vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
if (*it % 2 == 0)
{
it = v1.erase(it);
}
else
{
++it;
}
}
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
v1.pop_back();
v1.pop_back();
//v1.pop_back();
for (auto e : v1)
{
cout << e << " ";
}
cout << endl;
}
}
test.cpp
void test()
{
int* p = nullptr;
delete p;
}
int main()
{
//lzf::test_vector1();
//lzf::test_vector2();
//test();
//lzf::test_vector3();
lzf::test_vector4();
return 0;
}