首先铺垫一些扩展性知识:
1、模板不支持分离编译,why?
分离编译不会模板实例化
2、怎样解决?
1)显示实例化,最好指定模板类型(因为有的模板必须指定类型)。
2)最好的方式是把声明和定义放一个头文件里(类里面定义太大了)。
四大成员函数:
构造函数:
Vector()
: _first(NULL)
, _finish(NULL)
, _endofstorage(NULL)
{}
图解:
拷贝构造:
Vector( const Vector<T>& v)
{
_first = new T[v._finish-v._first];
/*memcpy(_first, v._first,(v._finish-v._first)*sizeof(T));*/
T* start = v._first;
while (start != v._finish)
{
*(_finish) = *(start);
_finish++;
start++;
}
_finish = _first + (v._finish - v._first);
_endofstorage = _first + (v._finish - v._first);
}
图解:
赋值运算符重载:
Vector<T>& operator=(const Vector<T>& v)
{
if (this!=&v)
{
T* newstr = new T[v._finish-v._first];
delete[] _first;
_first = _finish = newstr;
/*memcpy(_first, v._first, (v._finish - v._first)*sizeof(T));*/
T*start = v._first;
while (start != v._finish)
{
*(_finish) = *(start);
_finish++;
start++;
}
_finish = _first + (v._finish - v._first);
_endofstorage = _first + (v._finish - v._first);
}
return *this;
}
图解:
析构函数:
~Vector()
{
if(_first)
delete []_first;
_first = _finish = _endofstorage = NULL;
}
Vector内部实现:
扩容:
template<class T>
void Vector<T>::Expand(size_t n)
{
if (Size() >= Capacity())
{
size_t size = Size();
T* tmp = new T[n];
T* cur = tmp;
if (_first)
{
//memcpy(tmp, _first, Size()*sizeof(T));
T*start = _first;
while (start!=_finish)
{
(*tmp) = (*start);
++tmp;
++start;
}
delete[] _first;
}
_first = cur;
_finish = _first + size;
_endofstorage = _first + n;
}
}
图解:
插入:
template<class T>
void Vector<T>::Insert(size_t pos, const T& x)
{
assert(pos <= Size());
if (Size()>= Capacity())
{
Expand(Capacity() * 2 + 3);
}
T* end = _finish-1;
while (end >= _first + pos)
{
*(end+1) = *(end);
--end;
}
//while (end!=_finish)
//{
// *(end+1) = *end;
//}
*(_first + pos) = x;
++_finish;
}
图解:
任意位置删除:
template<class T>
void Vector<int>::Erase(size_t pos)
{
assert(pos < Size());
T* start = _first + pos + 1;
while (start < _finish)
{
*(start - 1) = *start;
++start;
}
--_finish;
}
图解:
查找:
template<class T>
size_t Vector<T>::Find(const T& x)
{
T* start = _first;
while (start != _finish)
{
if (*start == x)
return start - _first;
++start;
}
return -1;
}
测试传递不同类型的参数类型:
1)整型测试:
Vector<int> v1;
v1.PushBack(1);
v1.PushBack(2);
v1.PushBack(3);
v1.PushBack(4);
v1.Print();
v1.Insert(0, 5);
v1.Insert(3, 6);
v1.Insert(6, 7);
printf("%d",v1.Find(3));
v1.Print();
cout << endl;
v1.Erase(2);
v1.Erase(5);
v1.Print();
cout << endl;
结果:
结论:
完成了整型测试任意位置的插入,删除,查找。
2)字符串类型测试:string
Vector<string> v4;
v4.PushBack("hello");
v4.PushBack("world!");
v4.Print();
v4.PushBack("4444444444444444444444444");
v4.Print();
v4.PushBack("3211312434111111111111111");
v4.PushBack("23");
v4.Print();
注意:
这里进行字符串拷贝的时候要注意不能使用memcopy
原因:
错误拷法:
memcopy会导致上图错误
正确拷法:
windows下短值可拷的原因:
编译器将较小值存在数组buf中,若有一值过长,就会出问题:
所以拷值的时候为了一劳永逸,就采用如下拷贝方法:
Vector<T>& operator=(const Vector<T>& v)
{
if (this!=&v)
{
T* newstr = new T[v._finish-v._first];
delete[] _first;
_first = _finish = newstr;
/*memcpy(_first, v._first, (v._finish - v._first)*sizeof(T));*/
T*start = v._first;
while (start != v._finish)
{
*(_finish) = *(start);
_finish++;
start++;
}
_finish = _first + (v._finish - v._first);
_endofstorage = _first + (v._finish - v._first);
}
return *this;
}