[C + +] some syntax in C + + compared with C language

Keywords: C++


Recently, when looking at the data structure, I saw that many problem solutions were written in C + +, and I only learned the C language. Therefore, I want to quickly go through the syntax of C + + and record some contents that are easy to forget at the beginning, so as to facilitate the initial reference and use. If there are errors, welcome to correct!

1. cout

When you need to display content on the output device (that is, when printing), you can use cout to include the standard library
Include < iostream > e.g.:

#include <iostream>

int main()
{
		int c = 6;
		std::cout << c << std::endl;
		std::cout << c << std::endl;
		return 0;
}

Print results:

6
6

--------------------------------
Process exited after 0.006504 seconds with return value 0
 Please press any key to continue. . .

Where < < std:: endl stands for printing line breaks. If you don't want to write std:: in front of cout and endl every time you call, you can add a statement to release cout and endl, that is:

#include <iostream>
using namespace std;
int main()
{
		int c = 6;
		cout << c << endl;
		cout << c << endl;
		return 0;
}

The print result is the same.


2.C + + storage type

(1) static storage class

The static storage class instructs the compiler to maintain the existence of local variables throughout the life cycle of the program without creating and destroying them every time it enters and leaves the scope. Therefore, using static to modify local variables can maintain the value of local variables between function calls.

The static modifier can also be applied to global variables. When static modifies a global variable, it limits the scope of the variable to the file in which it is declared.

(2) extern storage class

When you have multiple files and define a global variable or function that can be used in other files, you can use extern in other files to get a reference to the defined variable or function. It can be understood that extern is used to declare a global variable or function in another file.

The extern modifier is usually used when two or more files share the same global variable or function.

3. Bitwise operator

Bit operators operate according to binary bits. Only binary shift left operators < < and shift right Operators > > are recorded here

#include <iostream>

using namespace std;

int main()
{
		unsigned int a = 60;      // 60 = 0011 1100  
		int c = 0;           
		
		c = a << 2;            // 240 = 1111 0000 = 60 * 2^2;
		cout << "c The value of is " << c << endl ;
		
		c = a >> 2;            // 15 = 0000 1111 = 60 * 2^(-2);
		cout << "c The value of is " << c << endl ;
		
		return 0;	
}

Print results

c The value of is 240
c The value of is 15

--------------------------------
Process exited after 0.02435 seconds with return value 0
 Please press any key to continue. . .

4. Pseudo random number generation

In many cases, random numbers need to be generated. About random number generators, there are two related functions. One is rand(), which returns only a pseudo-random number. The srand() function must be called before generating a random number.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
		int i , j;
		
		//Set seed
		srand( (unsigned)time( NULL ));
		
		for( i = 0 ; i < 10 ; i++ )
		{
				j = rand();
				cout << "Random number: " << j << endl;		
		} 
		return 0;
}
Random number: 6508
 Random number: 3722
 Random number: 29551
 Random number: 24689
 Random number: 19719
 Random number: 6245
 Random number: 16151
 Random number: 15374
 Random number: 4022
 Random number: 7174

--------------------------------
Process exited after 0.01974 seconds with return value 0
 Please press any key to continue. . .

5.C++ setw() function sets the width of the field

Call format: setw (n), n is represented by numbers and represents width

#include <iostream>
#include <iomanip> 		// To include this header file

using namespace std;

int main()
{
		char str[] = "word";
		cout << setw(8) << str << str << endl;
		return 0;
}

The output results are as follows:

    wordword

--------------------------------
Process exited after 0.02433 seconds with return value 0
 Please press any key to continue. . .

You can see that setw(n) works only on the output element immediately following it.

Posted by stephenf33 on Mon, 13 Sep 2021 19:45:22 -0700