C++primer 9.2.1节练习
练习9.3
迭代器begin和end必须指向相同的容器,end可以与begin指向相同的位置,但不能指向begin之前的位置;
练习9.4
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <vector> 5 #include <iterator> 6 7 using namespace std; 8 9 bool checkValue(vector<int>::iterator it, vector<int>::iterator it1, int a); 10 11 int main() 12 { 13 vector<int> num{ 1,2,3,4,5,6 }; 14 cout << checkValue(num.begin(), num.end(), 7) << endl; 15 system("pause"); 16 return 0; 17 } 18 19 bool checkValue(vector<int>::iterator it, vector<int>::iterator it1, int a) 20 { 21 while (it != it1) 22 { 23 if (*it == a) 24 return true; 25 else 26 ++(it); 27 } 28 return false; 29 }
练习9.5
1 #include <iostream> 2 #include <fstream> 3 #include <sstream> 4 #include <vector> 5 #include <iterator> 6 7 using namespace std; 8 9 const vector<int>::iterator *checkValue (vector<int>::iterator it, vector<int>::iterator it1, int a); 10 11 int main() 12 { 13 vector<int> num{ 1,2,3,4,5,6 }; 14 cout << (checkValue(num.begin(), num.end(), 7)) << endl; 15 system("pause"); 16 return 0; 17 } 18 19 const vector<int>::iterator *checkValue (vector<int>::iterator it, vector<int>::iterator it1, int a) 20 { 21 while (it != it1) 22 { 23 try 24 { 25 if (*it == a) 26 return ⁢ 27 else 28 ++(it); 29 if (it == it1) 30 throw runtime_error("error"); 31 } 32 catch (runtime_error err) { 33 cout << err.what() << endl; 34 } 35 } 36 return &it1; 37 }
利用抛出异常的方法来处理未找到定值的情况;
练习9.6
迭代器支持的算术运算不能用于list容器,因为list容器不是按照顺序存储的,他是个双向链表;
改成while(iter1 != iter2)
转载于:https://www.cnblogs.com/wuyinfenghappy/p/7326968.html
C++primer 9.2.1节练习相关推荐
- C++primer 10.2.1节练习
练习10.3 1 #include<iostream> 2 #include<string> 3 #include<vector> 4 #include <s ...
- C++primer 13.1.6节练习
练习13.18 1 #include <iostream> 2 #include <string> 3 #include <memory> 4 5 using na ...
- 《C++ Primer》10.1节练习
练习10.1: #include <iostream> #include <vector> #include <algorithm> using namespace ...
- 《C++ Primer》14.4节练习(部分)
练习14.20: class Sales_data {friend Sales_data operator+(const Sales_data &lhs,const Sales_data &a ...
- 《C++ Primer》14.3节练习
练习14.13: 对于Sales_data类,其实我们并不需要再为它添加其他算术运算符.但是这里我们可以考虑为它实现一个减法运算符. class Sales_data {friend Sales_da ...
- 《C++ Primer》14.1节练习
练习14.1: 不同点: 重载操作符必须具有至少一个class或枚举类型的操作数. 重载操作符不保证操作数的求值顺序,例如对&&和||的重载版本不再具有"短路求值" ...
- 《C++ Primer》7.4节练习
练习7.33: 题目代码: pos Screen::size()const {return height*width; } 如果添加如题目所示的size函数将会出现编译错误.因为该函数的返回类型pos ...
- 《C++ Primer》7.2节练习
练习7.16: 在类的定义中,可以包含0个或者多个访问说明符,并且对于某个访问说明符能出现多少次以及能出现在哪里都没有严格规定.每个访问说明符指定接下来的成员的访问级别,有效范围直到出现下一个访问说明 ...
- 《C++ Primer》1.51节练习
练习1.20 #include <iostream> #include "Sales_item.h" using namespace std;int main() {S ...
最新文章
- 神策数据:春节放假通知
- android开发期间使用真机调试但系统无法识别出真机
- D3DCOLOR与D3DXCOLOR
- qrect在图片上显示矩形框_Mac上用LabelImg手动标记图片
- java三元运算符_java三元运算符详解
- ARM架构——面试了菊厂,中兴,MTK,鼎桥,总结了ARM Cortex-M3中断
- 计算机算法设计与分析 第4版 (王晓东) 重点题
- wifi模块和51单片机相连的问题
- 字节社招经历:5年Java开发经验,半月3次面试,成功拿到 Offer
- java进阶笔记之常用(通用)Map(Hash,Tree,Linked,Properties等)
- 解析“Web3悖论”的内在机理与突破路径(上篇·悖论)
- Google I/O开发者大会第二弹之未来
- 每日新闻摘要:TrueCaller的隐私含义
- 如何预防猪流感!猪流感!?会像SARS那样爆发吗?我们能作的就是预防。
- 陈小春版韦小宝七个老婆简介
- 《机器学习实战》决策树的应用
- 风光柴储微电网最优化经济调度模型(Matlab+Yalmip+Cplex)——附代码
- 经典批处理实现自动关机.BAT
- 极客标签前端特效资源精品大荟萃#003
- 《小明升职记》(七)