C++實現(xiàn)動態(tài)順序表(vector)
vector是連續(xù)存儲結(jié)構(gòu),支持隨機(jī)的高效的隨機(jī)和在尾部進(jìn)行插入、刪除操作,其它位置的插入、刪除操作相對來說效率較低。
vector相當(dāng)于一個數(shù)組,但它的數(shù)組空間大小需要寫一程序來實現(xiàn)。
它的內(nèi)存分配原理大概可分為下面幾步:
1)首先分配一塊內(nèi)存空間進(jìn)行存儲;
2)當(dāng)所需存儲的數(shù)據(jù)超過分配的空間時,再重新分配一塊空間;
3)將舊元素復(fù)制到新空間;
4)釋放舊空間。
實現(xiàn)代碼如下:
vector.h
#pragma once #include<stdio.h> #include<assert.h> #include<string.h> #include<iostream> 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()); memcpy(_first, v._first, sizeof (DataType)*v.Size()); } if (_first > 0){ _finish = _first + v.Size(); _endofstorage = _first + v.Size(); } _first = _finish = _endofstorage = NULL; } Vector& operator=(const Vector& v){ if (this != &v){ /*swap(_first, v._first); swap(_finish, v._finish); swap(_endofstorage, v._endofstorage);*/ 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(); } return *this; } ~Vector(){ delete[] _first; _first = _finish = _endofstorage = NULL; } void Print(){ DataType* cur = _first; while (cur != _first){ cout << "*cur" << endl; cur++; } cout << endl; } size_t Size() const{ return _finish - _first; } size_t Capacity() const{ return _endofstorage - _first; } void Expand(size_t n){ if (n > Capacity()){ DataType* tmp = new DataType(n); size_t size = Size(); memcpy(tmp, _first, sizeof(DataType)*size); delete[] _first; _first = tmp; _finish = _first + size; _endofstorage = _first + n; } } void PushBack(DataType x){ if (_finish == _endofstorage){ size_t capacity = Capacity() > 0 ? Capacity() * 2 : 3; Expand(capacity); /*if (Capacity() == 0){ Expand(3); } else{ Expand(Capacity() * 2); }*/ } *_finish = x; ++_finish; } void PopBack(){ assert(_finish > _first); --_finish; } void Reserve(size_t n){ if (n > Capacity()){ Expand(n); } } void Insert(size_t pos, DataType x){ assert(pos < Size()); if (_finish = _endofstorage){ size_t capacity = Capacity() > 0 ? Capacity() * 2 : 3; Expand(capacity); } int tmp = Size() - 1; while (tmp >= (int)pos){ _first[tmp + 1] = _first[tmp]; --tmp; } _first[pos] = x; ++_finish; } void Erase(size_t pos){ assert(pos < Size()); size_t cur = pos; while (cur < Size()-1){ _first[cur] = _first[cur] + 1; ++cur; } --_finish; } size_t Find(DataType x){ DataType *cur = _first; while (cur != _finish){ if (*cur == x){ return cur - _first; } ++cur; } return -1; } private: DataType* _first; DataType* _finish; DataType* _endofstorage; };
test.cpp
#include"vector.h" void Tset(){ Vector v; v.PushBack(1); v.PushBack(2); v.PushBack(3); v.PushBack(4); v.PopBack(); v.Print(); size_t pos = v.Find(1); printf("pos->data:expcet 1,axtual %lu", pos); Vector v1; v1.Insert(1, 0); v1.Print(); Vector v2; v2.Erase(3); v2.Print(); } int main(){ Tset(); return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++中strlen(),sizeof()與size()的區(qū)別
本文主要介紹了C++中strlen(),sizeof()與size()的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05C++示例講解friend static const關(guān)鍵字的用法
靜態(tài)成員static是解決同一個類的不同對象之間數(shù)據(jù)和函數(shù)共享問題。區(qū)分全局變量,全局變量也能實現(xiàn)數(shù)據(jù)共享,但安全性和封裝性被破壞了,友元提供了不同類或?qū)ο蟮某蓡T函數(shù)之間、類的成員函數(shù)與一般函數(shù)之間進(jìn)行數(shù)據(jù)共享的機(jī)制,const常引用-被引用的對象不能被更新2022-06-06C++實現(xiàn)與Lua相互調(diào)用的示例詳解
這篇文章主要為大家詳細(xì)介紹了C++實現(xiàn)與Lua相互調(diào)用的方法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價值,感興趣的小伙伴可以了解一下2023-03-03剖析C++中的常量表達(dá)式與省略號的相關(guān)作用
這篇文章主要介紹了C++中的常量表達(dá)式與省略號的相關(guān)作用,以及表達(dá)式中的可變參數(shù)模板示例,需要的朋友可以參考下2016-01-01