"C++primer" learning: Chapter 1 (notes and programming exercises)

The first chapter of "C++primer":

1. Count how many consecutive times each value appears in the input?

#include <iostream>

// 程序设计:键盘获取一组不定长输入,求每个值连续性地输入了多少次?
int main()
{
    
    
	int Val, CurVal;
	if (std::cin >> CurVal) {
    
    
		// 如果确实得到了输入
		int cnt = 1;// 连续输入的次数初始化
		while (std::cin >> Val) {
    
    
			if (CurVal == Val) 
				cnt++;
			else {
    
    
				std::cout << CurVal << " occupys :" << cnt << " times." << std::endl;
				cnt = 1;// 准备记录下一次的输入次数
			}
			CurVal = Val;
		}
		std::cout << CurVal << " occupys :" << cnt << " times." << std::endl;
		// 输出最后一组连续输入的元素的出现次数
	}
	return 0;
}

Advantages of this program:

First: Use an if statement to detect whether there is input, to ensure that there must be data before processing

Second: Regard the first input as the current input, and use another Val variable to catch it every time you enter it next time, and then compare it. After the comparison, the current variable CurVal is updated alternately. This uses two variables The idea of ​​alternate input and detection work is very good.
Insert picture description here
This variable length input is input to the end of the file. If you want to interrupt it in advance: Ctrl + Z (win system), or Ctrl + D in Mac system

2. Research the return value of input and output

As we all know, functions will return values. In the C++ IO stream input and output, we use the classes in the istream and ostream libraries: std::cin, std::cout; the class has no return value, then the above code: while(std::cin >> What does Val){} mean? How can this input statement be put into while to determine the condition of the loop? Could it be the return value of bool type? Let's try to write such a code: bool p = std::cin >> a;Get the error as follows:
Insert picture description here
Check the source code of this std::cin:
Insert picture description here
From here, we can see that we are not really generating the return value of the std::cin class, because the class does not generate a return value ! But this operator: >>, we know that the binary operator is equivalent to a function with binary parameters. The left side of this operator takes an istream class as the left operand (that is, the first parameter of the function), and the right operand is an object to be operated. The content written in the command box will be written to the right operand. The object goes up. The following passage is from "C++primer",
Insert picture description here
but as for how to rewrite the binary operator into a function with binary parameters, I still don't know how to ask for advice from an expert!

From C++ to C language, we know that the input and output of C language are realized by functions: scanf & printf; the function naturally has a return value, let's see how its return value is?

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>

int main()
{
    
    
	int Val;
	bool p = scanf("%d", &Val);
	bool q = printf("%d", Val);
	std::cout << std::endl;

	std::cout << p << ' ' << q << std::endl;

	p = ~scanf("%d", &Val);
	q = ~printf("%d", Val);
	std::cout << std::endl;

	std::cout << p << ' ' << q << std::endl;

	std::cout << EOF << std::endl;

	return 0;
}

Output result: EOF = -1, C-style input and output return value is 1 (indicating that the end of the file has not been reached and the input/output is successful)

Insert picture description here

3. File redirection

Insert picture description here
Use cmd command to redirect under Windows 10 system:"程序编译后的.exe可执行文件" <input.txt >out.txt
Insert picture description here

Benefits of file redirection:

In an oj test case a few days ago, the amount of data was huge! If I copy input and output in the console, it is impossible to copy completely! Because the last most of the data will be swallowed and cannot be entered into the console. then what should we do? We use the "redirect" method to put the input file into a txt file, and then create an output file (empty txt file). Then use the above redirected command to output to see our output!
In addition, regarding redirection, there is also an explanation on "C++primer":

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44274276/article/details/105277148