C++Primer第五版——习题答案详解(二)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html
第3章 字符串、向量和数组
练习3.2
一次读入一整行
#include<iostream>
#include<string>
using namespace std;
int main() {
string a;
while (getline(cin, a)) {
cout << a << endl;
}
return 0;
}
一次读入一个词
#include<iostream>
#include<string>
using namespace std;
int main() {
string a;
while (cin>>a) {
cout << a << endl;
}
return 0;
}
练习3.4
#include<iostream>
#include<string>
using namespace std;
int main() {
string a, b;
cin >> a >> b;
if (a != b) {
cout << (a >= b ? a : b);
}
return 0;
}
练习3.5
#include<iostream>
#include<string>
using namespace std;
int main() {
string ans,a;
while (cin >> a) {
ans += a;
}
cout << ans << endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
string ans,a;
while (cin >> a) {
ans += a;
ans += " ";
}
cout << ans << endl;
system("pause");
return 0;
}
练习3.6
利用C++11新特性范围for语句,注意要修改字符串必须加上&引用
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main() {
string t;
cin >> t;
for (auto &i : t) {
i = 'X';
}
cout << t << endl;
system("pause");
return 0;
}
练习3.7
改为char后结果一样,因为修改的时候就是按字符修改的。
练习3.8
for循环
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main() {
string t;
cin >> t;
for (int i = 0;i < t.size();i++) {
t[i] = 'X';
}
cout << t << endl;
system("pause");
return 0;
}
while循环
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main() {
string t;
cin >> t;
int i = 0;
while (i < t.size()) {
t[i] = 'X';
i++;
}
cout << t << endl;
system("pause");
return 0;
}
显然已知循环次数,for循环更好用些。
练习3.9
不合法,s是空字符串。
练习3.10
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main() {
string t,ans;
cin >> t;
for (char &i : t) {
if (!ispunct(i)) {
ans += i;
}
}
cout << ans << endl;
system("pause");
return 0;
}
练习3.11
如果循环中修改字符串的值,不合法。
练习3.14
#include<iostream>
#include<vector>
using namespace std;
int main() {
vector<int> ans;
int temp;
while (cin >> temp) {
ans.push_back(temp);
}
return 0;
}
练习3.15
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<string> ans;
string temp;
while (cin >> temp) {
ans.push_back(temp);
}
return 0;
}
练习3.17
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<string> ans;
string temp;
while (cin >> temp) {
ans.push_back(temp);
}
for (string &i : ans) {
for (char &j : i) {
if (islower(j)) {
j = toupper(j);
}
}
cout << i << endl;
}
system("pause");
return 0;
}
练习3.18
不合法,规定只能对确知已存在的元素执行下标操作!
练习3.19
vector<int> val1{ 42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
vector<int> val2(10,42);
vector<int> val3 = val2;
练习3.20
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<int> t;
int temp;
while (cin >> temp) {
t.push_back(temp);
}
int len = t.size();
for (int i = 0;i < len; i++) {
cout << t[i] + t[i + 1] << endl;
i++;
}
system("pause");
return 0;
}
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<int> t;
int temp;
while (cin >> temp) {
t.push_back(temp);
}
int len = t.size();
for (int i = 0;i < len; i++) {
cout << t[i] + t[len - i - 1] << endl;
i++;
}
system("pause");
return 0;
}
练习3.23
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<int> res = {1,2,3,4,5,6,7,8,9,10};
for (auto it = res.begin();it != res.end();it++) {
*it = (*it) * 2;
}
for (auto i : res) {
cout << i << endl;
}
system("pause");
return 0;
}
练习3.24
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<int> t;
int temp;
while (cin >> temp) {
t.push_back(temp);
}
for (auto it = t.begin();it != t.end(); it++) {
cout << (*it) + *(it+1) << endl;
it++;
}
system("pause");
return 0;
}
练习3.25
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<unsigned> scores(11, 0);
unsigned grade;
while (cin >> grade) {
if (grade <= 100) {
auto it = scores.begin();
++*(it + grade / 10);
}
}
for (auto i : scores) {
cout << i << endl;
}
system("pause");
return 0;
}
练习3.26
两迭代器不能直接相加。迭代器的运算见P111
练习3.27
a.非法,数组大小应该为常量
b.合法
c.非法,返回值不是常量
d.非法,数组大小开小了
练习3.28
分别为空、0、空、随机值
练习3.29
数组大小不够灵活,不能往其中增加元素
练习3.30
去掉for循环中的等于号
练习3.31
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
int a[10];
for (int i = 0;i < 10;i++) {
a[i] = i;
}
for (int i = 0;i < 10;i++) {
cout << a[i] << endl;
}
system("pause");
return 0;
}
练习3.32
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<int> a(10);
for (int i = 0;i < 10;i++) {
a[i] = i;
}
for (int i = 0;i < 10;i++) {
cout << a[i] << endl;
}
system("pause");
return 0;
}
练习3.33
不初始化会出现随机数字。
练习3.35
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
int *p = a;
for (int i = 0;i < 10;i++) {
*(a + i) = 0;
}
for (int i = 0;i < 10;i++) {
cout << a[i];
}
system("pause");
return 0;
}
练习3.36
bool isEqual(int a[], int m,int b[],int n) {
if (m != n) return false;
for (int i = 0;i < m;i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
bool isEqual(vector<int> a, vector<int> b) {
if (a == b) return true;
else return false;
}
练习3.37
输出hello但后面跟了乱码,因为没有结束字符
练习3.38
地址相加没有意义
练习3.39
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
string s1 = "A string example";
string s2 = "A different string";
if (s1 < s2) cout << "s1<s2";
else if (s1 == s2)cout << "s1=s2";
else cout << "s1>s2";
system("pause");
return 0;
}
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
const char s1[] = "A string example";
const char s2[] = "A different string";
if (strcmp(s1, s2) < 0) cout << "s2>s1";
else if (strcmp(s1, s2) == 0) cout << "s1=s2";
else cout << "s1>s2";
system("pause");
return 0;
}
练习3.40
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main() {
char a[100] = "A example";
char b[100] = "A string";
char c[200];
strcpy_s(c, a);
char *pc = c;
while (*pc) {
cout << *pc;
pc++;
}
cout << endl;
strcat_s(c, " ");
pc = c;
while (*pc) {
cout << *pc;
pc++;
}
cout << endl;
strcat_s(c, b);
pc = c;
while (*pc) {
cout << *pc;
pc++;
}
cout << endl;
system("pause");
return 0;
}
练习3.41
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main() {
int a[10] = { 0,1,2,3,4,5,6,7,8,9 };
vector<int> t(begin(a), end(a));
for (auto i : t) {
cout << i;
}
system("pause");
return 0;
}
练习3.42
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main() {
vector<int> t(10,1);
int b[10];
for (int i = 0;i < 10;i++) {
b[i] = t[i];
}
system("pause");
return 0;
}
练习3.43
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main() {
int ia[3][4] = {
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
for (const int(&i)[4] : ia) {
for (int j : i) {
cout << j << " ";
}
}
cout << endl;
for (size_t i = 0;i < 3;i++) {
for (size_t j = 0;j < 4;j++) {
cout << ia[i][j] << " ";
}
}
cout << endl;
for (int(*i)[4] = begin(ia);i != end(ia);i++) {
for (int *j = begin(*i);j != end(*i);j++) {
cout << *j << " ";
}
}
cout << endl;
system("pause");
return 0;
}
练习3.44
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main() {
typedef int int_array[4];
int ia[3][4] = {
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
for (const int_array &i : ia) {
for (int j : i) {
cout << j << " ";
}
}
cout << endl;
for (size_t i = 0;i < 3;i++) {
for (size_t j = 0;j < 4;j++) {
cout << ia[i][j] << " ";
}
}
cout << endl;
for (int_array *i = begin(ia);i != end(ia);i++) {
for (int *j = begin(*i);j != end(*i);j++) {
cout << *j << " ";
}
}
cout << endl;
system("pause");
return 0;
}
练习3.45
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main() {
typedef int int_array[4];
int ia[3][4] = {
{0,1,2,3},
{4,5,6,7},
{8,9,10,11}
};
for (auto &row : ia) {
for (auto &col : row) {
cout << col << " ";
}
}
cout << endl;
for (size_t i = 0;i < 3;i++) {
for (size_t j = 0;j < 4;j++) {
cout << ia[i][j] << " ";
}
}
cout << endl;
for (auto *i = begin(ia);i != end(ia);i++) {
for (auto *j = begin(*i);j != end(*i);j++) {
cout << *j << " ";
}
}
cout << endl;
system("pause");
return 0;
}
C++Primer第五版——习题答案详解(二)的更多相关文章
- C++Primer第五版——习题答案详解(一)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...
- C++Primer第五版——习题答案详解(三)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...
- C++Primer第五版——习题答案详解(四)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...
- C++Primer第五版——习题答案详解(五)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...
- C++Primer第五版——习题答案详解(六)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...
- C++Primer第五版——习题答案详解(七)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...
- C++Primer第五版——习题答案详解(八)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第9章 顺序容器 练习9.1 a.list,需要按字典序插入,可能插入位置在中间 b.d ...
- C++Primer第五版——习题答案详解(九)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第10章 泛型算法 练习10.1 #include<iostream> #i ...
- C++Primer第五版——习题答案详解(十)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第11章 关联容器 练习11.3 #include<iostream> #i ...
随机推荐
- windows服务项目的 安装 卸载 查看
安装服务:installutil.exe C:\a.exe卸载服务Installutil.exe /u C:\a.exe 查看服务状态 services.msc
- vue教程自学笔记(三)
五.Class与Style绑定 可以用v-bind用于class和style,表达式结果类型除了字符串,还可以是对象或数组. 1.绑定HTML Class 对象语法:给v-bind:class传递一个 ...
- Debian/Ubuntu/Deepin下AndroidStudio2/3打开AVD模拟器无反应
Debian系AS无法启动模拟器 问题描述 早在半年前将开发环境从windows迁移到了Linux:当时用的是Debian系统,也是在安装完成AndroidStudio之后无法开启模拟器,也没出现什么 ...
- vue 和react的区别
1.数据是不是可变的 react整体是函数式的思想,把组件设计成纯组件,状态和逻辑通过参数传入,所以在react中,是单向数据流,推崇结合immutable来实现数据不可变. react在setSta ...
- windows2008 转 centos7 数据磁盘NTFS无损挂载
转换时 原win硬盘里作为系统稳盘的硬盘必须重新格式化才能装机 数据盘在安装ntfs-3g可以直接挂载 几个重要命令: #lsblk //查看硬盘情况 df -T 只可以查看已经挂载的分区和文件系统 ...
- 网络编程并发 多进程 进程池,互斥锁,信号量,IO模型
进程:程序正在执行的过程,就是一个正在执行的任务,而负责执行任务的就是cpu 操作系统:操作系统就是一个协调.管理和控制计算机硬件资源和软件资源的控制程序. 操作系统的作用: 1:隐藏丑陋复杂的硬件接 ...
- 【草稿】实验室新手HandBook
PS:本文旨在给初入CV领域实验室的新手一个可供参考的学习列表,使得能够快速熟悉常用且必要的工具.
- 20155219 第十周课下作业-IPC
题目:研究Linux下IPC机制:原理,优缺点,每种机制至少给一个示例,提交研究博客的链接 共享内存 管道 FIFO 信号 消息队列 1.共享内存 共享内存就是允许两个不相关的进程访问同一个逻辑内存. ...
- 2018 vue前端面试题
1.active-class是哪个组件的属性?嵌套路由怎么定义?答:vue-router模块的router-link组件. 2.怎么定义vue-router的动态路由?怎么获取传过来的动态参数? 答: ...
- 通过 ppa 在ubuntu server 上安装java 8
第一步:使用ppa/源方式安装,安装ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 第二步:安装 oracle ...