Introduction to C + + novice-4

Keywords: C++ Back-end

## Introduction to C + + novice -- 4

C + + storage class

C + + storage class: defines the range and life cycle of variables / functions in C + + programs
include:

  • auto
  • register
  • static
  • extern
  • mutable
  • thread_local

Didn't look at it in detail

C + + operator

Arithmetic operator

+,-,*,/,%,++,–

Relational operator

==,!=,>,<,>=,<=

Logical operator

&&,||,!

Bitwise Operators

&[and], | [or], ^ [XOR], ~ [non]
Suppose that if A = 60 and B = 13 are now represented in binary format, they are as follows:

A = 0011 1100

B = 0000 1101

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A = 1100 0011

< < [binary shift left], > > [binary shift right]
Binary shift left operator. Shift all binary bits of an operand to the left by several bits (the binary bits on the left are discarded and 0 is filled on the right). [a < < 2 will get 240, i.e. 1111 0000]
Binary shift right operator. All binary bits of a number are shifted to the right by several bits, positive numbers are supplemented by 0 to the left, negative numbers are supplemented by 1 to the left, and the right is discarded. [a > > 2 will get 15, that is 0000 1111]

Assignment Operators

=,+=,-=,*=,/=,%=,<<=,>>=,&=,|=,^=,~=,

Miscellaneous operator

  • sizeof operator (returns the size of the variable)
  • Condition?X:Y (condition operator, condition is true, value is x, otherwise value is y)
  • Comma operator, (performing a series of operations in sequence)
    code:
/*Comma Operator */
#include<iostream>
using namespace std;
int main() {

	int i, j;

	j = 10;
	i = (j++, j + 100, 999 + j);

	cout << i;

	return 0;
}

result:

Process:
Solve the expressions 1 (j + +) = = > J = 11, 2 (j+100) = = > 11 + 100 = 111, 3 (999+j) = = > 11 + 999 = 1010 in turn;
Assign the value of expression 3 to i
i = 1010

  • Member operators. And - > (the. Dot operator is used to access the members of the structure; when accessing the members of the structure through the pointer, the - > arrow operator [not understood])
  • Cast character
    code:
/*Cast operator*/
#include<iostream>
using namespace std;
int main() {

	double a = 21.09399;
	float b = 10.20;
	int c;

	c = (int)a;
	cout << "Line 1 - Value of (int)a is :" << c << endl;

	c = (int)b;
	cout << "Line 2 - Value of (int)b is  :" << c << endl;

	return 0;
}

result:

  • Pointer operator & (returns the address of a variable)
  • Pointer operator * (returns the value value corresponding to the pointer variable)
    code:
/* Pointer operators & and* */
#include<iostream>
using namespace std;

int main() {

	int var;
	int *ptr; //Pointer to the address of ptr -- it is an address variable
	int val;

	var = 3000;
	
	//Get the address of var
	ptr = &var;//ptr is a pointer variable, which can only store addresses, so the corresponding value should also be the address value, & that is, the address of the variable

	//Get the value of ptr
	val = *ptr;//val is a variable, which stores the variable value of the corresponding address, and the * sign acts on the pointer variable to obtain the variable value of the corresponding address

	cout << "Value of var :" << var << endl;//Stored is the value of the variable var
	cout << "Value of ptr :" << ptr << endl;//Stored is the address value stored in the pointer variable ptr
	cout << "Value of val :" << val << endl;//Stored is the value value of the address stored in the pointer variable

	return 0;
}

result:

Operator priority in C + +

/*Operator priority*/
#include<iostream>
using namespace std;
int main() {

	int a = 20;
	int b = 10;
	int c = 15;
	int d = 5;
	int e;

	e = (a + b) * c / d;//(30*15)/5
	cout << "(a + b) * c / d The value of is" << e << endl;

	e = ((a + b) * c) / d;//(30*15)/5
	cout << "((a + b) * c) / d The value of is" << e << endl;

	e = (a + b) * (c / d);//30*(15/5)
	cout << "(a + b) * (c / d)The value of is" << e << endl;

	e = a + (b * c) / d;//20+(150/5)
	cout << "a + (b * c) / d The value of is" << e << endl;

	return 0;
}

result:

while Loop

code:

/*while loop*/
#include<iostream>
using namespace std;
int main() {

	//Local variable declaration
	int a = 10;

	//while loop execution
	while (a < 20) {
		cout << "a = " << a << endl;
		a++;
	}

	return 0;
}

result:

for loop

code:

/*for loop*/
#include<iostream>
using namespace std;
int main() {
	//for loop traversal
	for (int a = 10; a < 20; a++) {
		cout << "a The value of is:" << a << endl;
	}
	return 0;
}

result:

Range based for loop

code:

/*Range based for loop*/
#include<iostream>
using namespace std;

int main() {

	int my_array[5] = { 1,2,3,4,5 };

	for (int &x:my_array) {//The address of X points to my_array address? Anyway, x stands for my_ All elements of array
		x *= 2;//The operation on x is actually on my_array operation
		cout << x << endl;
	}

	//auto type is in the new C++11 standard, which is used to automatically obtain the type of variable
	for (auto &x : my_array) {//The address of x points to my again_ Address of array
		x *= 2;//To operate on x is to operate on my_array operation
		cout << x << endl;
	}

	return 0;
}

result:

The first part of the above for clause defines the variables used for range iteration. Just like the variables declared in a general for loop, their scope is only within the scope of the loop. The second block after ":" represents the range to be iterated.
code:

/**/
#include<iostream>
#include<string>
using namespace std;
int main() {

	string str("some thing");
	//range for statement
	for (auto &c : str) {//The address of c points to str
		c = toupper(c);//Modifying c is modifying str
	}
	cout << str << endl;
	return 0;
}

result:

The above program uses the Range for statement to traverse a string, change all characters to uppercase, and then output the result

do... while loop

You can ensure that the loop is executed at least once and check the conditions at the end of the loop body
code:

/*do-while loop*/
#include<iostream>
using namespace std;
int main() {

	//Local variable declaration
	int a = 10;

	//do loop execution
	do {
		cout << a << endl;
		a++;
	} while (a < 20);

	return 0;
}

result:

Nested loop for prime (not very understood)

code:

/*Nested loops find prime numbers in 1-100*/
#include<iostream>
using namespace std;
int main() {

	int i, j;
	for (i = 2; i < 100; i++) {//The smallest prime number in nature is 2
		for (j = 2;j<=(i/j);j++) {
			if (!(i % j)) {
				break;
			}
		}
		if (j > (i / j)) {
			cout << i << "Is a prime number\n";
		}
	}

	return 0;
}

result:

Posted by pasychosheep on Tue, 09 Nov 2021 10:21:54 -0800