C++学习路程 22/3/6 AM 8:20

#include <iostream>
using namespace std;
//函数分文件编写,把函数分开放到一个文件里
// 1.创建 .h后缀的头文件    放入函数声明
// 2.创建 .cpp的源文件      放入函数定义
// 3.#include <iostream>  
//  using namespace std;
// 4.源文件前加入  #include "函数名.h"
// 5. main 应用函数时,前面加入 #include "函数名.h"

//  指针 就是保存变量的地址
int main()
{
    int a = 10;
    //定义指针  数据类型*指针名
    int* p;
    //让指针定义变量地址
    p = &a;
    cout << "a的地址" << &a << endl;
    cout << "指针p是" << p << endl;
    // 利用指针 解引用 ,找到指针指向的内存
    // 解引用 为指针前加*,找到指针指向的内存,从而改变
    *p = 10000;//修改访问 指针p所代表地址的内容
    cout << "a=" << a << endl;
    cout << "*p=" << *p << endl;


    //空指针
    int* p = NULL;
    //空指针不能访问修改, 地址0—255都不可以
    //野指针
    int* p = (int*)10000;
    //自己没有申请的空间,不可以访问

    //const 修饰指针
    int a = 10;
    int b = 20;
    const int* p = &a;
   //常量指针,指向可以改,指向的内容不能改
   // //p所代表地址可以改,*p的内容不能改
   // * p = 20;//错误
    p = &b;

    int const* p = &a;
    //指针常量,指向不可以改,指向的内容能改
    //p所代表地址不可以改,*p的内容能改

    /常量指针 定常量,指针常量 定指针/


    //指针 数组
     int arr[] = {1,2,3,4,5,6};
    // int* p = &arr[0];
     int* p = arr;//数组首地址
     *p = 9999;
     cout << arr[0] << endl;
     p++;//指针直接向后移到后面一个字节
     cout << *p << endl;

     int* p1 = arr;
     for (int i = 0; i < 6; i++)
     {
         cout << *p1 << endl;
         p1++;
     }
}





    //指针 函数   地址传递改实参;值传递不行
    #include <iostream>
    using namespace std;
    void swap01(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
    cout << a << endl;
    cout << b << endl;
}
//地址传递
void swap02(int *p1, int *p2)
{
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
    cout << *p1 << endl;
    cout << *p2 << endl;
}
//  指针,保存变量的地址
  int main()
{

         //指针 函数
      int a = 1;
      int b = 2;
      swap01(a, b);//形参改变
      cout << a << endl;//实参不改变
      cout << b << endl;
      cout << ".............." << endl;
      swap02(&a, &b);
      cout << a << endl;//实参改变
      cout << b << endl;
      值传递 实参不变;地址传递  一步到位直击地址的内容
}


    //指针 数组 函数
    #include <iostream>
using namespace std;
void bubbleSort(int *arr,int L)///数组作为输入时,是输入数组的首地址
{
    for (int i = 0; i < L - 1; i++)
    {
        for (int j = 0; j < L - i-1; j++)
        {
            if (arr[j] >= arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < L; i++)
    {
        cout << arr[i] << endl;
    }
}
int main()
{// 指针 数组  函数
      int arr[] = {1,5,236,4,8,28,4,5};
      int L = sizeof(arr) / sizeof(arr[0]);
      bubbleSort( arr,  L);
}   


/结构体
#include <iostream>
using namespace std;
#include <string>
struct Student
{
    string name;
    int age;
    int score;
};注意分号
int main()
{
///结构体 : 自己定义数据类型
        //定义结构体 方式一
      struct Student S1;//创建变量时 struct 可以省略
      S1.name = "张三";
      S1.score = 100;
      S1.age = 10;
      cout << S1.name << S1.score << S1.age << endl; 
       //定义结构体 方式二
      struct Student S2 = {"李四",17,200};
      cout << S2.name << S2.score << S2.age << endl; 
}   



#include <iostream>
using namespace std;
#include <string>
结构体数组
struct Student
{
    string name;
    int age;
    int score;
};
int main()
{
结构体数组
      //创建数组
      struct Student stuarr[3]
      {
          {"你",19,100},
           {"你",19,100},
            {"你",19,100}
      };
      stuarr[0].age = 1000;
      cout << stuarr[0].age << endl;
}   


//结构体指针
#include <iostream>
using namespace std;
#include <string>
struct Student
{
    string name;
    int age;
    int score;
};
int main()
{//结构体指针
      struct Student S={ "蝙蝠侠", 30,10000 };
      struct Student* p = &S;  ///S的地址
      cout << "名字" << p->name << "年龄"<<(*p).age<<endl;/// p -> 成员  (*p).成员
}   



/结构体嵌套结构体
#include <iostream>
using namespace std;
#include <string>
struct student // 内部的结构体先写在前面
{
    string name;
    int age;
    int score;
};
struct teacher
{
    int id;
    string name;
    int age;
    struct student stu;
};
int main()
{
teacher t;
t.id = 1000;
t.name = "老张";
t.age = 50;
t.stu.age = 18;
t.stu.name = "小张";
t.stu.score = 100;
}   



///结构体做函数参数
#include <iostream>
using namespace std;
#include <string>
struct student
{
    int id;
    string name;
    int age;
    int score;
};
void printf(struct student S)
{
    cout << S.age << S.id << S.name << S.score << endl;
}
void printf1(struct student *p)
{
    cout << p->age << p->id << p->name << p->score << endl;
}
  int main()
{
 ///结构体做函数参数
student S;
S.age = 15;
S.id = 1111111;
S.name = "ABCD";
S.score = 100;
printf( S);///应用结构体时只写名字,不需要数据类型
printf1(&S);  }


//结构体案例
#include <iostream>
using namespace std;
#include <string>
#include <ctime>
struct student
{
    string name;
    int score;
};
struct teacher
{
    string name;
    struct student arr[5];
};
void inputT(struct teacher arr[], int L)
{
    string nameT = "ABCDE";
    for (int i = 0; i < L; i++)
    {
        arr[i].name = "Teacher_";
        arr[i].name += nameT[i];/ X+=Y >>>>X=X+Y 
        for (int j = 0; j < 5; j++)
        {
            arr[i].arr[j].name = nameT[j];
            int random = rand() % 100;
            arr[i].arr[j].score = random;
        }       
    }
}
void printT(struct teacher arr[], int L)
{
    for (int i = 0; i < L; i++)
    {
        cout << "Tea_name:" << arr[i].name<<endl;
        for (int j = 0; j < 5; j++)
        {
            cout << "Stu_name:" << arr[i].arr[j].name<<endl;
            cout << "Stu_score:" << arr[i].arr[j].score << endl;
        }
    }

}
 int main()
{
srand((unsigned int)time(NULL));
struct teacher ARR[3];
int L = sizeof(ARR) / sizeof(ARR[0]);
inputT( ARR,L);
printT(ARR, L);}



#include <iostream>
using namespace std;
#include <string>
/英雄 排序
struct hero
{
    string name;
    int age;
    string sex;
};
void bubble(struct hero arr[], int L)
{
    for (int i = 0; i < L-1; i++)//排序轮数
    {
        for (int j = 0; j < L - i - 1; j++)//每轮的次数
        {
            if (arr[j].age > arr[j + 1].age)
            {
                hero temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < L; i++)
    {
        cout << "name" << arr[i].name << "年龄" << arr[i].age << "性别" << arr[i].sex << endl;
    }
}
//void print(struct hero arr[], int L)
//{
//    for (int i = 0; i < L; i++)
//    {
//        cout << "name" << arr[i].name << "年龄" << arr[i].age << "性别" << arr[i].sex << endl;
//    }
//}

int main{

struct hero arr1[5] = {{"LB",23,"MAN"},{"GY",22,"MAN"},{"ZF",20,"MAN"},{"ZY",21,"MAN"},{"DC",19,"WOMEN"}};
int L = sizeof(arr1) / sizeof(arr1[0]);
hero * p = &arr1[5];
bubble(arr1, L);
//print(arr1, L);
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值