c++学习

 ','  逗号运算符,分隔/或去最后一个等式

while(~scanf("%d",&a)) 析构函数 输入正确执行/输入错误重新输入

自动转换转换为高的类型,或强制转换。

递归函数:把递归条件写下来,特判停止条件和递归条件

exp:Hanoi塔问题

hanoi(n, A, B, C)={mov(1, A, C)   n==1

    { hanoi(n-1, A, C, B)

    { mov(n, A, C)

    { hanoi(n-1, B, A, C)

inline 内联函数 用空间换时间/

1.函数体内代码过长不用

2.不宜出现循环

3.递归不能定义为内联函数

4.不宜出现switch复杂控制结构

带默认形参值的函数:

1.自右向左连续定力,右边没定义,左边不能定义。

2.调用时,如果省去一个实参数,右边的都要省略

3.不能重复指定

即int sub(int x = 3, int y = 4);函数原型

调用:sub(20, 15);  sub(10),sub(),  不能:sub( , 10)

函数定义:int sub(int x, int y)

{

}//x,y不能指定值

4.同一作用域中不能重复定义默认行参/不同域可以。

5.overload 参数类型(光有这个不行)/行参类型/行参个数---实现不同的功能。绑定 与 绑定的二异性 (自动转换 向高类型有两种)

6.函数模版与模版的实例化   如果模版中有普通参数必须得用显示实例化    

template<class T1,class T2>

T1 add(T1 x,T2 y)显示   add<int,double>  隐式 add(9,7)

7.abs()  fabs()  sqrt()  pow()  exp()  log()  ceil() floor()  atoi() isalpha() isdigit() 常用函数

8.枚举常量 enum 类型名{枚举常量1,枚举常量2,枚举常量3}不指定默认整数对应。(0,1)指定的后面依次加1

枚举变量 enum city{}  city1,city2

enum {} city1,city2

enum city city1 city2

city city1,city2  不赋予初值为无意义的数

枚举变量进行赋值和运算(算完为整数要强制转化) /类型要一致(强制转化)。

9.动态内存分配

int *ip = new int(5);申请一个内存赋初值为5,然后ip指向它

int *ip = new int [5] 指向5个未初始化的int型数据对象的首地址

int (* ip)[4][5]

ip = new int [4][5][6];

int ( * ip )[4]

new int [4][5];

10.释放创建的动态内存

delete ip

delete [] pa;

delete [] pb; //无论什么格式都是这样

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7   int m,n;
 8   int ** dm;
 9   cout<<"input matrix size m,n:";
10   cin>>m>>n;
11   int i,j;
12   dm = new int * [m];
13   for(i = 0; i < m; i++)
14     if((dm[i] = new int [n]) == NULL)
15      exit(0);
16   for(i = 0; i < m; i++)
17   {
18       for(int j = 0; j < n; j++)
19       {
20         cin>>dm[i][j];
21       }
22   }
23   for(i = 0; i < m; i++)
24   {
25     for(j = 0; j <n; j++)
26       cout<<dm[i][j]<<"	";
27     cout<<endl;
28   }
29   for(i = 0; i < m; i++)
30     delete []dm[i];
31   delete []dm;
32   return 0;
33 }
View Code

11.结构体所占的内存为所有成员变量的大小。

  struct name

{

  long no, birthday;

  char name[22];

  gender sex;

  float score;

};

11.1

  在定义时才能整体赋初值,其余只能单独赋初值,不能 s002.name = "LiGuohua";

  结构体之间能进行运算赋值

11.2

  链表 零碎内存占用

  创建头节点: newp = new student;<创建新节点>

        newp -> next =NULL;

        head = newp;

  1 #include <iostream>
  2 
  3 using namespace std;
  4 
  5 struct student
  6 {
  7   char name[20];
  8   float score;
  9   struct student * next;
 10 };
 11 
 12 typedef student NODE;
 13 
 14 NODE *Searchc(NODE * head, int key)
 15 {
 16   NODE * p;
 17   p = head;
 18   while(p -> next != NULL)
 19   {
 20     if(p -> next -> score < key)
 21       return p;
 22     p = p -> next;
 23   }
 24   return p;
 25 }
 26 
 27 void InsertNode(NODE * p, NODE * newp)
 28 {
 29   newp -> next = p -> next;
 30   p -> next = newp;
 31 }
 32 
 33 void DelNode(NODE * p)
 34 {
 35   NODE * q;
 36   if(p -> next != NULL)
 37   {
 38     q = p -> next;
 39     p -> next = q -> next;
 40     delete q;
 41   }
 42 }
 43 
 44 void DelList(NODE * head)
 45 {
 46   NODE * p;
 47   p = head;
 48   while(head -> next != NULL)
 49   {
 50     head = head -> next;
 51     delete p;
 52     p = head;
 53   }
 54   delete head;
 55 }
 56 
 57 void DispList(NODE * head)
 58 {
 59   NODE * p;
 60   p = head;
 61   while(p -> next != NULL)
 62   {
 63     cout<<p -> next -> name<<"	"<<p -> next -> score<<endl;
 64     p = p -> next;
 65   }
 66 }
 67 
 68 int main()
 69 {
 70   NODE * newp, * head, * p;
 71   char name[20];
 72   float score, low=60;
 73   if((newp = new NODE) == NULL)
 74   {
 75     cout<<"new NODE fail!"<<endl;
 76     exit(0);
 77   }
 78   head = newp;
 79   head -> next = NULL;
 80   cout<<" Input name and score( - 1 to exit): "<<endl;
 81   cin>>name>>score;
 82   while(score > 0)
 83   {
 84     if((newp = new NODE) == NULL)
 85     {
 86       cout<<" new NODE fail!"<<endl;
 87       exit(0);
 88     }
 89     strcpy(newp -> name,name);
 90     newp -> score = score;
 91     newp -> next = NULL;
 92     p = Search(head, score);
 93     InsertNode(p, newp);
 94     cin>>name>>score;
 95   }
 96   cout<<"Before delete: "<<endl;
 97   DispList(head);
 98   for(p = Search(head, low); p -> next != NULL; p = Search(head, low))
 99     DelNode(p);
100   cout<<"After delete:"<<endl;
101   DispList(head);
102   DelList(head);
103   return 0;
104 }
View Code

12.

  共用体类型

活在现实,做在梦里。
原文地址:https://www.cnblogs.com/do-it-best/p/5327399.html