错误关联器 C++实现
来源:互联网 发布:java集合类有哪些 编辑:程序博客网 时间:2024/06/03 12:14
-----------------------------------------------ErrorCorrelator.h----------------------------------------------#include<ostream>
#include<string>
#include<queue>
#include<stdexcept>
//error 类声明
class Error
{
public:
Error(int priority,std::string errMsg):
mPriority(priority),mError(errMsg){}
int getPriority() const {return mPriority;}
std::string getErrorString() const {return mError;}
friend bool operator<(const Error& lhs,const Error &rhs);
friend std::ostream& operator<<(std::ostream &str,const Error &err);
protected:
int mPriority;
std::string mError;
};
//error类容器,返回最高优先级的错误
class ErrorCorrelator
{
public:
ErrorCorrelator(){};
////向优先队列中添加元素
void addError(const Error& error);
//取得最优先元素
Error getError() throw (std::out_of_range);
protected:
std::priority_queue<Error> mErrors;
private:
//prevent assignment and pass-by-refernence
ErrorCorrelator(const ErrorCorrelator &src);
ErrorCorrelator & operator=(const ErrorCorrelator &rhs);
};
//比较符<重载
bool operator<(const Error& lhs,const Error &rhs)
{
return lhs.mPriority<rhs.mPriority;
}
//输出符<<重载
std::ostream& operator<<(std::ostream &str,const Error &err)
{
str << err.mError <<"(priority "<<err.mPriority<<")"<<std::endl;
return (str);
}
//向优先队列中添加元素
void ErrorCorrelator::addError(const Error &error)
{
mErrors.push(error);
}
//取得最优先元素
Error ErrorCorrelator::getError() throw(std::out_of_range)
{
//判断优先队列是否为空
if(mErrors.empty())
{
throw(std::out_of_range("No elements!"));
}
//获取对头元素
Error top = mErrors.top();
// 弹出队头元素
mErrors.pop();
return top;
}
---------------------------------------------------主函数-------------------------------------------------------
#include "stdafx.h"
#include<iostream>
#include "ErrorCorrelator.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ErrorCorrelator ec;
ec.addError(Error(3,"Unable to read file!"));
ec.addError(Error(1,"Incorrect entry from user!"));
ec.addError(Error(10,"Unable to allocate memory!"));
while(1)
{
try
{
Error e = ec.getError();
cout << e << endl;
}
catch(out_of_range&)
{
cout<<"Finished processing errors!"<<endl;
break;
}
}
system("pause");
return 0;
}
-----------------------------------------------------程序测试--------------------------------------------------Unable to allocate memory!(priority 10)
Unable to read file!(priority 3)
Incorrect entry from user!(priority 1)
Finished processing errors!
请按任意键继续. . .
- 错误关联器 C++实现
- 错误关联器 C++实现
- Objective-c语言_错误(视图多重关联)
- Objective-C的Category与关联对象实现原理
- objective-c中关联引用的底层实现
- EXE关联错误
- 数据关联错误
- Q:实现IContextMenu出现C2787错误:“IContextMenu”:没有与该对象关联的GUID
- Q:实现IContextMenu出现C2787错误:“IContextMenu”:没有与该对象关联的GUID
- C++primer 关联容器
- 《C++primer》关联容器
- C++MAP关联容器
- objective-c关联
- Objective-C关联引用
- [Objective-C]关联
- Objective-C关联引用
- C++map关联容器
- 数据结构-关联容器(C++)
- TabHost使用小结
- The Best 404 Error Pages Of All Time
- 快速排序的一种写法
- 二分查找的递归算法(折半查找)
- Linux 如何挂载光驱
- 错误关联器 C++实现
- 每日一题【二】-数组元素去重
- zoj 2100 Seeding(dfs)
- 学习笔记:shared_ptr陷阱
- VS2010下配置OpenCV2.2运行环境
- qt4 http
- OpenCms数据备份与转移,OpenCms 数据备份 转移 与 还原
- NSString category:unrecognized selector sent to instance
- (2011.11.20)02_循环链表举例_约瑟夫(Joseph)环问题.cpp