c++17(17)-异常try catch,operator[],vector at
0,精通Python,62.37==>1
1,精通C++,49.21==>1
1,精通C++,49.21==>1
访问索引超过范围Hit any key to continue...
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;class Book
{private:int _id;char* _name;float _price;static inline int nowid{0};int _bookCount{1};
public : Book(int id, char* name, float price): _id(id), _price(price){int nameLen = strlen(name);_name = new char[nameLen + 1];strcpy(_name, name);}Book(char* name, float price):Book(nowid,name,price){nowid++;}~Book(){delete[] _name;}friend const Book& operator +(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount+rightBook._bookCount;return *book;}else{return leftBook;}}friend const Book& operator -(const Book& leftBook,const Book& rightBook){if (leftBook==rightBook) {Book *book=new Book(rightBook._name, rightBook._price); book->_bookCount=leftBook._bookCount-rightBook._bookCount;if ( book->_bookCount<0) book->_bookCount=0;return *book;}else{return leftBook;} } Book& operator -=(const Book& otherBook){if (*this==otherBook) {_bookCount-=otherBook._bookCount;if ( _bookCount<0) _bookCount=0;return *this;}else{return *this;} } Book& operator +=(const Book& otherBook){if (*this==otherBook) {_bookCount+=otherBook._bookCount;return *this;}else{return *this;} } friend const bool operator ==(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return true;}else{return false;} } friend const bool operator !=(const Book& leftBook,const Book& rightBook){if (strcmp (rightBook._name,leftBook._name) == 0 && rightBook._price==leftBook._price) {return false;}else{return true;} } const void print() const{cout<<_id<<","<<_name<<","<<_price<<"==>"<< _bookCount<<endl;}void printCount(){cout<<_name<<":"<<_price<<"==>"<< _bookCount<<endl; }
};
class BookS{private:vector<Book*> _myBooks;
public:void addBook(Book* book){_myBooks.push_back(book); }void print() {for(const Book *bk:_myBooks){bk->print();}}Book& operator[](int i){ return *(_myBooks.at(i));}};int main(int argc, char **argv)
{Book *book1=new Book("精通Python",62.37); Book *book2=new Book("精通C++",49.21); BookS myBks;myBks.addBook(book1); myBks.addBook(book2); myBks.print();try{ myBks[1].print();myBks[2].print(); }catch(std::out_of_range e){cout<<"访问索引超过范围"<<endl;}}
下面对[]的重载,对vector使用.at的方式访问,这样可以抛出超范围异常,如果直接使用[]将导致超过范围
Book& operator[](int i){ return *(_myBooks.at(i));}
c++17(17)-异常try catch,operator[],vector at相关推荐
- c++异常Try catch
c++异常Try catch 分类: c++专区 功能函数测试集锦2014-04-09 17:43 1505人阅读 评论(0) 收藏 举报 一.简单的例子 首先通过一个简单的例子来熟悉C++ 的 tr ...
- [leetcode 面试题 17.17] -- 多次搜索,KMP与字典树
[leetcode 面试题 17.17] -- 多次搜索 题目来源 分析 KMP思路 完整代码 字典树 完整代码 题目来源 https://leetcode-cn.com/problems/multi ...
- 字典树/Trie/前缀树-LeetCode总结:720词典中最长的单词;127. 单词接龙;677. 键值映射;面试题 17.17. 多次搜索;648. 单词替换
MyTrie结构体和相关操作函数 typedef struct MyTrie {bool is_word;vector<MyTrie*> next;MyTrie():is_word(fal ...
- 解决微信H5获取SDK授权报错提示errMsg: “config:fail,Error: 系统错误,错误码:63002,invalid signature [20200908 22:17:17][]“
如果常规检查都做过可以仔细看下微信开放文档 这个里面的报错原因 如果都不是那么极有可能是(恭喜你!看样子你的H5页面日活人数还蛮多[呲牙]) 获取腾讯微信平台access_token超过每日默认上限2 ...
- 异常 try catch finally return 执行关系 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 分解成质因数(如435234=251*17*17*3*2
分解成质因数(如435234=251*17*17*3*2,据说是华为笔试题) void prim(int m, int n) { if(m>n) { while(m%n != 0) n++ ...
- 03/13 17:17:57: Launching 'app' on OPPO PBEM00. Installation did not succeed. The application could
AndroidStudio连接真机测试项目时可能会报如下错误: 03/13 17:17:57: Launching 'app' on OPPO PBEM00. Installation did not ...
- C++ nlohmann/json 的主要用法(cout、printf打印,异常try...catch处理)
文章目录 前言 准备工作 引用头文件: 主要用法: 一.声明与构造 1 纯粹声明 2 内容构造 二.解析与序列化 字符串 解析 序列化 文件:// 比如有文件 c:\rankings.json,其内容 ...
- java多层catch语句_Java异常之catch语句块
今天在阅读项目代码时看到如下奇怪的代码,以为是竖线 | 可以作为多类型赋值,经过网上搜索才知道是Java SE 7新增的功能:一个catch捕获多种类型的异常.原文是Java官方的文档,现翻译如下.红 ...
最新文章
- 漫画:如何用 K8s 实现 CI/CD 发布流程?
- Ubuntu下 安装 window 虚拟机
- Pair Testing
- a different object with the same identifier val...
- php页面调用时间戳,php--------获取当前时间、时间戳
- 04-CoreData Stack技术栈堆手动实现
- 1612D. X-Magic Pair
- 论文浅尝 - ICLR2021 | BERTology 遇上生物学:在蛋白质语言模型中解释注意力
- dos系统重启计算机名,dos系统重启的命令是是
- PyTorch | 通过torch.arange创建等差数列张量 | torch.arange()如何使用?torch.arange()使用方法 torch.arange()举例说明
- java中枚举表示数据状态
- Fax Helper
- Sklearn——用Sklearn实现线性回归(LinearRegression)
- dz3 php post 登录,discuz X3用户登录uc_user_login()函数详解
- 随机优化算法–爬山法VS模拟退火算法
- c++priority_queue详解
- 《王者荣耀》一次上最强王者的心路历程
- 正则表达式re.S与re.M的区别
- 被Win10自带杀毒软件自动误删除的exe从这里找
- Docker测试环境笔记