We can see that one advances the character by one, and the other converts `char` to integer. Reading the book and adding my own current understanding, I think `cout` output parameters automatically adjust output based on content. So the former is recognized as `char` increment, and the latter is recognized as integer addition. The final result outputs `c`'s code + 1 (as int) vs 'c' (as char). **The difference between 'A' and "A" is that string A is equivalent to 'A\0', adding a null character after char 'A'** # Two EOF statement, resets file end. ## Functions and Arrays `void(int[] arr,int n)` Here `arr` represents an array pointer. By accessing the pointer of the first value of the `arr` array, the entire array is accessed. The book uses an example to explain this situation. ```cpp int cookies[8] = {1,2,4,8,16,32,64,128}; void sum_arr(arr,8)
Access array with capacity 8, sum is 255;
1
voidsum_arr(arr,3);
Access the same array, capacity 3, result is 7.
1
voidsum_arr(arr+4,4);
Array capacity 4, calculating sum of the last four numbers. Result is 240. From the three above, we can see that arr is passed as the first pointer of the array in the function.
When we use reference variables for value swapping, wallent1, wallent2 are passed in as a variable name for a, b, simplified as int& a = wallent1. At this time a, b act as formal parameters for swapping values. You can see that after value swapping ends, the value of wallent1 and the address of a match.
If values are passed directly into the function, a creates a temporary int variable with wallent1‘s value. In the function relationship, only a, b values are swapped, irrelevant to passed values.
You can see a, b and wallent1, wallent2 point to different addresses.
Strings, Vectors, and Arrays
1 2 3 4 5 6 7 8 9 10
string s = "hiYa";// Copy initialization, copy value 'hiYa' to s string s2("hiYa");// Direct initialization string s3(10,'c');// Direct initialization, same meaning as vector(10,'c'), create specified number of 'c'
vector<unsigned > scores {11,0};// Define a vector with capacity 11, values 0. Direct initialization unsigned grade; while(cin>>grade){ if(grade<=100) ++scores[grade/10];// Equivalent to auto ind = grade/10; scores[ind] = scores[ind]+1; Equivalent to retrieving value at specific position and incrementing }
C++ Primer Part 5.4
Exercise 5.4.1: Read several strings and find consecutively repeated words, output the maximum number of repetitions. If none exist, output a message stating no words appeared consecutively. I saw a similar problem when grinding LeetCode before, forgot how I did it so I re-wrote it. Feel like it needs optimization, the logic is a bit messy, will optimize later.
intmain(){ usingnamespace std; string s;// Intermediate value int count=0;// Final value int tmp = 1;// Intermediate value, start at 1 string sStr;// Final string value vector<string> strings {"how","now","cow","cow"};// Define object collection s = strings[0];// Use first as judgment object for first loop for (int i = 1; i < strings.size(); ++i) { if(strings[i]==s){ ++tmp;// If equal, increment count } else { if (tmp > count&&tmp>=2)// If consistent with recorded string intermediate value and tmp>2, swap object, assign count { count = tmp; sStr=s; s = strings[i]; tmp = 1; } else{// If count doesn't exceed existing sum, simply swap intermediate value tmp=1; s=strings[i]; } } // Using for loop or while loop this way will miss the last array value if(strings.size()-1==i&&strings[i]==s) {if(tmp>count&&tmp>=2) {count = tmp;sStr=strings[i];} }
} // If no duplicates in object collection if(count==0&&s==strings[strings.size()-1]){cout<<"No duplicate objects in collection"<<endl;} else{ cout<<count<<sStr<<endl; }
return0; }
C++ Primer Part 6.1 Function Basics
Local static object: Initialized when program execution path first passes the object definition statement, and is not destroyed until the program terminates. Even if the function where the object resides finishes executing, it has no effect on it.
1 2 3 4 5 6 7 8 9 10 11 12
size_tcount_calls(){ staticsize_t ctr = 0; return ++ctr; } intmain(){ usingnamespace std; for (size_t i = 0; i != 10; ++i) { cout<<count_calls()<<endl; } return0; }