C++實(shí)現(xiàn)順序表的方法
廢話不多說了,直接給大家上關(guān)鍵代碼了。
#pragma once
#include <assert.h>
template<class T>
class SeqList
{
public:
SeqList()
:_a(NULL)
,_size(1)
,_capacity(1)
{}
SeqList(T* a, size_t size)
:_a(new T[size])
,_size(size)
,_capacity(size)
{
for (size_t i = 0; i < _size; ++i)
{
_a[i] = a[i];
}
}
//拷貝構(gòu)造函數(shù)常規(guī)寫法
/*SeqList(const SeqList<T>& s)
:_a(new T[s._size])
,_size(s._size)
,_capacity(s._capacity)
{
for (size_t i = 0; i < _size; ++i)
_a[i] = s._a[i];
}*/
//拷貝構(gòu)造函數(shù)現(xiàn)代寫法
SeqList(const SeqList<T>& s)
:_a(NULL)
{
SeqList<T> tmp(s._a, s._size);
swap(_a, tmp._a);
_size = s._size;
_capacity = s._capacity;
}
~SeqList()
{
if (_a)
delete[] _a;
}
//賦值運(yùn)算符重載常規(guī)寫法
SeqList<T>& operator=(const SeqList<T>& s)
{
if (this != &s)
{
T* tmp = new T[s._size];
for (size_t i = 0; i < s._size; ++i)
{
tmp[i] = s._a[i];
}
delete[] _a;
_a = tmp;
_size = s._size;
_capacity = s._capacity;
}
return *this;
}
//賦值運(yùn)算符重載現(xiàn)代寫法
/*SeqList<T>& operator=(SeqList<T> s)
{
if (this != &s)
{
swap(_a, s._a);
_size = s._size;
_capacity = s._capacity;
}
return *this;
}*/
public:
void Print()
{
for (size_t i = 0; i < _size; ++i)
{
cout<<_a[i]<<" ";
}
cout<<endl;
}
void PushBack(const T& x)
{
_CheckCapacity();
_a[_size++] = x;
}
void PopBack()
{
assert(_size > 0);
--_size;
}
void Insert(int pos, const T& x)
{
assert(pos >= 0 && pos <= _size);
_CheckCapacity();
int iIndex = _size;
while (iIndex > pos) //int和size_t比較為什么不行?
{
_a[iIndex] = _a[iIndex-1];
--iIndex;
}
_a[iIndex] = x;
++_size;
}
void Erase(size_t pos)
{
assert(_size > 0 && pos < _size);
size_t index = pos;
while (index < _size-1)
{
_a[index] = _a[index+1];
++index;
}
--_size;
}
int Find(const T& x)
{
for (size_t i = 0; i < _size; ++i)
{
if (_a[i] == x)
{
return i;
}
}
return -1;
}
T& operator[](size_t index)
{
assert(index >= 0 && index < _size);
return _a[index];
}
void Reserve(size_t size) //保留空間,增容到size
{
_capacity = size;
_a = (T*)realloc(_a, _capacity * sizeof(T));
}
void Clear() //不釋放空間
{
_size = 0;
}
void Size()
{
return _size;
}
protected:
void _CheckCapacity()
{
if (_size+1 > _capacity)
{
_capacity = _capacity*2;
_a = (T*)realloc(_a, _capacity * sizeof(T));
}
}
protected:
T* _a;
size_t _size;
size_t _capacity;
};
以上所述是小編給大家介紹的順序表的C++實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Qt中TableView與TreeView組件聯(lián)動(dòng)實(shí)現(xiàn)
本文主要介紹了Qt中TableView與TreeView組件聯(lián)動(dòng)實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
C++類與對(duì)象深入之構(gòu)造函數(shù)與析構(gòu)函數(shù)詳解
朋友們好,這篇播客我們繼續(xù)C++的初階學(xué)習(xí),現(xiàn)在對(duì)我們對(duì)C++非常重要的一個(gè)知識(shí)點(diǎn)做出總結(jié),整理出來一篇博客供我們一起復(fù)習(xí)和學(xué)習(xí),如果文章中有理解不當(dāng)?shù)牡胤?還希望朋友們?cè)谠u(píng)論區(qū)指出,我們相互學(xué)習(xí),共同進(jìn)步2022-06-06
C++11中的時(shí)間庫std::chrono(引發(fā)關(guān)于時(shí)間的思考)
這篇文章主要介紹了C++11中的時(shí)間庫std::chrono(引發(fā)關(guān)于時(shí)間的思考),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
C語言模擬實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言模擬實(shí)現(xiàn)學(xué)生學(xué)籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07

