[关闭]
@Moritz 2019-01-25T05:12:58.000000Z 字数 2359 阅读 392

Exercises in Chapter 3

C++primer练习 C++ 编程 所有文稿


Vector

3.17: change to uppercase

Read a sequence of words from cin and store the values a vector. After you’ve read all the words, process the vector and change each word to uppercase. Print the transformed elements, eight words to a line.

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {
  6. vector<string> v;
  7. string t;
  8. char c;
  9. int i=0,u='A'-'a';
  10. while(cin>>t) //怎么停止输入?
  11. {
  12. v.push_back(t);
  13. i++;
  14. if(i>2) break;
  15. }
  16. for(auto &c:v)
  17. {
  18. for(i=0;c[i]!='\0';i++) //不能用*(c+i)的形式,不能创建引用的指针
  19. {
  20. if (c[i]>='a'&&c[i]<='z') c[i]+=u;
  21. }
  22. }
  23. for(auto c:v)
  24. cout<<c<<endl;
  25. return 0;
  26. }

Better Version

  1. for (auto &str : v)//把全部字符改为大写
  2. {
  3. for (auto &c : str)//嵌套范围for代替下标遍历
  4. {
  5. c = toupper(c);//string函数
  6. }
  7. }

3.19: three ways to define

List three ways to define a vector and give it ten elements, each with the value 42. Indicate whether there is a preferred way to do so and why

  1. vector<int> v1(10,42);
  2. vector<int> v2={42,42,42,42,42,42,42,42,42,42};
  3. vector<int> v3(10);
  4. for(auto &i:v3)
  5. i=42;

第一种方式最好

3.20: output the sum of elements

Read a set of integers into a vector. Print the sum of each pair of adjacent elements. Change your program so that it prints the sum of the first and last elements, followed by the sum of the second and second-tolast, and so on.

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int main()
  5. {
  6. vector<int> v;
  7. int n,i,t;
  8. cin>>n;
  9. for(i=0;i<n;i++) {cin>>t;v.push_back(t);}
  10. for(i=0;i<n-1;) {cout<<v[i]<<" "<<v[i+1]<<endl;i+=2;}
  11. cout<<endl;
  12. for(i=0;i<n/2;i++) cout<<v[i]+v[n-1-i]<<endl;
  13. return 0;
  14. }

2019.1.23


Multidimensioanl Arrays

3.43 three ways to print ia

Write three different versions of a program to print the elements of ia. In all three programs write all the types directly. That is, do not use a type alias, auto, or decltype to simplify the code.

  1. #include <iostream>
  2. using namespace std;
  3. int ia[3][4]= {0,1,2,3,4,5,6,7,8,9,0,11};
  1. use a range for

    ia中的元素是含有四个整型数元素的数组,所以row含有四个整型数元素的数组的引用(别名),在第二层for语句中可以直接使用row别名。

  1. for(int (&row)[4]:ia)
  2. {
  3. for(int col:row)
  4. cout<<col<<'\t';
  5. cout<<endl;
  6. }
  1. use an ordinary for loop with subscripts

    注意类型名是size_t

  1. for(size_t i=0; i<3; i++)
  2. {
  3. for(size_t j=0; j<4; j++)
  4. cout<<ia[i][j]<<'\t';
  5. cout<<endl;
  6. }
  1. use an ordinary for loop with pointers

    rowcol都是指针类型,int (*row)[4]=ia,把ia首元素(即含有四个整型数元素的数组)地址赋给row;第二层循环中,col是指向整型的指针,*row解引用得到一个数组(名),并把该数组首元素地址赋给col

    注意输出col的时候要加上*,代表访问col所指地址的值。

  1. for(int (*row)[4]=ia; row!=ia+3; row++)
  2. {
  3. for(int *col=*row; col!=*row+4; col++)
  4. cout<<*col<<'\t';
  5. cout<<endl;
  6. }

若不加上*,输出的是col本身(即地址)

  1. /*
  2. 0x28fed0 0x28fed4 0x28fed8 0x28fedc
  3. 0x28fee0 0x28fee4 0x28fee8 0x28feec
  4. 0x28fef0 0x28fef4 0x28fef8 0x28fefc
  5. Process returned 0 (0x0) execution time : 0.265 s
  6. Press any key to continue.
  7. */

2019.1.25


添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注