Sixth, structure

6.1 structure concepts

  • Structure (struct): is a series of data having a set of the same type or different types of data thereof.
  • "Structure" is a type of configuration, which is made up of several "members" thereof.
  • Each member may be a basic data type or is a structure type.
  • Structure is a "configuration" data type made, then it must be defined and used in the prior description, it is configured.

Structure 6.2 C ++ language

Declarations and definitions 6.2.1 structure

  • The general form of a statement of the type of structure:

    • struct 结构体名{
          成员列表;
      };
    • eg:

      struct Stu{//Stu是声明的一个结构体类型名
          int score;//整型成员变量
          int math;//整型成员变量
          char name[10];//字符型成员变量
      };
    • struct is a keyword structure, just above declares a structure constructed typeStu

    • And int,floatthe like, Stuis a type name, the structure can define a variable

    • Type declaration just told structure in the form of this type of system, and does not actually allocate memory

    • Note: The declaration and definition of the structure must be based semicolon end.

  • After defining the structure, you can create this type of variables:

    Stu a;//定义一个结构体变量a
    Stu hat;//定义一个结构体变量hat
  • (2) Sample Code

    1. This declaration has declared 3the structure members, respectively, integer a, character b and double precision c, but did not indicate its label declares the structure variabless1

      struct {
          int a;
          char b;
          double c;
      } s1;//s1是结构体变量
    2. This declaration has declared 3the structure members, respectively, to integer a, the character band double precision c, type the name of the structure is named Stu, the other variables are definedt1, t2[20], *t3

      struct Stu{
          int a;
          char b;
          double c;
      };
      Stu t1, t2[20], *t3; 
    3. Can be used typedefto create a new type, this declaration has declared 3the structure members, respectively, to integer a, the character band double precision c, the label structure is named Simple2, used Simple2as a new type of structure variable declarationu1, u2[20], *u3

      typedef struct Simple{//Simple是结构体类型名
          int a;
          char b;
          double c; 
      } Simple2;//注意:Simple2也是结构体类型名,相当于Simple的别名
      Simple2 u1, u2[20], *u3;//若去掉typedef则编译报错,error C2371: “Simple2”: 重定义;不同的基类型
    4. The same type of structure can be assigned directly.

      struct Stu{
          int score;
          int math;
          char name[10];
      };
      Stu a,b={100,50,"Tom"};
      a=b;//正确,a,b是同为Stu类型的结构题变量

6.3 structure of the memory allocation mechanism

  • Structures and other types of data is the same as in the definition of a structure, the system does not actually allocate memory space, only in the definition of variable, it is allocated.
  • Structure has the following requirements for memory allocation member variable:
  1. Structure variable of the first address to be the basic types of members its broadest size divisible ;

  2. Each member of the structure to the first address offset of the structure is an integer multiple of the size of the member, if necessary, the compiler adds padding between the members;

  3. Structure the total size of the structure members of the basic types of the widest size of an integer multiple of , if necessary, the compiler adds padding after the last member of a

  4. E.g:

    • struct Stu {
        char a;
        int b;
        char c;
      };
      1. In Stuthe widest of the data type intin 32bits system 4Byte);
      2. The second reference, a first data char, a store characcount 1 Byte, to ensure that the next intmemory starting address is 4an integral multiple, so the charback of the filling 3 Byte, and then stored in intthe variable b.
      3. The last is a chartype, he accounted for 1 Byte, he certainly is an integer multiple storage locations
      4. Finally, with reference to the third, the total size of the structure is the widest integer multiple types of data, it will be the second charrefill after 3 Byte.
      5. In this case, the space is occupied by a total of 1 + 3 + 4 + 1 + 3 = 12 (red pad bytes)
    • However , the exchange about the sequence structure of data members:

      struct s1 {
        int b;
        char a;
        char c;
      };
      • Likewise Analysis 4 + 1 + 1 + 2 = 8
      • In contrast, the efficiency of the storage space 33%.
      • Note: In the contest phase, in order to save memory , when the data of the data structure organization members may be members of the same type together, thus reducing the compiler to align the fill characters are added.

6.4 Initialization structure

  1. Defined initialization
struct Stu{
    int Num;
    bool Sex;
    char Name[20];
};
//定义时初始化
Stu a={0};//初始化所有成员为0
Stu b={15,1,"Tom"};//依次为成员赋值
  1. Constructor initializes
方法一:
struct Stu{
    int Num;
    bool Sex;
    char Name[20];
    //构造函数初始化
    Stu(){
        Num=0;
        Sex=false;
        memset(Name,0,sizeof(Name));
    }
};
方法二:
struct Stu{
    int Num;
    bool Sex;
    char Name[20];
    //构造函数初始化
    Stu(){
        memset(this->Name,0,sizeof(Name));
    }
};
  1. Access structure member can use the member operators:. ""
struct Stu{
    int Num;
    bool Sex;
    char Name[20];
    //构造函数初始化
    Stu(){
        memset(this,0,sizeof(Name));
    }
};
Stu a;
//访问结构体a的成员函数
a.Num=10;
a.Sex=1;
a.Name="Tom";

6.5 weight carrying structure operator overloading

6.5.1 operator overloading

  • basic concepts
    1. Overload operator is a special name Function
    2. By the keyword \ (operator \) and for heavy-duty operation symbol composed. \ (eg: operator + () \)
    3. Overload operator also includes a return type, and the body of the function parameter list
    4. Overload operator the number of parameters as the number of functions the operators act multiple operands. (Unary operators have a parameter, dibasic two parameters)
    5. If the operator function is a member, it is bound to the first operand implicit \ (the this \) pointer, thus explicit parameter than the actual parameter minority a
    6. Only overload existing operators have no right to invent new operators
    7. The vast majority can override operator. (Can not be overloaded :: , . , . * :? )
    8. Normally, not overloaded comma, to take the address, logical AND or logical operators

6.5.2 Structure weight carrying operator overloading

  • Type structure is a structure, a logical operation can not be performed directly, four operations, etc., if desired, can be overridden for the corresponding operators

    • No. structural body contained less than "<"

    • struct Stu{
          int score;//总分
          int math;//数学
          char Id[maxn];//学号
          //方式一:作为成员函数重载,二元运算符,省略第一个参数,第一个const表示形参为只读,第二个const表示
          //不允许修改成员变量,固定格式。
          bool operator <(const Stu &a)const{
              if(score==a.score){//如果总分相等按数学进行比较
                  //如果总分、数学都相等按学号进行比较
                  if(math==a.math)return (strcmp(Id,a.Id)<0);
                  //如果总分相等,数学不等,数学进行比较
                  return math>a.math;
              }
              //如果总分不等,按总分进行比较
              return score>a.score;
          }
      };
      //方法二:全局重载
      bool operator <(const Stu &a,const Stu &b){
           if(a.score==b.score){//如果总分相等按数学进行比较
               //如果总分、数学都相等按学号进行比较
               if(a.math==b.math)return (strcmp(a.Id,b.Id)<0);
               //如果总分相等,数学不等,数学进行比较
               return a.math>b.math;
           }
              //如果总分不等,按总分进行比较
           return a.score>b.score;
      }
    • Weight structures contained less than sign "+"

      struct Stu{
          int score;//总分
          int math;//数学    
          //方式一:作为成员函数重载,二元运算符,省略第一个参数,第一个const表示形参为只读,第二个const表示
          //不允许修改成员变量,固定格式。tongxue
          Stu operator +(const Stu &a)const{
              Stu temp;//临时结构体变量
              temp.score=this->score+a.score;//this->score等价与score表示改结构题的score成员变量
              temp.math=this->math+a.math;
              rerturn temp;//计算后的结构变量
          }
      };
      //全局重在略……
  • Structure member functions and constructors

    struct Stu{
        int score;//总分
        int math;//数学    
        char Id[maxn];//学号
        //创建结构题时会自动创建一个默认的同名构造函数,构造函数在定义变量时会自动调用
        Stu(){
            memset(this,0,sizeof(Stu));
        }
        //成员函数,需要调用才会执行
        void Init(){
            memset(this,0,sizeof(Stu));
        }
        //成员函数是自己定义的实现某些功能的函数,需要调用才能使用
        void Print(){
            printf("%s\n",Id);
        }
    }a;
    //调用
    a.Init();
    a.Print();
  • The ability of the students can try to reload it this high-precision addition, subtraction, multiplication.

    struct Stu{
      int a[maxn];
      Stu(){
          memset(this,0,sizeof(Stu));
      }
      //重载赋值语句
      Stu operator =(const Stu &b){
          for(int i=0;i<=b.a[0];++i)
              a[i]=b.a[i];
          return *this;
      }
      //重载赋值语句
      Stu operator +(const Stu &b){
          Stu c;
          int i=1;
          while(i<=a[0] || i<=b.a[0]){
              c.a[i]+=a[i]+b.a[i];
              c.a[i+1]=c.a[i]/10;
              c.a[i]%=10;
              ++i;
          }
          if(c.a[i])c.a[0]=i;
          else c.a[0]=i-1;
          return c;
      }
      void read(){
          char s[maxn];
          scanf("%s",s);
          a[0]=strlen(s);
          for(int i=0;i<a[0];++i)
              a[a[0]-i]=s[i]-48;
      }
      void Print(){
          for(int i=a[0];i>0;--i)
              printf("%d",a[i]);
          printf("\n");
      }
    };
    

Guess you like

Origin www.cnblogs.com/hbhszxyb/p/12232125.html