C++模板实现list,迭代器
来源:互联网 发布:手机淘宝怎么关注 编辑:程序博客网 时间:2024/06/06 18:15
这篇博文主要是帮助去了解STL List与迭代器 算法实现的原理
让我们更明白STL容器的封装 迭代器与智能指针的关系 算法的封装
我们主要是通过链表的实现
下面看一下模板实现紫的链表
节点元素的封装:
#pragma oncetemplate <class T>class Node{public:T data;Node<T>* next;Node();~Node();};
链表的封装:
#pragma once#include "Node.h"#include "Myiterator.h"#include "Node.cpp"template <class T>class ForwardList{private:Node<T>* head;public:ForwardList();~ForwardList();void insert_back(T & data);void insert_front(T & data);void show();Myiterator<T> begin();Myiterator<T> end();void clear();};
#pragma once#include "Node.h"template <class T>class Myiterator{private:Node<T>* p;public:Myiterator():p(nullptr){}Myiterator(Node<T>* pnew):p(pnew){}~Myiterator(){}Node<T> operator*(){return *p;}Node<T>* operator->(){return p;}inline void operator++(){p = p->next;}inline void operator++(int){p = p->next;}bool operator !=(Myiterator<T>& myit){return this->p != myit.p;}};
下面就是List的实现代码:
#include "ForwardList.h"template <class T>ForwardList<T>::ForwardList() :head(nullptr){}template <class T>ForwardList<T>::~ForwardList(){clear();}template <class T>void ForwardList<T>::insert_back(T & data){Node<T>* newNode = new Node<T>();newNode->data = data;newNode->next = nullptr;if (this->head == nullptr){this->head = newNode;}else{Node<T>* tmp = this->head;while (tmp->next != nullptr){tmp = tmp->next;}tmp->next = newNode;}}template <class T>void ForwardList<T>::insert_front(T & data){Node<T>* newNode = new Node<T>();newNode->data = data;newNode->next = nullptr;if (this->head == nullptr){this->head = newNode;}else{newNode->next = this->head;this->head = newNode;}}template <class T>void ForwardList<T>::show(){Node<T>* tmp = this->head;while (tmp != nullptr){cout << "data = " << tmp->data << endl;tmp = tmp->next;}}template <class T>Myiterator<T> ForwardList<T>::begin(){return Myiterator<T>(this->head);}template <class T>Myiterator<T> ForwardList<T>::end(){Node<T>* tmp = this->head;while (tmp != nullptr){tmp = tmp->next;}return Myiterator<T>(tmp);}template <class T>void ForwardList<T>::clear(){if (this->head == nullptr){return;}Node<T>* p1 = this->head->next;Node<T>* p2;while (p1 != nullptr){p2 = p1->next;delete p1;p1 = p2;}delete this->head;this->head = nullptr;}
测试代码:
#include <iostream>#include "ForwardList.h"#include "ForwardList.cpp"using namespace std;template<class T,class F>void for_look(T begin,T end,F fun){for (auto ib = begin; ib != end; ib++){fun(*ib);}}void test(){ForwardList<int> mylist;for (int i = 0; i < 5; i++){mylist.insert_back(i);}int i = 100;mylist.insert_front(i);mylist.show();cout << "=========================" << endl;for (auto i : mylist){cout << i.data << endl;}cout << "==========================" << endl; for_look(mylist.begin(), mylist.end(), [](Node<int> node){cout << node.data << "<---->" << endl; });}void main(){test();cin.get();}
测试结果
是不是跟我们用STL List是一样的呢
阅读全文
0 0
- c++::用模板实现List
- 【c++】模板实现vector和list
- C++模板实现list,迭代器
- 使用模板类实现List容器&迭代器
- List 模板实现
- 模板类模拟实现List
- C++之模板实现list
- list 的c实现
- C++::模拟实现List
- C++List模板类的使用
- list 模板类的简单实现
- List模板类实现回文判断
- 单链表(List)的模板实现
- C++ 使用模板实现的一个List
- 以模板模拟实现List容器
- 双向链表List---类模板实现
- 类模板模拟实现STL中List
- list类的C++模板实现
- 读取数据的ff_read_packet 浅显分析
- 在解析XML文件的时候报出异常,应该有伪属性名
- UESTC 1591 An easy problem A ST表、简单题
- 如何搭建Selenium-Grid2环境(Java)
- OKHttp(三)---------post方式上传图片
- C++模板实现list,迭代器
- Android:LeakCanary 中文使用说明
- Android强制设置横屏或竖屏
- c++中的类总结
- ReactiveCocoa入门(2)元祖、json解析、应用
- 自定义View——弹性滑动
- [leetcode: Python]438. Find All Anagrams in a String
- xbox360 ubuntu14.04连接问题
- SVN服务器搭建和使用(二)