expression
4.1. Basics
Unary operator: acting on an object. (&, dereference*)
Binary operator: acting on two operands (==, multiplication*);;
Priority I will
write an example for you (someone in the group just happened to ask a few days ago):
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int i = 1;
cout<<i++ + i++<<endl;//等于3
int j = 1;
cout<<++j + ++j<<endl;//等于6
system("pause");
return 0;
}
I don't like to record those that are very cumbersome (I usually give links). There are a lot of such things on the Internet, but for some experience-based things, everyone has different answers (good and bad). I try to record my understanding.
Back to the topic, why the result is 3 and 6?
int & int:operator++(){
//++i的是实现
*this += 1;
return * this;
}
const int int::operator++(int){
//i++的实现
int old = *this;
++(*this);
return old;
}
4.2, arithmetic operators
Simple addition, subtraction, multiplication, division and remainder.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int i = 2;
int b = 3;
cout<<"求余"<<b % i;
system("pause");
return 0;
}
Because many types of digits have a maximum value, if the maximum value is exceeded, it will overflow.
4.3, logical operators
And or not, greater than or less than or equal to ==, this thing is not difficult.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int i = 2;
if(i != 1 || !(i < 0)) i--;
cout<<i<<endl;
system("pause");
return 0;
}
4.4, assignment operator
Here I feel that +=, /= and other operations need to be said, this assignment operator is completely equivalent to a = a op b; op is your operation.
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
int i = 2;
i += 1;
cout<<i<<endl;
system("pause");
return 0;
}
4.5, increment and decrement operators
The increment and decrement operator can not only perform numerical increments and decrements, but can also be used for iteration of iterators. C++ primer does not recommend the use of the post version of the increment and decrement operators.
4.6, member access operator
The focus is written in the program, the following two methods are equivalent.
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
string i = "我爱你", *i_ptr = &i;
cout<<(*i_ptr).size()<<"等价于"<<i_ptr->size();
system("pause");
return 0;
}
4.7, conditional operator
condition? Condition is true output: Condition is false output
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
string i = "我爱你";
string a = (i == "我爱你") ? "我喜欢你":"对不起";
cout<<a;
system("pause");
return 0;
}
4.8, bit operator
Shifting is a kind of idea that is often used in brushing questions, which can reduce the time spent on calculations.
link
4.9, sizeof operator
Returns the number of characters in an expression or a type name.
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
cout<<sizeof(int)<<" "<<sizeof("aaa");
system("pause");
return 0;
}
If you execute sizeof on the array, you get the space occupied by the entire array, while using sizeof on the pointer gets the space occupied by the pointer itself.
4.10, comma operator
According to the order from left to right, the result is always the last operation at the end.
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
int i, j;
j = 1;
i = (j++, j+100, 10+j);
cout << i;
system("pause");
return 0;
}
4.11 Type conversion
The simple reason is that if you store the data in char_32t, occupying 31 bits, and then convert it to char_16t, the data will definitely be lost, right, there is also a floating-point conversion integer. Understanding the principle is always more important than rote memorization.
Display conversion (more interesting here):
- static_cast: Any well-defined conversion, as long as there is no underlying const, static_cast can be used.
- dynamic_cast: supports type recognition at runtime.
- const_cast: specifically used to change the underlying const of the operand.
- reinterpret_cast (almost as interpret_cast recently memorized the word string ==): Provides a lower-level reinterpretation for the operand.
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
int j = 1;
double j1 = static_cast<double>(j);
char p = '1';
const char *p_ptr = &p;
char * p_ptr1 = const_cast<char *>(p_ptr);
int *j_ptr = &j;
char *j_ptr1 = reinterpret_cast<char*>(j_ptr);
cout<<"const_cost的结果"<<*p_ptr1<<endl;
cout<<"static_cost的结果"<<j1<<endl;
cout<<"reinterpret_cost的结果"<<*j_ptr1<<endl;
system("pause");
return 0;
}
Old-style coercion:
is my more common method (the effect is the same as reinterpret_cast)
#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
int main()
{
int j = 1;
int *j_ptr = &j;
char *j_ptr1 = (char *)j_ptr;
cout<<"reinterpret_cost的结果"<<*j_ptr1<<endl;
system("pause");
return 0;
}