【C++Primer】第五版【学习笔记】习题解答第三章

ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考。

作者:cosefy

Date: 2020/1/10

第三章:字符串,向量和数组

练习3.2:

  1. #include<iostream>
  2. #include<string>
  3. using std::string;
  4. using std::cout;
  5. using std::cin;
  6. using std::endl;
  7. int main()
  8. {
  9. string line;
  10. //while (cin >> line) //每次输入一个词
  11. while (getline(cin,line)) //每次输入一行
  12. if (!line.empty())
  13. cout << line << endl;
  14. return 0;
  15. }

练习3.3:

string的输入从字符开始,直到遇到空白停止;而getline遇到空白不停止,遇到回车停止。

练习3.4:

实现:比较输入的两个字符串是否等长,如果不等长,输出长度较大的那个字符串。

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string l1, l2;
  7. cin >> l1 >> l2;
  8. if (l1 == l2)
  9. cout << "字符串相等" << endl;
  10. else
  11. if (l1.size() > l2.size())
  12. cout << l1 << endl;
  13. else if(l1.size() < l2.size())
  14. cout << l2 << endl;
  15. else
  16. cout << "长度相等" << endl;
  17. return 0;
  18. }

练习3.5:

  1. int main()
  2. {
  3. string l1, l2;
  4. while (cin >> l2)
  5. l1 = l1 + l2 + " ";
  6. cout << l1 << endl;
  7. return 0;
  8. }

练习3.6:

实现:使用for语句,将字符串内的所有字符用X代替.

  1. #include<iostream>
  2. #include<string>
  3. #include<ctype.h>
  4. using namespace std;
  5. int main()
  6. {
  7. string s;
  8. getline(cin,s);
  9. for (auto &c : s)
  10. if (isalpha(c))
  11. c = 'X';
  12. cout << s << endl;
  13. return 0;
  14. }

练习3.8:

for循环更好,形式更加简洁。

练习3.9:

不合法,因为字符串s没有初始化,直接访问第一个字符会发生未知错误。

练习3.10:

实现:读入一行含标点符号的字符串,将标点符号删除后输出剩余部分

  1. #include<iostream>
  2. #include<string>
  3. #include<ctype.h>
  4. using namespace std;
  5. int main()
  6. {
  7. string s,s1;
  8. getline(cin, s);
  9. for (auto& c : s)
  10. if (!ispunct(c))
  11. s1 = s1 + c;
  12. cout << s1<<endl;
  13. return 0;
  14. }

练习3.12:

  • a正确
  • b错误,sevc是string类型,ivec是int型,类型不匹配。
  • c错误,应该用花括号来处理。

练习3.13:

  • v1:一个元素,空值
  • v2:10个元素都为0
  • v3:10个元素都为42
  • v4:一个元素,值为10
  • v5:两个元素,分别为10,42
  • v6:10个元素,都为空字符串
  • v7:10个元素,都为"hi"

练习3.14:

实现:用cin读入一组整数,并把它们存入一个vector对象

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int> invc;
  8. int a;
  9. while (cin >> a)
  10. invc.push_back(a);
  11. return 0;
  12. }

练习3.17:

实现:cin读入一组词,把它们存入一个vector对象,然后设法把所有词改为大写形式,每个词占一行。

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<string> vctr;
  8. string s;
  9. decltype(vctr.size()) index = 0;
  10. while (cin >> s)
  11. {
  12. for (auto& c : s)
  13. c = toupper(c);
  14. vctr.push_back(s);
  15. cout << vctr[index] << endl;
  16. index += 1;
  17. }
  18. return 0;
  19. }

练习3.18:

不合法,vector对象的下标只能用来访问已存在的元素,而不能用来添加元素。

可以用ivec.push_back()函数来添加。

练习3.19:

  1. vector<int> v1(10,42) //第一种
  2. vector<int>v1={42,42,42,42,42,42,42,42,42,42} //第二种
  3. vector<int>v2; //第三种
  4. for(int decltype(v1.size()) i=0; i!=10;i++)
  5. v1.push_back(42);

练习3.20:

实现:读入一组整数到vrctor对象中,要求先输出第一个和最后一个整数的和,然后输出第二个和倒数第二个整数的和,以此类推。

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int>v1;
  8. int value;
  9. while (cin >> value)
  10. v1.push_back(value);
  11. for (decltype(v1.size())i = 0; i <= v1.size() / 2; i++)
  12. if (v1.size() % 2 != 0 && i == v1.size() / 2)
  13. cout << v1[i] << endl;
  14. else
  15. cout << v1[i]+v1[v1.size()-1-i] << endl;
  16. return 0;
  17. }

练习3.22:

实现:把text的第一部分改为大写,然后输出。

  1. #include<iostream>
  2. #include<string>
  3. #include<ctype.h>
  4. using namespace std;
  5. int main()
  6. {
  7. string text ("hello world");
  8. for (auto it = text.begin(); it != text.end() && !isspace(*it); ++it)
  9. *it = toupper(*it);
  10. cout << text << endl;
  11. return 0;
  12. }

练习3.23:

实现:创建一个含有10个整数的vector对象,用迭代器将所有元素的值变为原来的两倍,最后输出vector对象的内容。

  1. #include<iostream>
  2. #include<string>
  3. #include<vector>
  4. using namespace std;
  5. int main()
  6. {
  7. vector<int>v1;
  8. int t = 0;
  9. for (unsigned i = 0; i != 10; i++)
  10. {
  11. cin >> t;
  12. v1.push_back(t); //创建含有10个整数的vector对象
  13. }
  14. for (auto it = v1.begin(); it != v1.end(); it++)
  15. {
  16. *it = *it * *it; //用迭代器处理
  17. cout << *it<<" ";
  18. }
  19. return 0;
  20. }

练习3.28:

函数体内的string型数组sa2与函数体内的string型数组sa,元素都是空。

函数体内的int型数组ia2中的元素未初始化;函数体外的int型数组ia中的元素都是0.

练习3.35:

实现:利用指针将数组中的值置为0.

  1. using namespace std;
  2. int main()
  3. {
  4. int a[] = { 1,2,3,4,5,6 };
  5. int* p = begin(a), * q = end(a);
  6. while (p != q)
  7. {
  8. *p = 0;
  9. cout << *p;
  10. p++;
  11. }
  12. }

《C++Primer》第五版习题答案--第三章【学习笔记】的更多相关文章

  1. 《C++Primer》第五版习题答案--第六章【学习笔记】

    <C++Primer>第五版习题答案--第六章[学习笔记] ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/16 第六章:函数 ...

  2. C++Primer第五版——习题答案目录

    目前正在刷<C++Primer>这本书,会在博客上记录课后习题答案,答案仅供参考. 因为水平有限,如有有误之处,希望大家不吝指教,谢谢! 目录地址 使用的系统为:win 10,编译器:VS ...

  3. C++Primer第五版——习题答案详解(一)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第1章 开始&&第2章 变量和基本类型 练习1.3 #include&l ...

  4. C++Primer第五版——习题答案详解(二)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第3章 字符串.向量和数组 练习3.2 一次读入一整行 #include<iost ...

  5. C++Primer第五版——习题答案详解(三)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第4章 表达式 练习4.10 while(cin>>i&&i ...

  6. C++Primer第五版——习题答案详解(四)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...

  7. C++Primer第五版——习题答案详解(五)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第6章 函数 练习6.4 #include<iostream> using ...

  8. C++Primer第五版——习题答案详解(六)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第7章 类 练习7.1 class Sales_data { public: std:: ...

  9. C++Primer第五版——习题答案详解(七)

    习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第8章 IO库 练习8.1 istream &iofunc(istream &a ...

随机推荐

  1. Hammersley-Clifford定理证明

    Proof of Hammersley-Clifford TheoremProof of Hammersley-Clifford Theorem依赖知识定义1定义2证明过程反向证明(吉布斯分布=> ...

  2. springboot activiti工作流简单示例

    最近一直研究springboot,根据工作需求,工作流需要作为一个单独的微服务工程来提供给其他服务调用,现在简单的写下工作流(使用的activiti)微服务的搭建与简单使用 jdk:1.8 数据库:m ...

  3. H3C PPP MP实现方式

  4. Capistrano:自动完成多台服务器上新版本的同步更新,包括数据库的改变

    https://baike.baidu.com/item/Capistrano/6844928?fr=aladdin   Capistrano是一种在多台服务器上运行脚本的开源工具,它主要用于部署we ...

  5. 关于scipy包的安装

    先安装好scikit-learn和numpy,从网上下载scipy的whl文件,通过pip install+安装包地址的方法安装

  6. webpack打包前删除之前的所有文件

    安装插件: npm install --save-dev clean-webpack-plugin 在webpack.prod.conf.js 中引入:  const { CleanWebpackPl ...

  7. tf.squeeze()

    转载自:https://www.cnblogs.com/mdumpling/p/8053376.html 原型 tf.squeeze(input, squeeze_dims=None, name=No ...

  8. tensorflow在文本处理中的使用——skip-gram & CBOW原理总结

    摘自:http://www.cnblogs.com/pinard/p/7160330.html 先看下列三篇,再理解此篇会更容易些(个人意见) skip-gram,CBOW,Word2Vec 词向量基 ...

  9. Common Logging包装设计

    类设计 LogFactory根据当前环境加载具体的Log实现: 1.从缓存中加载LogFactory 2.从系统属性org.apache.commons.logging.LogFactory 中加载L ...

  10. <QluOJ2018NewCode>约数个数

    题目描述 p^q表示p的q次方,正整数M可以分解为M=(p1^a1)*(p2^a2)*(p3^a3)*……*(pn^an)的形式,其中p1,p2……pn为质数(大于1并且只能被1和自身整除的数叫做质数 ...