STL List部分函式實現

2021-07-22 17:09:03 字數 2315 閱讀 4257

前面一篇文章講了stl::list用法詳解,簡單介紹list的一些函式,在這分享一下雙向鍊錶的部分簡單函式實現

因為鍊錶會出現頻繁的增加、刪除,所以使用迭代器較為簡單

先定義節點類,

struct listnode

};

然後需要定義迭代器類
在迭代器這塊,需要實現一些函式以供呼叫,例如operator++、operator++(int)、operator-、operator*、operator==和operator!=等一些函式。
template struct listiterator

listiterator()

{} ref operator*()//operator->

bool operator==(const self&s)const

bool operator!=(const self&s)const

self& operator++()//前置加加

self operator++(int)//後置加加

self& operator--()

self operator--(int)

node *_node;

};

這就需要定義鍊錶類了,此涉及節點類,迭代器類的呼叫,要提前宣告,涉及構造、析構、頭插、頭刪、尾插、尾刪、指定位置插入

指定位置刪除、指定位置插入指定元素、去重、查詢、清空等函式

當然插入和刪除節點順序尤為重要,在此就不做詳細介紹,記得以前有過此類問題詳解

template class list

list()

void pushback(const t&x)

void pushfront(const t&x)

void popback()

void popfront()

//在指定位置前插入

void insert(iterator pos,const t&x)

void erase(iterator& pos)

iterator begin()

iterator end()

private:

node*_head;

};

除錯函式

#include

#include "list.h"

#include

using namespace std;

void testlist()

cout<

}int main()

源**:

list.h

#pragma once

#includetemplate struct listnode

};template struct listiterator

listiterator()

{} ref operator*()//operator->

bool operator==(const self&s)const

bool operator!=(const self&s)const

self& operator++()//前置加加

self operator++(int)//後置加加

self& operator--()

self operator--(int)

node *_node;

};template class list

list()

void pushback(const t&x)

void pushfront(const t&x)

void popback()

void popfront()

//在指定位置前插入

void insert(iterator pos,const t&x)

void erase(iterator& pos)

iterator begin()

iterator end()

private:

node*_head;

};

test.cpp

#include

#include "list.h"

#include

using namespace std;

void testlist()

cout<}

int main()

C 中使用STL list 和find函式

首先find的作用 在具有指定值的範圍內找到元素首次出現的位置。template inputiterator find inputiterator first,乙個輸入迭代器,它在要搜尋指定值的範圍內定址第乙個元素的位置。inputiterator last,乙個輸入迭代器,用於在要搜尋的指定值範圍...

STL list的簡單用法和實現

list是由資料結構中的雙向鍊錶實現的可以高效地支援任意地方的刪除和插入 記憶體空間可以是不連續的,只能通過指標來進行資料的訪問,使得它的隨機訪問變的非常沒有效率。使用c 標準庫裡面的list時需加標頭檔案 include,且是屬於std域,所以還應加上using namespace std.lis...

實現部分庫函式

1.模擬實現strncat 與strcat無異,只是追加的塊大小不一樣,strncat只是向後追加n個位元組的內容 char my strncat char dst,const char src,int count while count 用數count控制迴圈的次數 dst src dst 0 r...