C++List容器常用函數(shù)接口刨析
一、基本結(jié)構(gòu)
由源代碼可知,list容器是有一個(gè)帶頭雙向循環(huán)鏈表實(shí)現(xiàn),所以我們模擬實(shí)現(xiàn)也需要實(shí)現(xiàn)一個(gè)帶頭雙向循環(huán)鏈表的數(shù)據(jù)結(jié)構(gòu)。
template<class T>
struct list_node
{
list_node<T>* _next;
list_node<T>* _prev;
T _data;
list_node(const T& val = T())//用一個(gè)匿名對(duì)象來(lái)做缺省參數(shù)
:_next(nullptr)
, _prev(nullptr)
, _data(val)
{}
};
template<class T>
class list
{
public:
typedef list_node<T> Node;
private:
Node* _head;
};二、list的迭代器的構(gòu)造
list的迭代器與vector的迭代器不一樣,list的迭代器是一個(gè)自定義類型的對(duì)象,成員變量包含一個(gè)指向節(jié)點(diǎn)的指針。
template<class T, class Ref, class Ptr>
struct __list_iterator
{
typedef list_node<T> Node;
typedef __list_iterator<T, Ref, Ptr> self;
Node* _node;
__list_iterator(Node* node)
:_node(node)
{}
// 析構(gòu)函數(shù) -- 節(jié)點(diǎn)不屬于迭代器,不需要迭代器釋放
// 拷貝構(gòu)造和賦值重載 -- 默認(rèn)生成的淺拷貝就可以
// *it
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
//return &(operator*());
return &_node->_data;
}
self& operator++()
{
_node = _node->_next;
return *this;
}
self operator++(int)
{
self tmp(*this);//拷貝構(gòu)造
_node = _node->_next;
return tmp;//因?yàn)閠mp出了作用域就不在了,所以不可以引用返回
}
self& operator--()
{
_node = _node->_prev;
return *this;
}
self operator--(int)
{
self tmp(*this);
_node = _node->_prev;
return tmp;
}??????即用一個(gè)自定義類型封裝,通過(guò)運(yùn)算符的重載使迭代器實(shí)現(xiàn)像指針一樣的操作行為。
三、迭代器的實(shí)現(xiàn)
template<class T>
class list
{
typedef list_node<T> Node;
public:
typedef __list_iterator<T, T&, T*> iterator;
typedef __list_iterator<T, const T&, const T*> const_iterator;
//僅僅是為了改名,如果不是為了改名,不用寫(xiě)。
__list_iterator<T, const T&, const T*> begin() const
{
// list_node<int>*
return __list_iterator<T, const T&, const T*>(_head->_next);
//構(gòu)造一個(gè)匿名對(duì)象
}
const_iterator end() const
{
return const_iterator(_head);
}
iterator begin()
{
return iterator(_head->_next);//構(gòu)造一個(gè)匿名對(duì)象來(lái)返回
//return _head->_next;//也可以,因?yàn)閱螀?shù)構(gòu)造函數(shù)支持隱式類型轉(zhuǎn)換。
//iterator it = _head->_next 隱式調(diào)用
}
iterator end()
{
return iterator(_head);
}
}四、insert,erase
// 插入在pos位置之前
iterator insert(iterator pos, const T& x)//pos是一個(gè)迭代器對(duì)象
{
Node* newNode = new Node(x);
Node* cur = pos._node;
Node* prev = cur->_prev;
// prev newnode cur
prev->_next = newNode;
newNode->_prev = prev;
newNode->_next = cur;
cur->_prev = newNode;
return iterator(newNode);
}
iterator erase(iterator pos)
{
assert(pos != end());
Node* cur = pos._node;
Node* prev = cur->_prev;
Node* next = cur->_next;
// prev next
prev->_next = next;
next->_prev = prev;
delete cur;
return iterator(next);
}五、push_back,push_front,pop_back,pop_front
void push_back(const T& x)
{
//Node* tail = _head->_prev;
//Node* newnode = new Node(x);
_head tail newnode
//tail->_next = newnode;
//newnode->_prev = tail;
//newnode->_next = _head;
//_head->_prev = newnode;
insert(end(), x);
}
void push_front(const T& x)
{
insert(begin(), x);
}
void pop_back()
{
erase(--end());
//這里不可以用end()-1吧,因?yàn)槲膊康髟谖矂h后是需要變得
}
void pop_front()
{
erase(begin());
}??這里均復(fù)用了insert和erase函數(shù)。
六、構(gòu)造函數(shù)與賦值重載
list()//帶頭雙向循環(huán)鏈表,初始化要先把頭弄出來(lái)
{
_head = new Node();
//自定義類型去調(diào)用對(duì)應(yīng)類的構(gòu)造函數(shù),帶不帶這個(gè)括號(hào)都可
_head->_next = _head;
_head->_prev = _head;
}
// lt2(lt1)————傳統(tǒng)寫(xiě)法
/*list(const list<T>& lt)
{
_head = new Node();
_head->_next = _head;
_head->_prev = _head;
for (auto e : lt)
{
push_back(e);//push_back中復(fù)用insert,insert中完成深拷貝
}
}*/
void empty_init()
{
_head = new Node();
_head->_next = _head;
_head->_prev = _head;
}
//如果我們寫(xiě)現(xiàn)代寫(xiě)法,那么必須提供相應(yīng)的帶參構(gòu)造
template <class InputIterator>
list(InputIterator first, InputIterator last)
{
empty_init();
while (first != last)
{
push_back(*first);//push_back中調(diào)用insert時(shí)會(huì)完成相應(yīng)深拷貝
++first;
}
}
void swap(list<T>& lt)
{
std::swap(_head, lt._head);//交換頭節(jié)點(diǎn)
}
// lt2(lt1) -- 現(xiàn)代寫(xiě)法
list(const list<T>& lt)
{
empty_init();//總不能把一個(gè)野指針換給別人呀!
list<T> tmp(lt.begin(), lt.end());
swap(tmp);
}
// lt2 = lt1
list<T>& operator=(list<T> lt)
//list<T> lt = lt1,傳值傳參這一步就調(diào)用了拷貝構(gòu)造完成深拷貝
{
swap(lt);
return *this;
}??????注意現(xiàn)代寫(xiě)法的方法
七、析構(gòu)與清空
~list()
{
clear();
delete _head;
_head = nullptr;
}
void clear()
{
iterator it = begin();
while (it != end())
{
it = erase(it);//用返回值更新,防止迭代器失效
}
}到此這篇關(guān)于C++List容器常用函數(shù)接口刨析的文章就介紹到這了,更多相關(guān)C++List容器 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++數(shù)據(jù)結(jié)構(gòu)之二叉搜索樹(shù)的實(shí)現(xiàn)詳解
二叉搜索樹(shù)作為一個(gè)經(jīng)典的數(shù)據(jù)結(jié)構(gòu),具有鏈表的快速插入與刪除的特點(diǎn),同時(shí)查詢效率也很優(yōu)秀,所以應(yīng)用十分廣泛。本文將詳細(xì)講講二叉搜索樹(shù)的C++實(shí)現(xiàn),需要的可以參考一下2022-08-08
C語(yǔ)言中g(shù)etchar()的原理以及易錯(cuò)點(diǎn)解析
用getchar()函數(shù)讀取字符串時(shí),字符串會(huì)存儲(chǔ)在輸入緩沖區(qū)中,包括輸入的回車字符,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中g(shù)etchar()的原理以及易錯(cuò)點(diǎn)解析的相關(guān)資料,需要的朋友可以參考下2022-03-03
C++中成員函數(shù)和友元函數(shù)的使用及區(qū)別詳解
大家好,本篇文章主要講的是C++中成員函數(shù)和友元函數(shù)的使用及區(qū)別詳解,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
C++中實(shí)現(xiàn)保存數(shù)據(jù)到CSV文件
這篇文章主要介紹了C++中實(shí)現(xiàn)保存數(shù)據(jù)到CSV文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
C語(yǔ)言壓縮文件和用MD5算法校驗(yàn)文件完整性的實(shí)例教程
這篇文章主要介紹了C語(yǔ)言壓縮文件和用MD5算法校驗(yàn)文件完整性的實(shí)例教程,這里演示了Windows下將文件壓縮為7z格式以及MD5檢驗(yàn)文件和密碼的方法,需要的朋友可以參考下2016-04-04
C語(yǔ)言中分支和循環(huán)的6種實(shí)現(xiàn)形式總結(jié)
C語(yǔ)言時(shí)一門結(jié)構(gòu)化的程序設(shè)計(jì)語(yǔ)言,這篇文章主要介紹了C語(yǔ)言中的分支和循環(huán)的6種實(shí)現(xiàn)形式,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04

