Saw a case in C++ primerplus Chapter 6 Branching Statements,
char ch;
cout << "Typr , and I shell repeat.\n";
cin.get(ch);
while (ch != '.') {
if (ch == '\n')
cout << ch;
else
cout << ch +1;
cin.get(ch);
}
cout << "\n Please excuse thr slight confusion.\n";
When we replace cout<<ch+1 with cout<<++ch, two different results appear:
-
ch+1

-
++ch

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.
int cookies[8] = {1,2,4,8,16,32,64,128};
void sum_arr(arr,8)
Access array with capacity 8, sum is 255;
void sum_arr(arr,3);
Access the same array, capacity 3, result is 7.
void sum_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.
Function Pointers
double besty(int);
double pam(int);
void estimate(int,double(*pf)(int));
int main()
{
using namespace std;
int code;
cout << "how many code you have : ";
cin >> code;
cout << "here's betsy's estimate:\n";
estimate(code, besty);
cout << "here's pam's estimate:\n";
estimate(code, pam);
return 0;
}
double besty(int ins) {
return 0.05 * ins;
}
double pam(int ins) {
return 0.03 * ins + 0.0004 * ins * ins;
}
void estimate(int lines, double (*pf)(int)) {
using namespace std;
cout << lines << "lines will take ";
cout << (*pf)(lines) << "hours(s) \n";
}
Calling functions via function pointer addresses.
Reference Variables as Function Arguments
Chapter 8 Functions Adventures - Reference Variables. Check back later if understanding improves.
Book example, using reference variables, pointers, and actual arguments for swapping parameters. Using references for value swapping:
void swapr(int& a, int& b) {
int tmp;
tmp = a;
a = b;
b = tmp;
}
void swapv(int a, int b) {
int tmp;
tmp = a;
a = b;
b = tmp;
}
void swap(int* a, int* b) {
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
void swapv(int a, int b);
void swap(int* a, int* b);
void swapr(int& a, int& b);
int main()
{
int wallent1 = 300;
int wallent2 = 350;
swapv(wallent1, wallent2);
cout << "wallent1 = $" << wallent1 << " wallent2 = $ " << wallent2;
swap(&wallent1, &wallent2);
cout << "wallent1 = $" << wallent1 << " wallent2 = $ " << wallent2;
swapr(wallent1, wallent2)
cout << "wallent1 = $" << wallent1 << " wallent2 = $ " << wallent2;
return 0;
}
- When we use reference variables for value swapping,
wallent1,wallent2are passed in as a variable name fora, b, simplified asint& a = wallent1. At this timea, bact 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,
acreates a temporaryintvariable withwallent1’s value. In the function relationship, onlya, bvalues are swapped, irrelevant to passed values.
You can see a, b and wallent1, wallent2 point to different addresses.
Strings, Vectors, and Arrays
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.
int main() {
using namespace 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;
}
return 0;
}
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.
size_t count_calls(){
static size_t ctr = 0;
return ++ctr;
}
int main() {
using namespace std;
for (size_t i = 0; i != 10; ++i) {
cout<<count_calls()<<endl;
}
return 0;
}
