zoukankan      html  css  js  c++  java
  • 结构体

    结构体也就是一种用户可以自定义的数据类型

    例如:

    // 创建学生的数据类型,:学生包括(姓名,年龄,分数)

    struct Student {

    string name;

    int age;

    intscore;

    }

    2、通过学生类型创建具体学生(前提是已经创建了学生这个数据类型)

    2.1 struct Student s1;

     给s1 树形赋值,通过访问结构体变量重的属性

    s1.name = "张三";

    s1.age=18;

    s1.score = 100;

    2.2 struct Student s2={...}//赋值赋初始值

    struct Student s2 = {"李四", 19,80};

    2.3 在定义结构体时顺便创建结构体变量

    在定义的时候

    struct Student {

    string name;

    int age;

    intscore;

    }s3

    s3.name = "王五";

    s3.age = 20;

    s3.score= 60;

    结构体数组

    struct 结构体名称 数组名 [元素个数] = {};

    1、定义结构体

    2、创建结构体数组

    struct  Student stuArray[3]={

    {"张三",15,90},

    {“李四”,17,100},

    {“王五”,20,80}

    }

    3、给结构体赋值

    4、遍历结构体

    for(int i = 0 ;i<3;i++)

    {

    cout<<"姓名:"<<stuArray[i].name <<endl;

    j

    int main 
    结构体变量

    int main(){
    //创建学生结构体变量
      student s ={"张三",18,100};
    //2,通过指针指向结构体变量
    student * p =&s;//s 为自定义学生的数据类型 ,那么在定义指针的时候,需要使用同样数据类型的结构体指针来接收
    //3 通过指针访问结构体变量中的数据 通过->进行访问
    cout<< "姓名" <<p->name<<endl;
    }
    int main 
    结构体嵌套结构体
    struct teacher {
      int id;
    string name;
      int age;
      struct student stu;

    }
    //学生结构体定义
    struct student {
      string
    }
  • 相关阅读:
    趣题:寻找出现了奇数次的数
    zstu2016校赛圣杯战争
    HDU 5183 Negative and Positive (NP) ——(后缀和+手写hash表)
    HDU 5673 Robot ——(卡特兰数)
    HDU 3775 Chain Code ——(Pick定理)
    2016 ICPC北京站现场赛总结(再度流水账)
    2014苏州大学新生赛第二场(12.10)题目解析
    【Jump Game II 】cpp
    【Jump Game】cpp
    【 Sqrt(x) 】cpp
  • 原文地址:https://www.cnblogs.com/gjianli/p/15261375.html
Copyright © 2011-2022 走看看