c++回顾(部分)

int score;
    switch (score)  //switch相对于if执行效率更高,缺点是不能设置区间范围
    {
    case 1:cout << "还行" << endl; break;//break跳出循环,continue跳出循环并根据条件决定是否继续循环
    case 2:cout << "良好" << endl; break;//没有break则各个case依次执行
    case 3:cout << "优秀" << endl; break;
    default:
        break;
    }
    //经典猜数
    srand((unsigned int)time(NULL));//设置系统时间为随机数种子,否则默认为1,伪随机数输出不变
    int num = rand() % 100 + 1;     //产生随机数1-100
    int guess = 0;
    while (1)                       //判断
    {
        cin >> guess;
        if (num > guess)
            cout << "猜小了。" << endl; 
        if (num < guess)
            cout << "猜大了。" << endl; 
        if (num == guess)
        {
            cout << "恭喜您猜对了。" << endl;
            break;
        }
    }
//经典水仙花数
    int num = 100;
    while (num < 1000)
    {
        int a=num%10;//个位
        int b=num/10%10;//十位
        int c=num/100;//百位
        if ((a*a*a + b*b*b + c*c*c) == num)//判断是否是水仙花数
        {
            cout << num<<endl;
        }
        num++;
    }

 指针占用内存大小:

32位:4字节

64位:8字节

  int a=1;

    //记忆技巧:const后面是什么,什么就不可以修改;
    int* const p = &a;//指针常量 指向不可修改;
    const int* p1 = &a;// 常量指针 指针指向不可修改;
    const int* const p2 = &a;//const修饰指针和常量,都不可以修改;
//使用指针遍历数组
    int array[] = { 1,2,3,
                    4,5,6,
                    7,8,9};
    int* p = array;//注:数组不加&;因为数组名本身就是地址
    for (int i = 0; i < 9; i++)
    {
        cout << *p << endl;
        p++;
    }

 

//地址传递;修改函数外变量需要用地址传递,值传递不改变外部变量
void swap(int* p, int* p1)
{
    int temp = *p;
    *p = *p1;
    *p1 = temp;
}
int main()
{
    int a = 233;
    int b = 666;
    swap(&a, &b);
    cout << a << endl << b;
    return 0;
}
//结构体数组的使用(和普通数组基本一样)
    struct Student {
        string name;//姓名
        int age;//年龄
        int score;//分数
    };
     Student c1[3]//创建结构体数组
    {
        {"张三",18,22},
        {"李四",22,55},
        {"王五",22,56}
    };
    for (int i = 0; i < 3; i++)//遍历输出结构体数组
    {
        cout << c1[i].name << " " << c1[i].age << " " << c1[i].score << endl;
    }
//结构体指针的用法
    struct Student {
        string name;//姓名
        int age;//年龄
        int score;//分数
    };
    Student c1 = { "张三",22,44 };
    Student* p = &c1;
    cout << p->name;//通过指针访问结构体成员用-> ; 通过结构体名访问成员用 . ;
//结构体嵌套
    struct Student { 
        string name;//姓名
        int age;//年龄
        int score;//分数
    };
    struct Teacher {
        string name;//教师姓名
        int id;//教师编号
        int age;//教师年龄
        Student stu;//教师辅导的学生
    };
    Teacher tea{ "桑西河", 10086,50, {"小桑",22,89} };
    cout << "教师的姓名是\t\t\t" << tea.name << endl
        <<"教师的编号是\t\t\t"<<tea.id << endl
        <<"教师的年龄是\t\t\t"<<tea.age << endl
        <<"教师辅导的学生是\t\t"<<tea.stu.name << endl
        << "教师辅导的学生年龄为\t\t" << tea.stu.age << endl
        << "教师辅导的学生分数是\t\t"<< tea.stu.score << endl;

 

//栈区用来存放局部变量;使用结束之后编译器自动释放,不要return 栈区地址(比如在值传递函数中,不要return形参地址)

 

//关于在堆区使用new创建数组
int
* new_1() { //在堆中定义数组并赋值打印 int* array = new int[10]; for (int i = 0; i < 10; i++) { //随机赋值 array[i] = rand() % 233; //输出数组元素 cout << array[i]<<endl; } //返回数组首地址 return array; } int main() { //设置随机数种子 srand((unsigned int)time(NULL)); //接收堆区数组地址 int*arr = new_1(); //释放堆区定义的数组 delete [] arr; return 0; }

 

//函数声明和函数定义中,只能有一个有默认参数
//函数重载可通过参数类型不同,参数顺序不同来重载

 

//引用的本质是指针常量
//class中自定义有参构造函数 c++将不再提供无参构造函数
//class中自定义拷贝构造函数“类名(const 类名 &p) ”,C++编译器将不再提供有参和无参构造函数

//浅拷贝 简单赋值(即编译器默认拷贝函数)
//深拷贝 重新在堆区开辟空间,常用语成员值有指针的情况(在析构函数处释放空间如果用浅拷贝会释放两次从而导致程序崩溃)

 

//类对象作为成员函数时,调用类内对象构造函数->调用该类构造函数->调用该类析构->类内对象析构函数

 

//类内常函数

mutable int a;//mutable 的变量,在常函数中也可以改

void 函数名() const
{
    
}
//常对象只能调用常函数

 

posted @ 2021-12-31 12:15  DNW12580  阅读(58)  评论(0)    收藏  举报