《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. Mysql----数据备份、pymysql模块

    一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https://pan.baidu.com/s/1bpo5mqj 掌握: #1. 测试+链接 ...

  2. ELKStack之消息队列

    redis消息队列 安装redis yum -y install redis 修改配置文件 修改ip 后台运行 启动 systemctl start redis 查看 lsof -i:6379 连接 ...

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

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

  4. 在ubuntu下安装使用latex

    参考:https://www.cnblogs.com/longdouhzt/archive/2012/09/27/2706358.html https://jingyan.baidu.com/albu ...

  5. hihocoder 1284 - 机会渺茫

    N有N_cnt个约数,M有M_cnt个约数,那么总共有N_cnt * M_cnt种对应情况. 假设其中有D_cnt个对应结果是相等的,而这D_cnt个数正好是gcd(N,M)的所有约数. 例如: N= ...

  6. Thymeleaf模板引擎的初步使用

    在springboot中,推荐使用的模板引擎是Thymeleaf模板引擎,它提供了完美的Spring MVC的支持.下面就简单的介绍一下Thymeleaf模板引擎的使用. 在controller层中, ...

  7. 常用的windows注册表大全

    目录 使系统没有“运行”选项                        1让操作系统无“关闭系统” 选项                    2让操作系统无“注销”选项              ...

  8. 异常处理:No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer

    No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no pro ...

  9. Mysql中Left Join 与Right Join 与 Inner Join 与 Full Join的区别

    看看Left Join 与Right Join 与 Inner Join 与 Full Join对表进行操作后得到的结果. 在数据库中新建两张表,并插入要测试的数据. 新建表: USE [Test] ...

  10. Inception系列

    从GoogLeNet的Inceptionv1开始,发展了众多inception,如inception v2.v3.v4与Inception-ResNet-V2. 故事还是要从inception v1开 ...