论坛
潜水/灌水快乐,沉淀知识,认识更多同行。
ToB圈子
加入IT圈,遇到更多同好之人。
朋友圈
看朋友圈动态,了解ToB世界。
ToB门户
了解全球最新的ToB事件
博客
Blog
排行榜
Ranklist
文库
业界最专业的IT文库,上传资料也可以赚钱
下载
分享
Share
导读
Guide
相册
Album
记录
Doing
应用中心
搜索
本版
文章
帖子
ToB圈子
用户
免费入驻
产品入驻
解决方案入驻
公司入驻
案例入驻
登录
·
注册
账号登录
立即注册
找回密码
用户名
Email
自动登录
找回密码
密码
登录
立即注册
首页
找靠谱产品
找解决方案
找靠谱公司
找案例
找对的人
专家智库
悬赏任务
圈子
SAAS
qidao123.com技术社区-IT企服评测·应用市场
»
论坛
›
大数据
›
数据仓库与分析
›
C++——观察者模式,3个工厂模式,迭代器模式,异常处理 ...
C++——观察者模式,3个工厂模式,迭代器模式,异常处理 ...
缠丝猫
论坛元老
|
2025-4-14 04:08:08
|
显示全部楼层
|
阅读模式
楼主
主题
1675
|
帖子
1675
|
积分
5025
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class myException:public exception{
private:
public:
const char* what()const noexcept{
return "越界访问";
}
};
template <class T>
class myList{
public:
struct Node{
T val;
Node* next;
Node* prev;
};
class iterator{
private:
Node* ptr;
public:
iterator(Node* ptr=NULL);
T& operator*();
bool operator!=(const iterator& r);
iterator& operator++(int);
iterator& operator++();
};
myList();
void push_back(const T& val);
myList& operator<<(const T& val);
T& operator[](int index);
int size();
iterator begin();
iterator end();
private:
Node* head; //真正的链表(链表头头节点)
Node* tail; // 链表尾节点
int count;
};
//===========================================
//迭代器代码
template <typename T>
myList<T>::iterator::iterator(Node* ptr)
:ptr(ptr){}
template <typename T>
T& myList<T>::iterator::operator*(){
return ptr->val;
}
template <typename T>
bool myList<T>::iterator::operator!=(const iterator& r){
return ptr!=r.ptr;
}
template <typename T>
typename myList<T>::iterator& myList<T>::iterator::operator++(int){
if(ptr==NULL)
{
throw myException();
}
ptr=ptr->next;
return *this;
}
template <typename T>
typename myList<T>::iterator& myList<T>::iterator::operator++(){
if(ptr==NULL)
{
throw myException();
}
ptr=ptr->next;
return *this;
}
template <typename T>
typename myList<T>::iterator myList<T>::begin(){
iterator it(head->next);
return it;
}
template <typename T>
typename myList<T>::iterator myList<T>::end(){
iterator it(tail->next);
return it;
}
template <typename T>
myList<T>::myList(){
head = new Node;
head->next = NULL;
head->prev = NULL;
tail = head; // 只有头节点的情况下,尾节点即使头节点
count = 0;
}
template <typename T>
void myList<T>::push_back(const T& val){
Node* newnode = new Node;
newnode->val = val;
newnode->next = NULL;
newnode->prev = tail;
tail->next = newnode;
tail = newnode;
count ++;
}
template <typename T>
myList<T>& myList<T>::operator<<(const T& val){
push_back(val);
// return 0
return *this;
}
template <typename T>
T& myList<T>::operator[](int index){
if(index<0 || index>=count){
throw myException();
}
Node* p = head->next;
for(int i=0;i<index;i++){
p = p->next;
}
return p->val;
}
template <typename T>
int myList<T>::size(){
return count;
}
int main(int argc,const char** argv){
myList<int> l;
l << 1 << 3 << 5 << 7 << 9;
try{
cout << l[5] << endl;
}catch(const myException& e){
cout << "捕获异常:" << e.what() <<endl;
}
try{
myList<int>::iterator it=l.end();
++it;
}catch(const myException& e){
cout << "捕获异常:" << e.what() <<endl;
}
return 0;
}
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
本帖子中包含更多资源
您需要
登录
才可以下载或查看,没有账号?
立即注册
x
回复
使用道具
举报
0 个回复
倒序浏览
返回列表
快速回复
高级模式
B
Color
Image
Link
Quote
Code
Smilies
您需要登录后才可以回帖
登录
or
立即注册
本版积分规则
发表回复
回帖并转播
回帖后跳转到最后一页
发新帖
回复
缠丝猫
论坛元老
这个人很懒什么都没写!
楼主热帖
【设置ssh免密不起作用?彻底搞懂密钥 ...
关于Servlet的补充知识
MySQL 8.0 新特性梳理汇总
kubernetes之镜像拉取策略ImagePullSec ...
【云原生】Docker 进阶 -- 构建自定义 ...
java如何编写增强for循环呢? ...
基于C#+unity的2D跑酷闯关对战冒险游戏 ...
常见开发模型-敏捷开发与瀑布开发模型 ...
【深度思考】一线开发大头兵对于工作的 ...
在chatGPT的帮助下成功从Rancher中删除 ...
标签云
国产数据库
集成商
AI
运维
CIO
存储
服务器
浏览过的版块
开源技术
快速回复
返回顶部
返回列表