C++實(shí)現(xiàn)動(dòng)態(tài)順序表
更新時(shí)間:2020年05月22日 16:00:38 作者:adorable_
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)動(dòng)態(tài)順序表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C++實(shí)現(xiàn)動(dòng)態(tài)順序表的具體代碼,供大家參考,具體內(nèi)容如下
Vector.h
#pragma once
#include <stdio.h>
#include <iostream>
#include <assert.h>
#include <string.h>
using namespace std;
typedef int DataType;
class Vector
{
public:
Vector()
:_first(NULL)
, _finish(NULL)
, _endofstorage(NULL)
{}
Vector(const Vector& v)
{
if (v.Size() > 0)
{
_first = new DataType[v.Size()]; //只開辟原有數(shù)據(jù)所占空間大小,節(jié)省空間
memcpy(_first, v._first, sizeof(DataType)*v.Size());
if (_first)
{
_finish = _first + v.Size();
_endofstorage = _first + v.Size();
}
else
{
_first = _finish = _endofstorage = NULL;
}
}
}
Vector& operator=(Vector& v)
{
if (this != &v)
{
////傳統(tǒng)寫法
//DataType* tmp = new DataType[v.Size()];
//memcpy(tmp, _first, sizeof(DataType)*v.Size());
//delete[] _first;
//_first = tmp;
//_finish = _first + v.Size();
//_endofstorage = _first + v.Size();
//現(xiàn)代寫法
swap(_first, v._first);
swap(_finish, v._finish);
swap(_endofstorage, v._endofstorage);
}
return *this;
}
~Vector()
{
delete[] _first;
_first = _finish = _endofstorage = NULL;
}
void Print()
{
DataType* cur = _first;
while (cur != _finish)
{
cout << *cur << " ";
++cur;
}
cout << endl;
}
size_t Size() const;
size_t Capacity() const;
void Expand(size_t n);
void PushBack(DataType x);
void Reserve(size_t n);
void PopBack();
void Insert(size_t pos, DataType x);
void Erase(size_t pos);
size_t Find(DataType x);
private:
DataType* _first;
DataType* _finish;
DataType* _endofstorage;
};
size_t Vector::Size() const
{
return _finish - _first;
}
size_t Vector::Capacity() const
{
return _endofstorage - _first;
}
void Vector::Expand(size_t n)
{
if (n > Capacity())
{
size_t size = Size();
DataType* tmp = new DataType[n];
memcpy(tmp, _first, sizeof(DataType)*size);
delete[] _first;
_first = tmp;
_finish = _first + size; //切記更新新的_finish和_endofstorage
_endofstorage = _first + n;
}
}
void Vector::PushBack(DataType x)
{
//if (_finish == _endofstorage)
//{
// if (Capacity() == 0)
// {
// Expand(3);
// }
// else
// {
// Expand(Capacity() * 2);
// }
//}
//*_finish = x;
//++_finish;
Insert(Size(), x);
}
void Vector::Reserve(size_t n)
{
if (n > Capacity())
{
Expand(n);
}
}
void Vector::PopBack()
{
assert(_finish > _first);
--_finish;
}
void Vector::Insert(size_t pos, DataType x)
{
assert(pos <= Size());
if (_finish == _endofstorage)
{
if (Capacity() == 0)
{
Expand(3);
}
else
{
Expand(Capacity() * 2);
}
}
int end = Size() - 1;
while (end >= (int)pos)
{
_first[end + 1] = _first[end];
--end;
}
_first[pos] = x;
++_finish;
}
void Vector::Erase(size_t pos)
{
assert(pos < Size());
size_t cur = pos;
while (cur < Size() - 1)
{
_first[cur] = _first[cur + 1];
++cur;
}
--_finish;
}
size_t Vector::Find(DataType x)
{
DataType* cur = _first;
while (cur != _finish)
{
if (*cur == x)
{
return cur - _first;
}
++cur;
}
return -1;
}
void TestVector()
{
Vector v1;
v1.PushBack(1);
v1.PushBack(2);
v1.PushBack(3);
v1.PushBack(4);
v1.Print();
size_t pos = v1.Find(2);
printf("pos expect 1,actual %lu\n", pos);
Vector v2(v1);
v2.Insert(0, 0);
v2.Print();
Vector v3;
v3 = v2;
v3.Print();
v3.Erase(1);
v3.Print();
}
test.cpp
#include "Vector.h"
int main()
{
cout << "順序表:" << endl;
TestVector();
return 0;
}
效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Qt實(shí)現(xiàn)繪制網(wǎng)格背景的示例代碼
這篇文章主要介紹了Qt如何實(shí)現(xiàn)繪制網(wǎng)格背景,并且能實(shí)現(xiàn)窗口大小調(diào)整時(shí)網(wǎng)格背景也自動(dòng)調(diào)整重繪,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06
貪吃蛇C語(yǔ)言代碼實(shí)現(xiàn)(難度可選)
這篇文章主要為大家詳細(xì)介紹了貪吃蛇C語(yǔ)言代碼實(shí)現(xiàn),游戲難度可供選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C++實(shí)現(xiàn)RSA加密解密算法是示例代碼
非對(duì)稱加密方式可以使通信雙方無(wú)需事先交換密鑰就可以建立安全通信,因此被廣泛應(yīng)用于身份認(rèn)證、數(shù)字簽名、等信息交換領(lǐng)域。其中最具有代表性的非對(duì)稱加密方式就是RSA公鑰密碼體制。本文將用C++實(shí)現(xiàn)RSA加密解密算法,需要的可以參考一下2022-09-09
Qt利用QScroller實(shí)現(xiàn)home界面滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Qt如何利用QScroller實(shí)現(xiàn)home界面滑動(dòng)效果,文中的實(shí)現(xiàn)過(guò)程講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-11-11
C++從文本文件讀取數(shù)據(jù)到vector中的方法
這篇文章主要給大家介紹了利用C++如何從文本文件讀取數(shù)據(jù)到vector中,文章通過(guò)實(shí)例給出示例代碼,相信會(huì)對(duì)大家的理解和學(xué)習(xí)很有幫助,有需要的朋友們下面來(lái)一起看看吧。2016-10-10

