c++第十一天
《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++第十一天的更多相关文章
- CRL快速开发框架系列教程十一(大数据分库分表解决方案)
本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...
- 我的MYSQL学习心得(十一) 视图
我的MYSQL学习心得(十一) 视图 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据 ...
- WCF学习之旅—第三个示例之五(三十一)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) WCF学习 ...
- [占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合
[占位-未完成]scikit-learn一般实例之十一:异构数据源的特征联合 Datasets can often contain components of that require differe ...
- 关于大型网站技术演进的思考(二十一)--网站静态化处理—web前端优化—下【终篇】(13)
本篇继续web前端优化的讨论,开始我先讲个我所知道的一个故事,有家大型的企业顺应时代发展的潮流开始投身于互联网行业了,它们为此专门设立了一个事业部,不过该企业把这个事业部里的人事成本,系统运维成本特别 ...
- CPrimerPlus第十一章中的“选择排序算法”学习
C Primer Plus第十一章字符串排序程序11.25中,涉及到“选择排序算法”,这也是找工作笔试或面试可能会遇到的题目,下面谈谈自己的理解. 举个例子:对数组num[5]={3,5,2,1,4} ...
- 无废话ExtJs 入门教程二十一[继承:Extend]
无废话ExtJs 入门教程二十一[继承:Extend] extjs技术交流,欢迎加群(201926085) 在开发中,我们在使用视图组件时,经常要设置宽度,高度,标题等属性.而这些属性可以通过“继承” ...
- GPS部标监控平台的架构设计(十一)-基于Memcached的分布式Gps监控平台
部标gps监控平台的架构,随着平台接入的车辆越来越多,架构也面临越来越大的负载挑战,我们当然希望软件尽可能的优化并能够接入更多的车辆,减少在硬件上的投资.但是当车辆增多到某一个临界点的时候,仍然要面临 ...
- 双十一 VS 火车票(12306)
火车票开售了,又是一年,code了一年,咱们也该回顾回顾了. 还记得12306上线之初各种技术大牛给人家出方案,吐槽人家外包费用?我们来回顾回顾. 就园子里都过千篇文章来侃这事儿,请问有多少主题的文章 ...
- Bootstrap <基础三十一>插件概览
在前面布局组件中所讨论到的组件仅仅是个开始.Bootstrap 自带 12 种 jQuery 插件,扩展了功能,可以给站点添加更多的互动.即使不是一名高级的 JavaScript 开发人员,也可以着手 ...
随机推荐
- mysql语句性能分析
1.开启慢查询 slow_query_log = 1 //开启 slow_query_log_file = mysql_slow_query.log //日志文件位置 long_query_time ...
- Blocks POJ - 1390 多维dp
题意:有一排box,各有不同的颜色.你可以通过点击某个box使得与其相邻的同色box全部消掉,然后你可以得到的分数为消去长度的平方,问怎样得到最高分? 题解:考虑用一维dp,/*dp[i]为1~i个b ...
- Spark Streaming 在数据平台日志解析功能的应用
https://mp.weixin.qq.com/s/bGXhC9hvDj4lzK7wYYHGDg 目前,我们使用Filebeat监控日志产生的目录,收集产生的日志,打到logstash集群,接入ka ...
- SCRAM
RFC 5802 - Salted Challenge Response Authentication Mechanism (SCRAM) SASL and GSS-API Mechanisms ht ...
- python xlwt 设置单元格样式-合并单元格
xlwt模块详解--合并单元格 import xlwtworkbook = xlwt.Workbook()worksheet = workbook.add_sheet('My sheet')# 合并第 ...
- 用PHP编写一个APP的API
第一部分,通信接口的实现 标签(空格分隔): PHP 手机后台 api 通信接口 Andy PHP开发手机API时,一般返回XML或JSON数据类型的数据,除了要返回从源数据(程序本身需要的数据)外还 ...
- 【python-opencv】15-图像阈值
[微语]立志要如山,行道要如水.不如山,不能坚定,不如水,不能曲达 import cv2 as cv import numpy as np from matplotlib import pyplot ...
- Centos7网桥配置
CentOS 的网桥虽然配置了很多次,不过总是记不住那几条,还是简单记录下,增加网桥可以通过brctl命令,但是为了简便快捷,直接生成配置文件即可 1.在/etc/sysconfig/network- ...
- 利用compass制作雪碧图
compass是什么?是sass一款神奇插件,具体教程,我还是推荐阮一峰sass,compass教程,简单清晰明了. 用ps制作雪碧图,工作效率太低了.用compass来制作,方便很多.下图的用com ...
- [javascript]编码&i字符串格式化&nput历史记录&清空模态框
js中编码问题 https://www.haorooms.com/post/js_escape_encodeURIComponent 我在前端js添加时候创建dom时候,有汉字,发现是乱码就研究了下 ...