C++学习记录——십삼 List
创始人
2024-06-01 14:44:40
0

文章目录

  • 1、认识List
  • 2、list模拟实现


1、认识List

List实际上是一个双向带头循环列表。

想要遍历List就使用迭代器,方括号只是特定的一种,而迭代器则是通用的。

	list lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;

列表库中提供了一个sort,不用算法库中的sort,是因为算法库中的sort用了快排,需要三数取中,这对于链表来讲很不高效。虽然模板语法上可传任意类型参数,但内部使用迭代器对迭代器有要求,库里接口参数的名字会暗示要传随机迭代器。

迭代器分为三种,单向,双向,随机。单向迭代器例子就是单链表,只能++;链表就是双向迭代器,可++,可–;vector,string的迭代器就是随机迭代器,++ – 以及加减都可用。

要求传单向也可以传双向,随机,要求传双向也可以传随机。

不过列表用sort,显而易见,性能不够好。

2、list模拟实现

写完这篇后我修改了一下,完整代码在这里:

https://gitee.com/kongqizyd/start-some-c-codes-for-learning.c/tree/master/List

先开个头

	templatestruct list_node{list_node* next;list_node* prev;T data;};templateclass list{typedef list_node node;public:list(){head = new node;head->prev = head;head->next = head;}private:node* _head;};

添加插入函数

	struct list_node{list_node* next;list_node* prev;T data;list_node(const T& x):next(nullptr), prev(nullptr), data(x){}};
		void push_back(const T& x){node* tail = head->prev;node* newnode = new node(x);tail->next = newnode;newnode->prev = tail;newnode->next = head;head->prev = newnode;}

如果是单纯的链表结构,那么用一个指针指向一个节点,++就能到下一个节点,但是现在创建的链表不是这样,它的类型是list_node < int >*类型的,解引用后就是list_node,不一定能++正确,它不像链表一样是连续的物理空间。再造一个结构体,里面做些重载函数。

现有一个类list和一个结构体list_node, 在list之前再加一个结构体。

	templatestruct _list_iterator{typedef list_node node;typedef _list_iterator self;node* _node;_list_iterator(node* n):_node(n){}T& operator*(){return _node->data;}self& operator++(){_node = _node->next;return *this;}bool operator!=(const self& s){return _node != s._node;}};

然后在list类里写上测试代码

	void Test_list1(){list lt;lt.push_back(1);lt.push_back(2);lt.push_back(3);lt.push_back(4);list::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";++it;}cout << endl;for (auto e : lt){cout << e << " ";}cout << endl;}

用struct写是为了公开代码,让代码不受限制。测试代码中的迭代器,begin赋值给it,是浅拷贝,因为我们没写拷贝构造,但是也过了;是因为这里没写析构函数,而且节点不需要释放,因为节点不是迭代器结构体的,而是其他结构体的,所以它没有资格释放。除此之外,前后置+±-以及等于和不等于函数

		self& operator++(){_node = _node->next;return *this;}self operator++(int){self tmp(*this);_node = _node->next;return tmp;}self& operator--(){_node = _node->prev;return *this;}self operator--(int){self tmp(*this);_node = _node->prev;return tmp;}bool operator==(const self& s){return _node != s._node;}bool operator!=(const self& s){return _node != s._node;}

以及begin和end加上const版本和打印函数

		iterator begin(){return iterator(head->next);}iterator begin() const{return iterator(head->next);} iterator end(){return iterator(head);}iterator end() const{return iterator(head);}void print_list(const list& lt){list::iterator it = lt.begin();while (it != lt.end()){(*it) *= 2;cout << *it << " ";++it;}cout << endl;}

但是用const的begin和end构造出来的迭代器里面可以修改数值,这是因为他们构造出来普通迭代器,内容依然可以修改,const修饰的只是指针而已,不是内容。我们要返回const迭代器。

之前已经写过typedef _list_iterator< T > iterator,那么可不可以这样写
typedef const iterator const_iterator。

这就相当于T和T const,也就是这样把迭代器本身变成不可修改了。const迭代器应当可以++,我们要的是cosnt T*。

我们可以这样写,再创建一个结构体,然后把名字中加上const,和原有结构体区分开来。然后改一下这个函数

		const T& operator*(){return _node->data;}

list类里

	templateclass list{typedef list_node node;public:typedef _list_iterator iterator;typedef _list_const_iterator const_iterator;const_iterator end() const{return const_iterator(head);}const_iterator begin() const{return const_iterator(head->next);} 

打印函数里就是这样

list::const_iterator it = lt.begin();

然后把之前那个const结构体去掉,归到原先的结构体里,引入新的模板

	templatestruct _list_iterator{typedef list_node node;typedef _list_iterator self;node* _node;_list_iterator(node* n):_node(n){}Ref operator*(){return _node->data;}
	templateclass list{typedef list_node node;public:typedef _list_iterator iterator;typedef _list_iterator const_iterator;//typedef _list_const_iterator const_iterator;

如果传的是普通的,那就是T和T&。如果是const对象,那就是T和const T&,在_list_iterator结构体那里const T&就是Ref.

继续看迭代器的使用

	struct AA{int _a1;int _a2;AA(int a1, int a2):_a1(a1),_a2(a2){}};void test_list2(){list lt;lt.push_back(AA(1, 1));lt.push_back(AA(2, 2));lt.push_back(AA(3, 3));list::iterator it = lt.begin();while (it != lt.end()){cout << *it << " ";}cout << endl;}

程序会报错,因为it是AA的数据,需要重载<<。但还有别的办法

		list::iterator it = lt.begin();while (it != lt.end()){cout << it->_a1<< ":" << it->_a2 << " ";++it;}cout << endl;

本质是指针的迭代器,这样写貌似看起来更合理,那么就得重载->这个符号。在list_iterator写上这个函数

		T* operator->(){return &_node->data;}

其实本应当是it->->_a1。it一次解引用得到AA*,再次解引用得到AA,不过编译器就会识别为2个。原本的写法是这样:cout << it.operator->()->_a1 << “:” << it.operator->()->_a2 << " ";

如果这里是const,那么又要增加一个模板参数。

完善列表,写插入等函数。

		void push_back(const T& x){/*node* tail = head->prev;node* newnode = new node(x);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());}void pop_front(){erase(begin());}void insert(iterator pos, const T& x){node* cur = pos.node;node* _prev = cur->prev;node* newnode = new node(x);prev->next = newnode;newnode->prev = prev;newnode->next = cur;cur->prev = newnode;}void erase(iterator pos){assert(pos != end());node* prev = pos.node->prev;node* next = pos.node->next;prev->next = next;next->prev = prev;delete pos.node;}

写上clear函数以及要让erase函数传回来现在的位置还有析构函数。

		void clear(){iterator it = begin();while (it != end()){it = erase(it);}}
		iterator erase(iterator pos){assert(pos != end());node* prev = pos.node->prev;node* next = pos.node->next;prev->next = next;next->prev = prev;delete pos.node;return iterator(next);}
		~list(){clear();delete head;head = nullptr;}

迭代器区间函数,顺便改一下初始化。

		void empty_init(){head = new node;head->next = head;head->prev = head;}list(){empty_init();}templatelist(Iterator first, Iterator last){empty_init();while (firse != last){push_back(*first);++first;}}

拷贝构造,两种方式。

		void swap(list& tmp){std::swap(head, tmp.head);}list(const list& lt){/*empty_init();for (auto e : lt){push_back(e);}*/list tmp(lt.begin(), lt.end());swap(tmp)}

重载=

		list& operator=(list lt){swap(lt);return *this;}

结束。

相关内容

热门资讯

苹果怎么倒进安卓系统,一键倒装... 你有没有想过,把苹果手机里的宝贝倒腾到安卓系统里去?听起来是不是有点像变魔术?别急,今天就来手把手教...
安卓系统都能双系统么吗,揭秘双... 你有没有想过,你的安卓手机是不是也能来个“双胞胎”呢?没错,就是那种一个手机里同时运行两个操作系统,...
长安汽车升级安卓系统,安卓系统... 你知道吗?最近长安汽车可是来了一次大变身呢!没错,就是那个我们熟悉的国产汽车品牌,这次他们竟然升级了...
mac电脑装安卓系统,轻松实现... 亲爱的电脑迷们,你是否曾幻想过在你的Mac电脑上运行安卓系统?想象那些你钟爱的安卓应用,在你的Mac...
安卓p系统流畅吗,畅享无忧 你有没有发现,最近安卓P系统成了大家热议的话题呢?不少朋友都在问,这个新系统到底流畅不流畅啊?今天,...
剑灵2安卓系统,畅游东方奇幻世... 你知道吗?最近在安卓系统上,有一款游戏可是火得一塌糊涂,那就是《剑灵2》!这款游戏不仅画面精美,操作...
安卓系统是否指定品牌,品牌定制... 你有没有想过,为什么你的安卓手机总是那么独特,而别人的安卓手机却看起来差不多呢?这背后,其实隐藏着一...
安卓系统和iso系统 照片共享... 你有没有发现,现在手机拍照功能越来越强大,拍出来的照片美得不要不要的!但是,当你想和朋友们分享这些精...
安卓系统领夹麦,便携式音频解决... 你有没有发现,现在手机通话越来越方便了,但是有时候,手机那点小小的麦克风,真的有点力不从心呢!尤其是...
安卓系统经常无法唤醒,探究系统... 你是不是也遇到过这种情况?手机屏幕黑了,手指在屏幕上疯狂地滑动,却怎么也唤醒不了安卓系统。这可真是让...
自己编译安卓系统源码,编译安卓... 你有没有想过,安卓系统其实就像一个巨大的宝藏,里面藏着无数的秘密和可能性?今天,就让我带你一起探索这...
鸿蒙系统属于安卓,基于安卓的全... 你知道吗?最近有个话题在科技圈里可是闹得沸沸扬扬的,那就是鸿蒙系统。没错,就是那个华为自主研发的操作...
lephone是安卓系统吗,深... 你有没有听说过lephone这个品牌呢?最近,身边的朋友都在讨论这个手机,说它性价比超高,但是,有人...
pc双系统安卓系统下载软件,P... 你有没有想过,在电脑上同时运行安卓系统,是不是就像在手机上玩电脑游戏一样酷炫呢?没错,这就是今天我要...
电量壁纸安卓系统设置,安卓系统... 手机电量告急,是不是又得赶紧找充电宝了?别急,今天就来和你聊聊如何通过设置电量壁纸来给安卓系统来个“...
ip是安卓系统吗,通过IP地址... 你有没有想过,那个陪伴你每天刷剧、玩游戏、办公的IP,它是不是安卓系统呢?别急,今天就来揭开这个谜底...
安卓系统谁负责升级,揭秘幕后负... 你有没有想过,你的安卓手机为什么有时候会突然收到系统更新的通知呢?是不是好奇,是谁在背后默默地为你的...
安卓系统需要降级吗,安卓系统升... 你有没有发现,你的安卓手机最近有点儿“老态龙钟”了呢?运行速度慢吞吞的,有时候还卡个不停。这时候,你...
性价比手机安卓系统,盘点安卓系... 你有没有想过,在这个手机更新换代如此迅速的时代,如何用最少的钱,买到最满意的手机呢?没错,我要说的是...
虚拟大师安卓2.0系统,安卓新... 你有没有听说最近虚拟大师安卓2.0系统火得一塌糊涂?这可不是空穴来风,而是真的让不少手机用户都跃跃欲...