《c++ primer, 5E》

第68页到第81页,笔记:

1、读取未知量的string对象示例

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<string>
using std::string; int main()
{
string word;
while(cin >> word)
cout << word << endl;
return ;
}

2、使用getline读取一行示例

    #include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<string>
using std::string; int main()
{
string line;
while(getline(cin , line))
cout << line << endl;
return ;
}

3、string_A.empty()、string_A.size()

4、string_A.size()返回值是一个无符号的整数,不要在表达式中与有符号的int型混用

5、string比较大小按照字典序,大小写敏感!

6、字符串字面值与string是不同的类型。不能把字面值直接相加,必须确保每个加法运算 + 两侧的运算对象至少有一个是string

遇到的问题:

课后练习:

练习3.1

1

#include<iostream>
using std::cout;
using std::endl;
int main()
{
int sum = ;
for(int val = ; val <= ; ++val)
sum += val;
cout << sum << endl;
return ;
}

2

#include<iostream>
using std::cout;
using std::endl;
int main()
{
int sum = ;
for(int val = ; val >= ; --val)
cout << val << endl;
return ;
}

3

#include<iostream>
using std::cout;
using std::endl;
using std::cin;
int main()
{
int a, b;
cin >> a >> b;
for(int val = a; val <= b; ++val)
cout << val << endl;
return ;
}

4

#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
struct Sales_data{
string bookNo;
unsigned units_sold = ;
double price = 0.0;
double revenue = 0.0;
};
int main()
{
Sales_data data;
// std::cin >> data;
cin >> data.bookNo >> data.units_sold >> data.price;
// std::cout << data;
cout << data.bookNo << " " << data.units_sold << " " << data.price << endl;
return ;
}

5

#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
struct Sales_data{
string bookNo;
unsigned units_sold = ;
double price = 0.0;
double revenue = 0.0;
};
int main()
{
Sales_data data1, data2;
// std::cin >> data1 >> data2;
cin >> data1.bookNo >> data1.units_sold >> data1.price;
cin >> data2.bookNo >> data2.units_sold >> data2.price;
// std::cout << data1 + data2;
cout << data1.bookNo << " " << data1.units_sold + data2.units_sold << " " << data1.price << endl;
return ;
}

6

#include<iostream>
#include<string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
struct Sales_data{
string bookNo;
unsigned units_sold = ;
double price = 0.0;
double revenue = 0.0;
};
int main()
{
Sales_data data, sum_data;
while(cin >> data.bookNo >> data.units_sold >> data.price)
sum_data.units_sold += data.units_sold;
cout << data.bookNo << " " << sum_data.units_sold << " " << data.price << endl;
return ;
}

7

#include<iostream>
#include<string>
using std::string;
using std::cout;
using std::cin;
using std::endl;
struct Sales_data{
string bookNo;
unsigned units_sold = ;
double price = 0.0;
double revenue = 0.0;
};
int main()
{
int num = ;
string last_bookNo;
Sales_data temp_data;
// 读取第一个
// std::cin >> temp_data;
cin >> temp_data.bookNo >> temp_data.units_sold >> temp_data.price;
last_bookNo = temp_data.bookNo;
num = ;
// while(cin >> temp_data){
while(cin >> temp_data.bookNo >> temp_data.units_sold >> temp_data.price){
if(temp_data.bookNo == last_bookNo){
++num;
}
else{
cout << last_bookNo << " " << num;
last_bookNo = temp_data.bookNo;
num = ;
}
}
cout << last_bookNo << " " << num;
return ;
}

8

#include<iostream>
#include<string>
using std::cerr;
using std::string;
using std::cin;
using std::cout;
using std::endl;
struct Sales_data{
string bookNo;
unsigned units_sold = ;
double price = 0.0;
double revenue = 0.0;
};
int main()
{
Sales_data total;
if(cin >> total.bookNo >> total.units_sold >> total.price){
Sales_data trans;
// while(cin >> total)
while(cin >> trans.bookNo >> trans.units_sold >> trans.price){
if(total.bookNo == trans.bookNo){
total.units_sold += trans.units_sold;
}else{
// std::cout << total << std::endl;
cout << total.bookNo << " " << total.units_sold << " " << total.price << endl;
// total = trans;
total = trans;
}
}
cout << total.bookNo << " " << total.units_sold << " " << total.price << endl;
}else{
cerr << "No data?" << endl;
return -;
}
return ;
}

练习3.2

1

    #include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<string>
using std::string; int main()
{
string line;
while(getline(cin , line))
cout << line << endl;
return ;
}

2

#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<string>
using std::string; int main()
{
string word;
while(cin >> word)
cout << word << endl;
return ;
}

练习3.3

string类的输入运算符将空白字符作为分隔符,

string类的getline函数将空白符作为字符读入字符串。

练习3.4

1

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
int main()
{
string s1, s2;
cin >> s1 >> s2;
if(s1 == s2)
cout << "Equal" << endl;
else if(s1 > s2)
cout << s1 << endl;
else
cout << s2 << endl;
return ;
}

2

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
int main()
{
string s1, s2;
cin >> s1 >> s2;
if(s1.size() == s2.size())
cout << "Equal" << endl;
else if(s1.size() > s2.size())
cout << s1 << endl;
else
cout << s2 << endl;
return ;
}

练习 3.5

1

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
int main()
{
string sum_string;
string temp_string;
while(cin >> temp_string)
sum_string += temp_string;
cout << sum_string << endl;
return ;
}

2

#include<iostream>
using std::cout;
using std::cin;
using std::endl;
#include<string>
using std::string;
int main()
{
string sum_string;
string temp_string;
while(cin >> temp_string)
sum_string += temp_string + " ";
cout << sum_string << endl;
return ;
}

c++第十一天的更多相关文章

  1. CRL快速开发框架系列教程十一(大数据分库分表解决方案)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  2. 我的MYSQL学习心得(十一) 视图

    我的MYSQL学习心得(十一) 视图 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...

  3. WCF学习之旅—第三个示例之五(三十一)

       上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九) WCF学习 ...

  4. [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合

    [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合 Datasets can often contain components of that require differe ...

  5. 关于大型网站技术演进的思考(二十一)--网站静态化处理—web前端优化—下【终篇】(13)

    本篇继续web前端优化的讨论,开始我先讲个我所知道的一个故事,有家大型的企业顺应时代发展的潮流开始投身于互联网行业了,它们为此专门设立了一个事业部,不过该企业把这个事业部里的人事成本,系统运维成本特别 ...

  6. CPrimerPlus第十一章中的“选择排序算法”学习

    C Primer Plus第十一章字符串排序程序11.25中,涉及到“选择排序算法”,这也是找工作笔试或面试可能会遇到的题目,下面谈谈自己的理解. 举个例子:对数组num[5]={3,5,2,1,4} ...

  7. 无废话ExtJs 入门教程二十一[继承:Extend]

    无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...

  8. GPS部标监控平台的架构设计(十一)-基于Memcached的分布式Gps监控平台

    部标gps监控平台的架构,随着平台接入的车辆越来越多,架构也面临越来越大的负载挑战,我们当然希望软件尽可能的优化并能够接入更多的车辆,减少在硬件上的投资.但是当车辆增多到某一个临界点的时候,仍然要面临 ...

  9. 双十一 VS 火车票(12306)

    火车票开售了,又是一年,code了一年,咱们也该回顾回顾了. 还记得12306上线之初各种技术大牛给人家出方案,吐槽人家外包费用?我们来回顾回顾. 就园子里都过千篇文章来侃这事儿,请问有多少主题的文章 ...

  10. Bootstrap <基础三十一>插件概览

    在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...

随机推荐

  1. 7.20python线程(2)

    RLock 递归锁 线程事件

  2. cURL 是一个功能强大的PHP库。

    使用PHP的cURL库可以简单和有效地去抓网页.你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了.无论是你想从从一个链接上取部分数据,或是取一个XML文件并把 ...

  3. 数据库outer连接

    left (此处省略outer) join, 左边连接右边,左边最大,匹配所有的行,不管右边 right join,右边连接左边,右边最大,匹配所有的行,不管左边 条件直接放ON后面,是先筛选右边的表 ...

  4. POJ-1887 Testing the CATCHER(dp,最长下降子序列)

    Testing the CATCHER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16515 Accepted: 6082 ...

  5. poj2492 A Bug's Life【并查集】

    Background  Professor Hopper is researching the sexual behavior of a rare species of bugs. He assume ...

  6. ZOJ 2760 - How Many Shortest Path - [spfa最短路][最大流建图]

    人老了就比较懒,故意挑了到看起来很和蔼的题目做,然后套个spfa和dinic的模板WA了5发,人老了,可能不适合这种刺激的竞技运动了…… 题目链接:http://acm.zju.edu.cn/onli ...

  7. Oracle管理监控之如何对数据库进行监控检查

    oracle自动工作负载库(AWR):采集与性能相关的统计数据,并从统计的数据中导出性能量度,以跟踪数据库潜在的问题. 如何生成oracle数据库的自动负载库报告. 手工生成一份oracle数据库的快 ...

  8. flask数据库操作

    Python 数据库框架 大多数的数据库引擎都有对应的 Python 包,包括开源包和商业包.Flask 并不限制你使用何种类型的数据库包,因此可以根据自己的喜好选择使用 MySQL.Postgres ...

  9. find a way to escape--hdu1593

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1593 找到二者角速度相等时水中人的R,在此之前二者保持在一条直线上,之后水中的人沿直线到岸边S点匀 ...

  10. Kubernetes容器调度

    Kubernetes的调度器是Kubernetes众多组件的一部分,独立于API服务器之外.调度器本身是可插拔的,任何理解调度器和API服务器之间调用关系的工程师都可以编写定制的调度器.本文后面的介绍 ...