当前位置:网站首页>Chapter IV expression
Chapter IV expression
2022-06-12 13:52:00 【Doll noodles I】
List of articles
1. Left and right
To sum up briefly : When an object is used as an R-value , Using the value of the object ( Content ). When an object is used as an lvalue , It's the identity of the object ( Location in memory . Address )
Where an R-value is required, an l-value can be used instead , But the right value cannot be used as the left value
2. Order of evaluation
int i = 0;
cout << i++ << " " << i << endl; //0 1
cout << i << " " << i++ << endl; //1 0
cout << i << " " << ++i << endl; //1 1
cout << ++i << " " << i << endl; //1 1
3. remainder
// Symbols only look at the remainder
-3 % 2 = -1;
3 % -2 = 1;
// Floating point number can't take remainder , And the remainder cannot be 0
4. Operator priority
Logic is not ! > The arithmetic ( Add, subtract, multiply and divide ) > Relationship ( Greater than or equal to but not equal to ) > Logic and && > Logic or || > assignment =;
* Priority of dereference < Front and back ++;
* Priority of dereference < .;
cout << *it++;
First ++, After ++ Will return to the original it;
Dereference is to dereference the original it;
5. Right association law , Multiple variables are assigned at the same time
int a, b, c;
a = b = c = 0;
int i;double d;
i = d = 3.5; //3 3.5
d = i = 3.5; //3 3
6. Assignment loop optimization writing
// before
int i = get_value();
while(1 != 42)
{
...
i = get_value();
}
// Now?
int i;
while((i = get_value) != 42)
{
...
}
7. sizeof
Satisfy the right union law . The resulting value is a size_t type
sizeof *p; // Right association law , Operation is equivalent to sizeof(*p). And don't care p Value , So whatever you return is 4
Be careful :
- When a character array represents a string , Its
sizeof
Value will ’/0’ Count it in .(strlen
Can't calculate ’\0’, And the terminator is encountered ’\0’ stop it ) - When the array is a formal parameter , Its
sizeof
The value is equivalent to the... Of the pointersizeof
value .
8. Transformation in type
bool -> char -> unsigned char -> short -> unsigned short -> int -> long -> float -> double
9. Cast
//type For the target data type ,expression A variable or expression of the original data type
cast-name<type>(expression);
cast-name = [static_cast | dynamic_cast | const_cast | reinterpret_cast];
1.static_cast
It's equivalent to the traditional C Coercion in language , Can be cast to any type except the following ,
Can't switch off expression Of const、volatile、 perhaps __unaligned attribute .
2.const_cast
const_cast, Used to modify the type of const or volatile attribute .
This operator is used to modify the const( The only one with this ability C+±style Transformation operators ) or volatile attribute . except const or volatile Beyond modification , new_type and expression Of the same type .
- Constant pointers are converted into non constant pointers , And still point to the original object ;
- Constant references are converted to non constant references , And still point to the original object ;
- const_cast It is generally used to modify the bottom pointer . Such as const char *p form .
const int g = 20;
//int *h = &g;
int *h = const_cast<int *>(&g);
*h = 52;
cout << g + 10; //30
cout << *h + 10; //62
3.reinterpret_cast
( It is not recommended to use )
type It must be a pointer 、 quote 、 Arithmetic Type 、 Function pointer or member pointer .
It can convert a pointer to an integer , You can also convert an integer to a pointer ( First convert a pointer to an integer , Then convert the integer to the pointer of the original type , You can also get the original pointer value ).
边栏推荐
- Comparator summary
- 上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
- Codeforces 1638 D. Big Brush —— BFS
- Now you must know the pointer
- CUDA error: CUBLAS_ STATUS_ NOT_ INITIALIZED when calling `cublasCreate(handle)`
- 阿里云开发板HaaS510解析串口JSON数据并发送属性
- 1003: align output
- Recursion of subviews of view
- Codeforces 1637 D. yet another minimization problem - Mathematics, DP
- 1001:Hello,World
猜你喜欢
Understanding recursion
Hash tables, sets, maps, trees, heaps, and graphs
List of common ACM knowledge points (to be continued)
D1 Nezha Development Board understands the basic startup and loading process
拆改广告机---业余解压
[semidrive source code analysis] [x9 chip startup process] 25 - Introduction to mailbox inter core communication mechanism (code analysis) rpmsg-ipcc RTOS & QNX
阿里云开发板HaaS510报送设备属性
阿里云开发板HaaS510响应UART串口指令
[WUSTCTF2020]颜值成绩查询-1
Display logs in the database through loganalyzer
随机推荐
AWLive 结构体的使用
What is the default gateway
Code debugging - print log output to file
CUDA error: CUBLAS_ STATUS_ NOT_ INITIALIZED when calling `cublasCreate(handle)`
[WUSTCTF2020]颜值成绩查询-1
Codeforces 1638 D. Big Brush —— BFS
Player screen orientation scheme
Alibaba cloud development board haas510 connects to the Internet of things platform -- Haas essay solicitation
Application of bit operation in C language
Implementing pytorch style deep learning framework similartorch with numpy
阿里云开发板HaaS510解析串口JSON数据并发送属性
上海解封背后,这群开发者“云聚会”造了个AI抗疫机器人
Real time software source code of COVID-19
Now you must know the pointer
Talk about the top 10 classic MySQL errors
[advanced MySQL] query optimization principle and scheme (6)
Hash tables, sets, maps, trees, heaps, and graphs
jupyternotebook有汉字数据库吗。在深度学习中可以识别手写中文吗
Codeforces 1629 F2. Game on sum (hard version) - Yang Hui's triangle, violence, finding rules
Tree reconstruction (pre order + middle order or post order + middle order)