C++primer 9.5.5节练习
练习9.50
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 5 using namespace std; 6 7 int main() 8 { 9 int sum = 0; 10 int num; 11 vector<string> vec{ "12","23","1","34","13","99" }; 12 for (auto it = vec.begin(); it != vec.end(); ++it) 13 { 14 num = stoi(*it); 15 sum += num; 16 } 17 cout << sum << endl; 18 system("pause"); 19 return 0; 20 }
修改后
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 5 using namespace std; 6 7 int main() 8 { 9 double sum = 0; 10 double num; 11 vector<string> vec{ "12.0","23.9","1.0","34.0","13.0","99.0" }; 12 for (auto it = vec.begin(); it != vec.end(); ++it) 13 { 14 num = stod(*it); 15 sum += num; 16 } 17 cout << sum << endl; 18 system("pause"); 19 return 0; 20 }
练习9.51
比较麻烦,截取其中一个做做实验,关键是掌握各种函数的用法
1 #include<iostream> 2 #include<string> 3 #include<vector> 4 5 using namespace std; 6 7 class date { 8 friend ostream &print(ostream &os, date &d); 9 public: 10 date(unsigned y, unsigned m, unsigned d) : years(y), month(m),days(d){} 11 date() : date(1990,1,1) {} 12 date(string &s); 13 14 private: 15 unsigned years; 16 unsigned month; 17 unsigned days; 18 }; 19 20 ostream &print(ostream &os, date &d); 21 22 int main() 23 { 24 string s{ "1/1/1990" }; 25 date d1(s); 26 print(cout, d1); 27 system("pause"); 28 return 0; 29 } 30 31 date::date(string &s) 32 { 33 days = stoi(s.substr(0, s.find_first_of('/') - 0)); 34 month = stoi(s.substr(s.find_first_of('/') + 1, s.find_last_of('/') - s.find_first_of('/') -1)); 35 years = stoi(s.substr(s.find_last_of('/') + 1)); 36 } 37 38 ostream & print(ostream & os, date & d) 39 { 40 os << d.years << " " << d.month << " " << d.days; 41 return os; 42 // TODO: 在此处插入 return 语句 43 }
转载于:https://www.cnblogs.com/wuyinfenghappy/p/7348082.html
C++primer 9.5.5节练习相关推荐
- C++primer 9.2.1节练习
练习9.3 迭代器begin和end必须指向相同的容器,end可以与begin指向相同的位置,但不能指向begin之前的位置: 练习9.4 1 #include <iostream> 2 ...
- 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 ...
最新文章
- ELK教程2:Kibana的安装
- 【PC工具】U盘SD卡测试工具,速度测试,坏块测试查找
- 文件共享同步5种方式:NFS、NAS、rsync、scp、ftp
- KMP算法的nextval[] 即优化next[]
- logistic 回归_具有Logistic回归的优秀初学者项目
- MySQL远程访问报错解决
- lightswitch 添加 TreeView 控件
- 《天天数学》连载04:一月四日
- 【英语学习】【WOTD】scrumptious 释义/词源/示例
- 【java】Java 动态调试技术原理及实践
- Unity开发《一起来捉妖》教程 | 1.陀螺仪控制相机
- 博士的浪漫,就是把你写进论文致谢,保存一辈子
- mysql查出的结果太大 php_如何解决PHP使用mysql_query查询超大结果集超内存问题
- SourceInsight工程文件与源文件相对路径
- 小马哥---深度解析mtk刷机平台报错解决 4032 8038等
- HTML5 2D平台游戏开发#8指令技
- 向上滚动的文字的淡入淡出效果
- Win10搭建(FTP、LLS)
- IOS个人开发者账号和wp公司开发者帐号申请注意点
- 怎样设置阿里云Web应用攻击防护?