目录
须知:typedef 私有成员 模板声明
typedef
私有成员
模拟声明
1.构造函数 拷贝构造函数
2.析构函数
3.赋值重载 =
4.size capacity
5.reserve
6.resize
7.下标运算符 [ ]
8.push_back pop_back
9.swap
10.insert
11.erase
12.完整代码 + 测试代码
vector.h
test.cpp
typedef T* iterator;
typedef const T* const_iterator;
iterator _start = nullptr; // 指向数据块的开始
iterator _finish = nullptr;; // 指向有效数据的尾
iterator _endOfStorage = nullptr;; // 指向存储容量的尾
template
template
vector()
{}
vector(int n, const T& value = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(value);
}
}
template
vector(InputIterator first, InputIterator last)
{
while (first < last)
{
push_back(*first);
first++;
}
}
vector(const vector& v)
{
reserve(v.capacity());
T* cur = v._start;
while (cur < v._finish)
{
push_back(*cur);
cur++;
}
}
~vector()
{
delete[] _start;
_start = _finish = _endOfStorage = nullptr;
}
vector& operator= (vector v)
{
//传统写法
T* tmp = new T[v.size()];
for (size_t i = 0; i < v.size(); i++)
{
tmp[i] = v._start[i];
}
delete[]_start;
_start = tmp;
_finish = _start + v.size();
_endOfStorage = _start + v.capacity();
//现代写法
//swap(v);
return *this;
}
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endOfStorage - _start;
}
void reserve(size_t n)
{
if (n > capacity())
{
T* tmp = new T[n];
size_t sz = size();
if (_start)
{
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[]_start;
}
_start = tmp;
_finish = _start + sz;
_endOfStorage = _start + n;
}
}
void resize(size_t n, const T& value = T())
{
if (n <= size())
{
_finish = _start + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while (_finish < _start + n)
{
*_finish = value;
_finish++;
}
}
}
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 push_back(const T& x)
{
insert(end(), x);
}
void pop_back()
{
erase(end() - 1);
}
void swap(vector& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endOfStorage, v._endOfStorage);
}
iterator insert(iterator pos, const T& x)
{
assert(pos <= _finish);
assert(pos >= _start);
//检查容量
if (_finish == _endOfStorage)
{
int len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
//挪动数据
iterator it = _finish;
while (it > pos)
{
*it = *(it - 1);
it--;
}
*it = x;
_finish++;
return pos;
}
iterator erase(iterator pos)
{
assert(pos < _finish);
assert(pos >= _start);
//挪动数据
iterator it = pos;
while (it < _finish)
{
*it = *(it + 1);
it++;
}
_finish--;
return pos;
}
#include
//模拟实现vector类,并完成测试
namespace bit
{
template
class vector
{
public:
// Vector的迭代器是一个原生指针
typedef T* iterator;
typedef const T* const_iterator;
iterator begin()
{
return _start;
}
iterator end()
{
return _finish;
}
const_iterator cbegin() const
{
return _start;
}
const_iterator cend() const
{
return _finish;
}
// construct and destroy
vector()
{}
vector(int n, const T& value = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(value);
}
}
template
vector(InputIterator first, InputIterator last)
{
while (first < last)
{
push_back(*first);
first++;
}
}
vector(const vector& v)
{
reserve(v.capacity());
T* cur = v._start;
while (cur < v._finish)
{
push_back(*cur);
cur++;
}
}
vector& operator= (vector v)
{
//传统写法
T* tmp = new T[v.size()];
for (size_t i = 0; i < v.size(); i++)
{
tmp[i] = v._start[i];
}
delete[]_start;
_start = tmp;
_finish = _start + v.size();
_endOfStorage = _start + v.capacity();
//现代写法
//swap(v);
return *this;
}
~vector()
{
delete[] _start;
_start = _finish = _endOfStorage = nullptr;
}
// capacity
size_t size() const
{
return _finish - _start;
}
size_t capacity() const
{
return _endOfStorage - _start;
}
void reserve(size_t n)
{
if (n > capacity())
{
T* tmp = new T[n];
size_t sz = size();
if (_start)
{
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[]_start;
}
_start = tmp;
_finish = _start + sz;
_endOfStorage = _start + n;
}
}
void resize(size_t n, const T& value = T())
{
if (n <= size())
{
_finish = _start + n;
}
else
{
if (n > capacity())
{
reserve(n);
}
while (_finish < _start + n)
{
*_finish = value;
_finish++;
}
}
}
///access///
T& operator[](size_t pos)
{
assert(pos < size());
return *(_start + pos);
}
const T& operator[](size_t pos)const
{
assert(pos < size());
return *(_start + pos);
}
///modify/
void push_back(const T& x)
{
insert(end(), x);
}
void pop_back()
{
erase(end() - 1);
}
void swap(vector& v)
{
std::swap(_start, v._start);
std::swap(_finish, v._finish);
std::swap(_endOfStorage, v._endOfStorage);
}
iterator insert(iterator pos, const T& x)
{
assert(pos <= _finish);
assert(pos >= _start);
//检查容量
if (_finish == _endOfStorage)
{
int len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
//挪动数据
iterator it = _finish;
while (it > pos)
{
*it = *(it - 1);
it--;
}
*it = x;
_finish++;
return pos;
}
iterator erase(iterator pos)
{
assert(pos < _finish);
assert(pos >= _start);
//挪动数据
iterator it = pos;
while (it < _finish)
{
*it = *(it + 1);
it++;
}
_finish--;
return pos;
}
void print_vector()
{
for (int i = 0; i < size(); i++)
{
cout << _start[i] << " ";
}
cout << endl;
}
private:
iterator _start = nullptr; // 指向数据块的开始
iterator _finish = nullptr;; // 指向有效数据的尾
iterator _endOfStorage = nullptr;; // 指向存储容量的尾
};
//测试 构造函数 拷贝构造函数 赋值重载
void test1()
{
vector v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.print_vector();
vector v2(10, 1);
v2.print_vector();
vector::iterator begin = v1.begin();
vector::iterator end = v1.end();
vector v3(begin, end);
v3.print_vector();
vector v4(v2);
v4.print_vector();
vector v5;
v5 = v2;
v5.print_vector();
vector v6(30, 2);
v6.print_vector();
cout << v6.capacity() << endl;
v2.reserve(100);
v6 = v2;
v6.print_vector();
cout << v6.capacity() << endl;
}
//测试 resize reserve
void test2()
{
vector v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.resize(10, 3);
v1.print_vector();
cout << v1.capacity() << endl;;
vector v2(v1);
v2.reserve(100);
cout << v2.capacity() << endl;;
}
//测试 []
void test3()
{
vector v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1[0] = 100;//非const可以修改
for (int i = 0; i < v1.size(); i++)
{
cout << v1[i] << ' ';
}
cout << endl;
const vector v2(v1.begin(), v1.end());
//v2[0] = 100; //const 不能修改
for (int i = 0; i < v2.size(); i++)
{
cout << v2[i] << ' ';
}
cout << endl;
}
//测试 push_back pop_back
void test4()
{
vector v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.print_vector();
v1.pop_back();
v1.pop_back();
v1.print_vector();
}
//测试 insert erase
void test5()
{
vector v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
v1.push_back(4);
v1.push_back(5);
v1.insert(v1.end() - 1, 100);
v1.insert(v1.end() - 1, 100);
v1.insert(v1.end() - 1, 100);
v1.insert(v1.end() - 1, 100);
v1.insert(v1.end() - 1, 100);
v1.insert(v1.end() - 1, 100);
v1.insert(v1.end() - 1, 100);
v1.insert(v1.end() - 1, 100);
v1.print_vector();
v1.erase(v1.begin() + 1);
v1.erase(v1.begin() + 1);
v1.erase(v1.begin() + 1);
v1.print_vector();
}
}
#include
using namespace std;
#include "vector.h"
int main()
{
//bit::test1();
//bit::test2();
//bit::test3();
//bit::test4();
bit::test5();
return 0;
}