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 开发人员,也可以着手 ...
随机推荐
- Python通过正则表达式去除(过滤)HTML标签,提取文字
# -*- coding: utf-8-*- import re ##过滤HTML中的标签 #将HTML中标签等信息去掉 #@param htmlstr HTML字符串. def filter_tag ...
- poj3662 Telephone Lines【最短路】【二分】
http://poj.org/problem?id=3662 Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- CCCC L2-007. 家庭房产 建图 XJB模拟
https://www.patest.cn/contests/gplt/L2-007 题解:一开始是想直接并查集,一个家就是一个集合,对每个集合维护一个人数num1一个房产数num2 一个房产面积ar ...
- 各种小巧的Hello World
在Reddit看到这篇文章:Hello from a libc-free world! ,觉得挺有趣,然后又想起以前看过的各种相关资料,在此做一个整理.注意所有实验环境都为Linux. 版本一: 实际 ...
- svm核函数的理解和选择
https://blog.csdn.net/leonis_v/article/details/50688766 特征空间的隐式映射:核函数 咱们首先给出核函数的来头:在上文中,我们已经了解到了S ...
- Subsequence---poj3061(尺取法||二分)
题目链接:http://poj.org/problem?id=3061 题意:给n个正整数和一个数S,求出总和不小于S的连续子序列的长度的最小值,如果无解输出0: 我们可以用sum[i]表示前i项的和 ...
- extjs分页
1.本地分页:设置store的proxy属性为pagingmemoryproxy实例 2.远程分页
- Percona Data Recovery Tool 单表恢复
前几天写过update或者delete忘加where条件的数据恢复.今天介绍一款开源的MySQL数据库InnoDB数据恢复工具:innodb-tools,它通过从原始数据文件中提取表的行记录,实现从丢 ...
- Linux输入输出重定向和文件查找值grep命令
Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...
- python count()
count() 描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. 语法 count()方法语法: str.count(sub, st ...