Linux C++ self-study notes 1

program

The program usually includes two parts: data structure and algorithm. According to the needs of the task, design the data structure and algorithm, write the code to test its correctness, and get the correct running result.

Programming methodology: Programming should follow certain methods and principles to develop a good programming style.

algorithm

问题的求解方法
不允许存在二义性
算法设计过程是逐步求精的
常使用流程图描述算法

data structure

数据:程序操作对象
数据结构:数据对象之间的相互关系及构造方法,与算法关系密切,良好的数据结构可使算法更简单,恰当的算法可使数据结构更易理解。

The above is very important. Things are universally connected. Algorithms and data structures must be connected. The larger the project or project, the more this relationship can be reflected. From Marxist philosophy, they can be transformed into each other under certain circumstances. In a program, the algorithm is not easy to change, and the data structure is easy to change. First of all, the parts that are not easy to change should be overcome.
Structured programming (basic)
data structure, program flow control, functions and algorithms, program organization.
Object-oriented programming
Object: collection of behaviors and attributes,
classes and objects, object construction, inheritance and derivation.
Paradigm programming
Standard template library: iterators, containers, abstract algorithms.

example

Example 1: A typical c++ code

#include <iostream>
using namespace std;
int main()
{
	cout << "hello world" << endl;
	return 0;
}

Parsing

Like C, there is a precompiled instruction (instruction to be executed before compilation) at the beginning of the code. In the early days, iostream.h was used. In the new version of C++, .h is removed, and iostream is used instead.

The direct translation of the second sentence is "use namespace std", so std is a namespace, why use std as a namespace? Because cout and endl are used in main, these two statements are in the std namespace. If you delete the second statement, you only need to change the first statement of the main function to

std::cout << "hello world" << std::endl;

If you have to add std:: every time you use cout and endl, it will be more troublesome, so write it directly at the beginning. The double colon :: is called the name resolution operator.

The third statement cout << "hello world"<<endl;
endl means end line, which means line break.
Cout is the standard output object, which outputs a line of text on the output device (usually the screen).
<< indicates the direction of data flow, from right to left! Provides the right information to the left output object.

Example 2: Calculate the sum of two integers entered by the user

#include <iostream>
using namespace std;
int main()
{
	int a = 0,b= 0,sum = 0;
	cout << "a:";
	cin >> a;
	cout << "b:";
	cin >> b;
	sum = a+b;
	cout << a << "+" << b << "=" << sum << endl;
	return 0;
}

cin >> a; means to give a number entered by the user to a;

The basic process of programming

Insert picture description here
If the source file is c++, then it is .cpp, if it is c, then the format is .c. The
header file is a .h file
. The object file generated by compilation is generally in .o format.

Editing
line text editor: vi or vim system usually comes with it.
Window text editor: gedit or other, you need to download it yourself.
Compile
gcc: compile c program
g++: compile c++ program
Example: g++ main.cpp, the current directory will generate an executable file of a.out for
execution.
Use ./a.out to execute the file a.out in the current directory.

Data type related

3.14e3 means 3.14 times the third power of 10, and e can be in upper and lower case.

constant

Format: const data type constant name = initial value;
for example: const int dd = 444; it
can only be initialized when it is defined, and then it cannot be assigned.
The meaning of constants: To solve the problem that the text that appears cannot explain its meaning.
Precautions:

  1. The initial value of the constant must be able to be calculated during compilation.
  2. The defined constant must be initialized.
  3. The constant cannot change its value.

Operator classification

Unary operator: only a single operand, such as a minus sign;
binary operator: there are 2 operands, such as a minus sign;
ternary operator: there are three operands, such as a conditional expression;

Macro definition

For example: #define PI 3.14
can be used instead of constants once defined, but macro definitions are not constants.
Write programs prefer constants instead of macros.

Shorthand

a += b is equivalent to a = a+b;
a = b is equivalent to a = a b;
a /= b is equivalent to a = a / b; if you want to get a decimal, you need to change a or b to a decimal.
a %=b is equivalent to a = a%b;
Special note: a*=a+b is equivalent to x = x*(a+b)

Source code layout

The progressive level should be left indented.
Each line of code should not exceed 80 characters.
Function code should not exceed 60 lines.
Use blank lines to distinguish different function codes.
Compound sentences (brackets should be written uniformly). The writing format should be uniform.
Unless there is special need, do not write multiple lines on one line. Line statements and
naming conventions must be consistent.
No matter what standard is adopted, it must be implemented in accordance with that standard

Guess you like

Origin blog.csdn.net/qq_35543026/article/details/105484956