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 开发人员,也可以着手 ...
随机推荐
- linux使用bin文件安装jdk
jdk1.6.20文件为bin文件安装过程如下 添加执行权限 chmod +x jdk-6u20-linux-x64.bin 运行,出现提示需要输入yes ./jdk-6u20-linux-x64.b ...
- poj1001 Exponentiation【java大数】
Exponentiation Time Limit: 500MS Memory Limit: 10000K Total Submissions: 183034 Accepted: 44062 ...
- CCCC 月饼
https://www.patest.cn/contests/gplt/L2-003 题解:按平均值贪心. 坑:有一个样例卡住了,是因为 while (i<=n&&x - bs[ ...
- iOS多线程编程之NSThread的使用(转载)
1.简介: 1.1 iOS有三种多线程编程的技术,分别是: 1.NSThread 2.Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的 ...
- spring boot继承web和mybatis时,调用接口删除记录出现的空指针以及解决办法
前两天在学spring boot的时候,出现了一个很奇怪的错误,因为是第一次使用spring boot,所以没想到会遇到这种莫名其妙的bug,即调用接口删除数据库中一条记录的时候,数据库中记录事实上以 ...
- 指定多个pip源
https://blog.csdn.net/liujiong63/article/details/78795015 Linux环境下创建pip配置文件: vim .pip/pip.conf [glob ...
- sublime text3控制台每次报错会显示几行[ ]
如下图所示,每次编译报错的时候会显示: 我只需要报错信息,不想红框中的信息出现. 解决方案: 1 找到sublime Text3安装路径下的Default.sublime-package,如~Subl ...
- 洛谷P3157 动态逆序对 [CQOI2011] cdq分治
正解:cdq分治 解题报告: 传送门! 长得有点像双倍经验还麻油仔细看先放上来QwQ! 这题首先想到的就直接做逆序对,然后记录每个点的贡献,删去就减掉就好 但是仔细一想会发现布星啊,如果有一对逆序对的 ...
- 大数据智能SOC解决方案
- GDB调试qemu源码纪录
今天介绍下如何利用gdb调试qemu 1.首先获取qemu源码 获取地址:https://www.qemu.org/ 2.编译安装qemu 进入qemu目录 ./configure --enable- ...