About c and c ++ knowledge points, you should understand these (a)

@About c and c ++ knowledge points, you should understand these (1)

About c and c ++ knowledge points, you should understand these (a)


This is my own process of learning the basics of C ++. By the way, I reviewed the C language and summarized some small knowledge points. I hope that I can provide some help for everyone ’s learning. Come on and make progress together.

Section 1

1.

int a; sizeof(int) = sizeof(a); ---求所储存的字节

2.

float aa = 'A'  cout << (int)aa << endl; //输出A的数值(强制转化为 int) 注意格式

3.

float f2 = 3e2  //3*10^2  float f3 = 3e-2  //3*0.1^2

4.

bool true or false bool flag = false / true (0/1); —except 0 is false, all others are true

5. String

  (1)

char str[ ] = "hello world"  cout<<str<<endl;    

  (2) string variable name = "string value" (reference header file include "string" some are in the iostream header file)

string name[3] = {"张三","李四","王麻子"}; string name=”胖虎”;

6.

int a1 = 10; ++a1*10; //(运算结果为110)先加1后乘以10,
int a1 =10; a1++*10;   //(运算结果为101)先乘以10 后加1,先执行表达式,再增加

7.

-=  /=  %=  
int a=10; int b=5; cout<<(a==b); //输出结果为0(假)

8.

rand() % 80; //生成0-79的随机数   
rand() % 80+1; // 生成0-80随机数  
/* 需要包含头文件 #include “cstdlib” */
//   #include ”ctime“ (伪函数)  srand((unsigned int)time(NULL));  利用系统时钟生成伪数字

9.

int a = 10; int b = 20;  (a<b?a:b)=100;   cout <<a; a=100 cout<<b;  //b=2

10.

int a = 354; //个位   a%10  十位   a%100/10  百位  a/100

11.

 \t   制表符代替空格

12.

goto   flag ;  flag:   //跳转语句 ---读懂就可,不建议使用

13.

/*数组*/   int arr[5] = {1,2,3,4,5} 
/*每个数组占用的内存空间*/  sizeof(arr)
/*每个素占用的内存空间 */   sizeof(arr[0]),
/*数组中元素个数为*/       sizeof(arr) / sizeof(arr[0]) //------重点

14.

/*数组地址 */       int arr[5] = {1,2,3,4,5} 
/*数组首整型地址*/    cout<< (int)arr;  
/*第一个元素整型地址*/ cout<<(int)&arr[0];    //------重点

15. Bubble sorting method

void fun(int c[], int d)
{
	int temp=0;
	for (int a = 0; a < d - 1; a++)
	{
		for (int b = 0; b < d - 1 - a; b++)
		{
			if (c[b] <c[b + 1])
			{
				temp = c[b];
				c[b] = c[b + 1];
				c[b + 1] = temp;
			}
		}
	}
}

16.

/*二维数组 总元素*/  sizeof ( arr ) ;
/*  第一行元素  */      sizeof ( arr [0] )  ;
/*     行数    */      sizeof ( arr ) / sizeof ( arr [0] )  
/*     列数    */    sizeof ( arr [0]) / sizeof( arr [0] [0] ) )  

17.

/* 二维数组首地址  */  cout << arr << endl;  
/*  第一行首地址  */ cout << arr [0] << endl; 
/*第一个元素首地址 */ cout << & arr [0] [0] << endl;  //  此三者相等三者相等     
/*  第二行首地址  */    cout << arr[1] << endl;

18.

/*指针就是地址*/   int *p; (p为指针变量) //使用时需要解引用  *p 即可 ( *p解引用)

19.

/*空指针:  用来给指针变量进行初始化,空指针不可以进行访问的*/ int *p = NULL;  
//不可再将地址赋值给  p 输出  

20.

const修饰指针    ---重点

1)const修饰指针---常量指针 

2)const修饰常量---指针常量 

3)const既修饰指针又修饰常量  

21.

—Emphasis—

//1)常量指针  
 const int  * p = &a;       /*指针的指向可以修改,但是指针指向  的值不可修改 */

//2)指针常量  
 int * const p = &a;        /*指针的指向不可以修改,指针的值可以修改        */

//3)既修饰指针又修饰常量  
 const int * const p = &a;  /*指针的指向和指针的值都不可以修改             */

//(如何记忆: const—常量 ,* p —指针     如何用:紧接着 const 的是p / *   紧跟着谁   谁不能操作)

22.

int arr [10], int * p;  p = arr ;  cout << *p;
//(数组的名称为其的首地址)  

23.

Value transfer, you can not modify the value of the actual parameter, only modify the value of the formal parameter. Address transfer can modify the value of the actual parameter.

24.

Structure

struct student/****  自己创建的结构体类型为 student, 此类型的变量名字为s1, s2    ***/
 {
  string name;

  int age;

  int score;

  };

//1  struct  student  s1;

//2  struct student s2 = { };

//3  创建结构体时顺便创建结构体变量,通过.来访问结构体中的属性

25.

The struct cannot be omitted in the process of creating a custom type structure, but the process of creating variables c ++ struct can be omitted

26.

 //  结构体数组 struct 结构体名字 数组名字{个数}={ { },{ } ,{ } }

 //  结构体指针  利用->可以通过结构体来访问结构体的属性

 student  s1  =  { "李斯",56,99 };

 student * p  =  &s1;

 cout  << "姓名:"   <<  p->name

       <<  "年龄:"  <<  p->age

       <<  "分数"   <<  p->score;

27. Structure nested structure

 struct student
 {

  string  name;

  int  scroe;
 };

 struct teacher
{

   int  id;

   string  name;

   int  age;

   struct   student  s1;//学生  s1
};

    teacher  t;

    t.id  = 10086;

    t.name  =  "老王";

    t.age  = 58;

    t.s1. name  =  "小王";

    t.s1.scroe  =  99;

    teacher * p  =  &t;

    cout  <<  "老师姓名:"  <<  p->name

          <<  "老师工号: "  <<  p->id

          <<  "老师年龄: " <<  p->age

          <<  "老师的学生名字: " <<  p->s1.name

          <<  "老师的学生分数: " <<  p->s1.scroe;

28. Use const in structure to prevent misoperation

29

—Emphasis—

The function is called in the main, and the formal parameters are equivalent to copying the parameters once, increasing the storage space and the amount of code. Changing the formal parameters of a function into a pointer can greatly reduce the memory footprint (a pointer does not copy data, it only transfers addresses)

30.

 system( “ pause ” );  //请按任意键继续 

    system( “ cls ” ); //清屏操作

To be continued ...


If you have any questions, please leave me a message

hexo blog: https: www.ho-brother.ltd
Published 5 original articles · Likes0 · Visits 46

Guess you like

Origin blog.csdn.net/qq_44955863/article/details/105596046
Recommended