当前位置:   article > 正文

C++primer(第五版)习题答案_c++primer第五版答案

c++primer第五版答案

前两章习题较简单,这里就不作整理了,直接从第三章开始(持续更新):

Chapter 3. Strings, Vectors and Arrays

Exercise 3.1

part 1

  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. int main()
  6. {
  7. int sum = 0, val = 1;
  8. while (val <= 10) {
  9. sum += val;
  10. ++val;
  11. }
  12. cout << "Sum of 1 to 10 inclusive is " << sum << endl;
  13. return 0;
  14. }

part 2

  1. #include <iostream>
  2. using std::cin;
  3. using std::cout;
  4. using std::endl;
  5. using std::cerr;
  6. struct Sales_data {
  7. std::string bookNo;
  8. unsigned unit_sold = 0;
  9. double revenue = 0.0;
  10. };
  11. int main()
  12. {
  13. Sales_data data1, data2;
  14. double price = 0;
  15. cin >> data1.bookNo >> data1.unit_sold >> price;
  16. data1.revenue = data1.unit_sold * price;
  17. cin >> data2.bookNo >> data2.unit_sold >> price;
  18. data2.revenue = data2.unit_sold * price;
  19. if (data1.bookNo == data2.bookNo) {
  20. unsigned totalCnt = data1.unit_sold + data2.unit_sold;
  21. double totalRevenue = data1.revenue + data2. revenue;
  22. cout << data1.bookNo << " " << totalCnt << " " << totalRevenue << " ";
  23. if (totalCnt != 0)
  24. cout << totalRevenue/totalCnt << endl;
  25. else
  26. cout << "(no sale)" << endl;
  27. return 0;
  28. }
  29. else{
  30. cerr << "Data must refer to the same ISBN" << endl;
  31. return -1;
  32. }
  33. }

Exercise 3.2

part 1

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string line;
  10. while (getline(cin, line)) {
  11. cout << line << endl;
  12. }
  13. return 0;
  14. }

part 2

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string word;
  10. while (cin >> word)
  11. cout << word << endl;
  12. return 0;
  13. }

Exercise 3.3

string类输入运算符(>>):忽视一切空格,从第一个非空格字符开始,到第一个空格结束;

getline():包含换行符前的所有空格;

Exercise 3.4

part 1

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string st1, st2;
  10. cin >> st1 >> st2;
  11. if (st1 == st2)
  12. cout << "The two strings are equal" << endl;
  13. else if (st1 > st2)
  14. cout << "The maximum string is " << st1 << endl;
  15. else
  16. cout << "The maximum string is " << st2 << endl;
  17. return 0;
  18. }

part 2

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string st1, st2;
  10. cin >> st1 >> st2;
  11. if (st1.size() == st2.size())
  12. cout << "The two strings have the same length " << endl;
  13. else if (st1.size() > st2.size())
  14. cout << "The longest string is " << st1 << endl;
  15. else
  16. cout << "The longest string is " << st2 << endl;
  17. return 0;
  18. }

Exercise 3.5

part 1

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string total_str, str;
  10. while (cin >> str)
  11. total_str += str;
  12. cout << "The concatenated string is " << total_str << endl;
  13. return 0;
  14. }

part 2

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string total_str, str;
  10. while (cin >> str)
  11. if (total_str.empty())
  12. total_str += str;
  13. else
  14. total_str += " " +str;
  15. cout << "The concatenated string is " << total_str << endl;
  16. return 0;
  17. }

Exercise 3.6

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string str("I wanna go home!");
  10. for (auto &c : str) {
  11. c = 'X';
  12. }
  13. cout << str << endl;
  14. return 0;
  15. }

Exercise 3.7

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string str("I wanna go home!");
  10. for (char c : str) {
  11. c = 'X';
  12. }
  13. cout << str << endl;
  14. return 0;
  15. }
  16. //输出仍为I wanna go home!

本质上是把string中的每一个字符拷贝给循环控制变量c,此时对c更改只是改变了c字符,原string并没有任何变化,想要通过c改变string的值,就必须把c定义为对字符的引用。

Exercise 3.8

part 1

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string str("I wanna go home!");
  10. decltype(str.size()) n = 0; //这里注意必须初始化
  11. while ( n < str.size()) {
  12. str[n] = 'X';
  13. ++n;
  14. }
  15. cout << str << endl;
  16. cout << n << endl;
  17. return 0;
  18. }

part 2

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. string str("I wanna go home!");
  10. for ( decltype(str.size()) index = 0; index < str.size(); ++index) {
  11. str[index] = 'X';
  12. }
  13. cout << str << endl;
  14. return 0;
  15. }

使用范围for语句更简洁,逻辑也更清晰。

Exercise 3.9

不合法,不能用下标访问空字符串。

Exercise 3.10

  1. #include <iostream>
  2. #include <string>
  3. using std::string;
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. int main()
  8. {
  9. cout << "Please input a string with punctuation: " << endl;
  10. string str, result;
  11. cin >> str;
  12. if (!str.empty()) {
  13. for (auto &c : str) {
  14. if ( !ispunct(c))
  15. result += c;
  16. }
  17. cout << result << endl;
  18. return 0;
  19. }
  20. cout << "Your string is fucking empty!" << endl;
  21. return -1;
  22. }

注:这里使用ispunct()但不Include <cctype>是因为iostream中间接包含了cctype。

Exercise 3.11

c为常量字符串的引用(底层const),只读取数据——合法,通过其修改数据——不合法。

Exercise 3.12

(a)正确,元素为向量,元素的元素为int;

(b)错误,拷贝初始化类型必须统一;

(c)正确,向量拥有10个空字符串元素;

Exercise 3.13

(a)无元素;

(b)10个元素,值均为0;

(c)10个元素,值均为42;

(d)一个元素,值为10;

(e)两个元素,值分别为10、42;

(f)10个元素,均为空字符串;

(g)10个元素,均为"hi";

Exercise 3.14

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. vector<int> ivec;
  12. int i;
  13. while (cin >> i) {
  14. ivec.push_back(i);
  15. return 0;
  16. }
  17. }

Exercise 3.15

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. vector<string> svec;
  12. string str;
  13. while (cin >> str) {
  14. svec.push_back(str);
  15. return 0;
  16. }
  17. }

Exercise 3.16:

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. //v1
  12. vector<int> v1;
  13. cout << "The size is: "<< v1.size() << endl;
  14. cout << "The value is: ";
  15. for (auto i : v1)
  16. cout << i << " ";
  17. cout << endl;
  18. //v2
  19. vector<int> v2(10);
  20. cout << "The size is: "<< v2.size() << endl;
  21. cout << "The value is: ";
  22. for (auto i : v2)
  23. cout << i << " ";
  24. cout << endl;
  25. //后续同理
  26. }

Exercise 3.17

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. vector<string> text;
  12. string word;
  13. while (cin >> word) {
  14. text.push_back(word);
  15. }
  16. cout << "The original text is: " << endl;
  17. for (auto i : text) {
  18. cout << i << endl;
  19. }
  20. cout << "The changed text is: " << endl;
  21. for (auto str : text) {
  22. for (auto &c : str) {
  23. c = toupper(c);
  24. }
  25. cout << str << endl;
  26. }
  27. return 0;
  28. }

Exercise 3.18

不合法,不能使用无效下标访问不存在的元素。修改如下:

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. vector<int> ivec;
  12. ivec.push_back(42);
  13. cout << ivec[0] << endl;
  14. return 0;
  15. }

Exercise 3.19

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. vector<int> ivec1(10, 42);
  12. vector<int> ivec2{42, 42, 42, 42, 42, 42, 42, 42, 42, 42};
  13. vector<int> ivec3;
  14. for (auto i = 0; i != 10; ++i)
  15. ivec3.push_back(42);
  16. vector<string> svec1{10, "42"};
  17. vector<string> svec2(10, "42");
  18. return 0;
  19. }

ivec1的定义方式简洁且高效。

Exercise 3.20

part 1:

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. vector<int> ivec;
  12. int i = 0, sum = 0;
  13. while (cin >> i)
  14. ivec.push_back(i);
  15. if (ivec.empty()) { //因为后续用到下标,这里必须验证是否为空!!
  16. cout << "Please input at least one integer"<< endl;
  17. return -1;
  18. }
  19. cout << "The sum is: " << endl;
  20. for (decltype(ivec.size()) index = 0; index < ivec.size() - 1; ++index) {
  21. sum = ivec[index]+ ivec[index + 1];
  22. cout << sum << " ";
  23. }
  24. cout << endl;
  25. return 0;
  26. }

part 2:

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. vector<int> ivec;
  12. int i = 0, sum = 0;
  13. while (cin >> i)
  14. ivec.push_back(i);
  15. if (ivec.empty()) {
  16. cout << "Please input at least one integer"<< endl;
  17. return -1;
  18. }
  19. cout << "The sum is: " << endl;
  20. for (decltype(ivec.size()) index = 0; index <= ivec.size()/2; ++index) {
  21. if (index < ivec.size() - 1 - index) {
  22. sum = ivec[index]+ ivec[ivec.size() - 1 - index];
  23. cout << sum << " ";
  24. }
  25. else if (index == ivec.size() - 1 - index) { //这里一定注意为双等号!!
  26. cout << ivec[index];
  27. }
  28. }
  29. cout << endl;
  30. return 0;
  31. }

Exercise 3.21

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. //以v2为例,其他同理
  12. vector<int> v2(10);
  13. if (v2.empty()) {
  14. cout << "This is a empty vector " << endl;
  15. return -1;
  16. }
  17. cout << "The size of the vector is: " << v2.size() << endl;
  18. cout << "The values of the elements are: ";
  19. for (auto it = v2.cbegin(); it != v2.cend(); ++it) {
  20. cout << *it << " ";
  21. }
  22. cout << endl;
  23. return 0;
  24. }

Exercise 3.22

  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using std::string;
  5. using std::cin;
  6. using std::cout;
  7. using std::endl;
  8. using std::vector;
  9. int main()
  10. {
  11. cout << "Please enter a text: " << endl;
  12. vector<string> svec;
  13. string str;
  14. while (getline(cin, str))
  15. svec.push_back(str);
  16. auto it = svec.begin();
  17. for (auto &c : *it)
  18. c = toupper(c);
  19. for (it;it != svec.cend(); ++it) //注意这里it可不重新定义,但不能省略!!
  20. cout << *it << endl;
  21. return 0;
  22. }

Exercise 3.23

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using std::vector;
  5. using std::string;
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. int main()
  10. {
  11. vector<int> ivec(10);
  12. int i;
  13. cout << "Please enter ten integers: " << endl;
  14. for (auto it = ivec.begin(); it != ivec.cend(); ++it) {
  15. cin >> *it;
  16. *it *= 2;
  17. }
  18. for (auto i : ivec)
  19. cout << i << " ";
  20. cout << endl;
  21. return 0;
  22. }

Exercise 3.24

part 1

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using std::vector;
  5. using std::string;
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. int main()
  10. {
  11. vector<int> ivec;
  12. int i;
  13. while (cin >> i)
  14. ivec.push_back(i);
  15. cout << "The sum is: " << endl;
  16. for (auto it = ivec.cbegin(); it != ivec.cend() - 1; ++it)
  17. cout << *it + *(it + 1) << " ";
  18. cout << endl;
  19. return 0;
  20. }

part 2

法I:

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using std::vector;
  5. using std::string;
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. int main()
  10. {
  11. vector<int> ivec;
  12. int i, sum;
  13. while (cin >> i)
  14. ivec.push_back(i);
  15. if (ivec.empty())
  16. cout << "Your vector is empty!" << endl;
  17. cout << "The sum is: " << endl;
  18. for (auto it = ivec.cbegin(); it != ivec.cbegin() + ivec.size()/2 +1; ++it) {
  19. if (it == ivec.cbegin() + (ivec.cend() - 1 - it)) {
  20. cout << *it;
  21. }
  22. else if (it < ivec.cbegin() + (ivec.cend() - 1 - it)) {
  23. sum = *it + *(ivec.cbegin() + (ivec.cend() - 1 - it));
  24. cout << sum << " ";
  25. }
  26. }
  27. cout << endl;
  28. return 0;
  29. }

法II:

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using std::vector;
  5. using std::string;
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. int main()
  10. {
  11. vector<int> ivec;
  12. int i, sum;
  13. while (cin >> i)
  14. ivec.push_back(i);
  15. if (ivec.empty())
  16. cout << "Your vector is empty!" << endl;
  17. cout << "The sum is: " << endl;
  18. for (auto beg = ivec.cbegin(), end = ivec.cend() - 1; beg <= end; ++beg, --end) { //注意方法!!
  19. if (beg == end)
  20. cout << *beg;
  21. else
  22. cout << *beg + *end << " ";
  23. }
  24. cout << endl;
  25. return 0;
  26. }

Exercise 3.25

  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. using std::vector;
  5. using std::string;
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. int main()
  10. {
  11. vector<unsigned> scores(11, 0);
  12. unsigned grade;
  13. auto it = scores.begin();
  14. while (cin >> grade) {
  15. if (grade <= 100) {
  16. ++*(scores.begin() + grade/10);
  17. }
  18. }
  19. for (auto i : scores)
  20. cout << i << " ";
  21. cout << endl;
  22. return 0;
  23. }

Exercise 3.26

两个迭代器间只定义了相见,未定义相加;

所以可通过利用iterator - iterator将iterator +iterator转化为iterator + n。

Exercise 3.27

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号