Linux C++ self-study notes 2

Boolean data

The value is only true and false

Enumerated type

Enumeration declaration

Format: enum enumeration name {element name 1, element name 2...element name n};
For example:

enum MONTH{
    
    JAN,FEB,MAR,APR,MAY,JUNE,JUL,AUG,SEP,OCT,NOV,DEC};

The elements in the enumeration type declaration are enumeration text, not
how are the variables stored inside? He will map the elements inside to numbers, where the top element is mapped to 0, and the following text is automatically the previous text +1. I can also customize the order, for example, change JAN to JAN = 1, which means that it increases from 1.

Definition of enum

E.g:

MONTH month;

Then month can be assigned, month can be assigned JAN, it can be FEB. But note that although the internal storage is in integers, month cannot be directly assigned to 1, or other integers.

User-defined type

Custom type format

Format: typedef The original type identifies the new type name

The nature of the custom type

The new type is the same as the original type, and no new type is generated. The renamed custom type makes the program easier to understand.
If integers can be used to represent two different types of data objects, using custom types can distinguish their custom types, not simple type substitution, although they are indeed equivalent.

Boolean type

Value: false true
false means 0, true is 1;

Definition of bool amount
Definition: bool modified;
assignment: modified = true;

Taking the C99 standard as an example, the bool type cannot be used directly in C, but C++ can be used; if C needs to be used, it needs to include the stdbool.h header file, or define it yourself like the following

typedef bool _Bool
#define true 1
#define false 0

It can be seen that _Bool is a bottom layer and can be directly used as a bool. Its length is 1, and the bool length is 4, so _Bool saves more space.

Logical expression

Note: In c/c++, 0 means false, and non-zero (usually 1) means true. So try to express logical values ​​with Boolean values.

关系操作符:<,>,==,<=,>=,!=
逻辑表达式:&&,||,!
关系操作符和逻辑操作符的优先级从高到底
!
< , >= , > , <=     结合顺序是从左到右
== ,!=
&
||

Add a function

cout<<'s'<<setw(3)<<'a'<<endl;

It means that there are 3-1=2 spaces between s and a, which are filled with spaces by default. You can use setfill() to modify the filled characters, setfill('#') is to fill characters with #. This function is in this header file.

Guess you like

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