一、vector.h
#pragma once
#include<iostream>
#include<assert.h>
#include<string>
namespace bit
{
template<class T>
class vector
{
public:
// Vector的迭代器是一个原生指针
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;
}
// construct and destroy
vector()
:_start(nullptr),_finish(nullptr),_endOfStorage(nullptr)
{}
vector(int n, const T& value = T())
{
reserve(n);
for (size_t i = 0; i < n; i++)
{
push_back(value);
}
}
template<class InputIterator>
vector(InputIterator first, InputIterator last)
{
while (first != last)
{
push_back(*first);
first++;
}
}
vector(const vector<T>& v)
{
reserve(v.capacity());
for (auto& e : v)
{
push_back(e);
}
}
vector<T>& operator= (vector<T> v)
{
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 old_size = size();
//memcpy(tmp, _start, size() * sizeof(T)); //如果用memcpy,string等类的拷贝是浅拷贝,_start析构会将空间释放掉
for (size_t i = 0; i < old_size; i++)
{
tmp[i] = _start[i];//string类或其他类的赋值是深拷贝
}
delete[] _start;
_start = tmp;
_finish = tmp + old_size;
_endOfStorage = tmp + n;
}
}
void resize(size_t n, const T& value = T())
{
if (n > size())
{
reserve(n);
while (_finish < _start + n)
{
*_finish = value;
_finish++;
}
}
else
{
_finish = _start + n;
}
}
///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());
}
void swap(vector<T>& 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 >= _start && pos <= _finish);
if (_finish == _endOfStorage)
{
size_t len = pos - _start;
reserve(capacity() == 0 ? 4 : capacity() * 2);
pos = _start + len;
}
iterator it = _finish - 1;
while (it >= pos)
{
*(it + 1) = *it;
it--;
}
*pos = x;
_finish++;
return pos;
}
iterator erase(iterator pos)
{
assert(pos >= _start && pos < _finish);
iterator it = pos + 1;
while (it < _finish)
{
*(it - 1) = *it;
it++;
}
_finish--;
return pos;
}
private:
iterator _start; // 指向数据块的开始
iterator _finish; // 指向有效数据的尾
iterator _endOfStorage; // 指向存储容量的尾
};
// 函数模板
template<class T>
void print_vector(const vector<T>& v)
{
for (size_t i = 0; i < v.size(); i++)
{
std::cout << v[i] << " ";
}
std::cout << std::endl;
//typename vector<T>::const_iterator it = v.begin();
/*auto it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << " ";
}
cout << endl;*/
}
void test_vector1()
{
std::cout << "--------------------------test_vactor1()--------------------------" << std::endl;
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);
/*for (size_t i = 0; i < v.size(); i++)
{
cout << v[i] << " ";
}
cout << endl;
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
for (auto e : v)
{
cout << e << " ";
}
cout << endl;*/
}
void test_vector2()
{
std::cout << "--------------------------test_vactor2()--------------------------" << std::endl;
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()
{
std::cout << "--------------------------test_vactor3()--------------------------" << std::endl;
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<int> v2(v1);
print_vector(v2);
}
void test_vector4()
{
std::cout << "--------------------------test_vactor4()--------------------------" << std::endl;
vector<int> v1(10, 1);
print_vector(v1);
}
void test_vector5()
{
std::cout << "--------------------------test_vactor5()--------------------------" << std::endl;
vector<std::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)
{
std::cout << e << " ";
}
}
void test_vector6()
{
std::cout << "--------------------------test_vactor6()--------------------------" << std::endl;
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);
vector<int>::iterator it = v1.begin() + 3;
v1.insert(it, 40);
print_vector(v1);
std::cout << *it << std::endl;//输出随机值,因为insert后迭代器失效,it失效后就不要使用了。
}
void test_vector7()
{
std::cout << "--------------------------test_vactor7()--------------------------" << std::endl;
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);
//删除偶数 -- 迭代器失效后,不要直接使用,如果要使用按规则重新更新后使用
vector<int>::iterator it = v1.begin();
while (it != v1.end())
{
if (*it % 2 == 0)
{
it = v1.erase(it);
}
else
{
++it;
}
}
print_vector(v1);
}
}
二、test.cpp
#include"vector.h"
using namespace bit;
int main()
{
//test_vector1();
//test_vector2();
//test_vector3();
//test_vector4();
//test_vector5();
//test_vector6();
//test_vector7();
return 0;
}
三、注意事项
在reserve函数中要注意不要使用memcpy函数
在使用迭代器时要注意迭代器的失效问题(如test_vector6()和test_vector7()中的演示)