我是靠谱客的博主 甜蜜项链,最近开发中收集的这篇文章主要介绍vector的基本模拟实现pragma onceincludeinclude,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

vector内部相当于顺序表实现。

pragma once

include

include

using namespace std;

template
class MyVector
{
typedef T*Iterator;
public:
MyVector()
:_start(NULL)
, _finish(NULL)
, _endofstorage(NULL)
{}
MyVector(const MyVector& s)
:_start(NULL)
, _finish(NULL)
, _endofstorage(NULL)
{
_start = s._start;
_finish = s._finish;
_endofstorage = s._endofstorage;
}
~MyVector()
{
if (_start)
{
delete[]_start;
_start = _finish = _endofstorage = NULL;
}
}
T& operator[](size_t index)//重载operator[]
{
assert(index < Size());
return _start[index];
}
Iterator Begin()
{
return _start;
}
Iterator End()
{
return _finish;
}
Iterator rBegin()//逆向访问
{
Reserve();
return _start;
}
Iterator rEnd()
{
Reverse();
return _finish;
}
void Reverse()
{
size_t sz = Size();
for (size_t i = 0; i < sz; i++)
{
for (size_t j = sz-1; j >= i; j–)
{
Swap(_start[i], _start[j]);
}
}
}
void Swap(Iterator a,Iterator b)//交换
{
T tmp = *b;
*b = *a;
*a = tmp;
}
void Assign(size_t n, const T&x)//指定向量内容将新内容赋给vector,替换其当前内容,并相应地修改其大小。
{
if (Size() != n)
{
Expand(n);
}
for (size_t i = 0; i < n; i++)
{
PushBack(x);
}
}
void Assign(Iterator a,Iterator b)//参数为指针
{
_start = a;
_finish = b;
}
bool Empty()
{
if (Size() == 0)
return true;
else
return false;
}
void Expand()
{
size_t sz = Size();
size_t cp = Capacity();
if (sz <= cp)
{
cp = (cp == 0) ? 3 : cp * 2;
T* tmp = new T[cp];
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[]_start;
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + cp;
}
}
void Expand(size_t n)
{
size_t sz = Size();
size_t cp = Capacity();
if (cp <= n)//不可以用realloc来开辟空间,会产生浅拷贝的问题(同一块空间被两个指针管理,会产生冲突)
{
Iterator tmp = new T[n];
for (size_t i = 0; i < sz; i++)
{
tmp[i] = _start[i];
}
delete[]_start;
_start = tmp;
_finish = _start + sz;
_endofstorage = _start + n;
}
}
void PushBack(const T& x)
{
size_t sz = Size();
if (Capacity() == 0 || Capacity() == Size())
{
Expand();
}
_start[sz] = x;
sz++;
_finish=_start + sz;
}
void PopBack()
{
size_t size = Size();
if (size == 0)
{
return;
}
T* del = _start + size - 1;
del = NULL;
_finish = _start + size - 1;
}
void PushFront(const T& x)
{
size_t sz = Size();
if (Capacity() == 0 || Capacity() == Size())
{
Expand();
}
for (size_t i = sz; i > 0; i–)
{
_start[i] = _start[i - 1];
}
_start[0] = x;
_finish = _finish + 1;
}
void PopFront()
{
size_t sz = Size();
for (size_t i = 0; i < sz; i++)
{
_start[i] = _start[i + 1];
}
_finish = _finish - 1;
}
size_t Size()
{
return _finish - _start;
}
size_t Capacity()
{
return _endofstorage - _start;
}
void Resize(size_t n, const T& val = T())//1.若元素个数大于n,则删除多余的元素。
{ //2.若元素个数小于n,且没有给初始值,则默认初始化为0,若给初始值,则多余的部分全部初始化为初始值
size_t capacity = Capacity(); //3.若元素个数等于n,则不做改变。
size_t size = Size();
if (capacity == n)
{
Expand(n);
}
if (n > size)//n大的情况
{
for (size_t i = size; i < n; i++)
{
if (val == NULL)
{
PushBack(0);
}
else
{
PushBack(val);
}
}
}
if (n < size)//n小的情况
{
for (size_t i = size; i > n; i–)
{
T*del = _finish - 1;
_finish = _finish - 1;
del = NULL;
}
}
else
{
return;
}
_finish = _start + n;
}
void Reserve(size_t n)//将容量扩至n个
{
if (Size() != n)
{
Expand(n);
}
}
void Insert(size_t pos,const T&x)//插入元素
{
assert(pos);
if (capacity == n)
{
Expand(n);
}
size_t sz = Size();
if (pos == 0)
{
PushFront(x);
}
if (pos == sz)
{
PushBack(x);
}
else
{
for (size_t i = sz; i > pos; i–)
{
_start[i] = _start[i - 1];
}
_start[pos] = x;
_finish = _finish + 1;
}
}
void Erase(size_t pos)//删除指定位置
{
size_t size = Size();
assert(pos);
if (pos == size - 1)
{
PopBack();
}
else
{
_start[pos] = NULL;
for (size_t i = pos; i < size; i++)
{
_start[i] = _start[i + 1];
}
_finish = _finish - 1;
}
}
void Clear()//清除所有
{
size_t sz = Size();
for (size_t i = 0; i < sz; i++)
{
_start[i] = NULL;
}
_finish = _start + 1;
}
void Display()
{
for (size_t i = 0; i < Size(); i++)
{
cout << _start[i] << ” “;
}
cout << endl;
}
protected:
Iterator _start;
Iterator _finish;
Iterator _endofstorage;
};

void test()
{
MyVectors;
s.PushBack(1);
s.PushBack(2);
s.PushBack(3);
s.PushBack(4);
s.PushBack(5);
s.Display();
//s.PopBack();
//s.Display();
//s.Resize(8,788);
//s.Display();
//s.Erase(2);
//s.Display();
s.Assign(7, 100);
s.Display();
}
int main()
{
test();
system(“pause”);
return 0;
}

最后

以上就是甜蜜项链为你收集整理的vector的基本模拟实现pragma onceincludeinclude的全部内容,希望文章能够帮你解决vector的基本模拟实现pragma onceincludeinclude所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部