结构体的相关概念1

1.结构体的声明

//the declaration of a structure
struct Birthday
{
    int year;
    int month;
    int day;
};   //注意结尾的";"必不可少

int main()
{
    Birthday day1;
    day1.year=2002;
    day1.month=5;
    day1.day=17;

    Birthday day2={2003,2,14};

    cout << "Day1--------------------------------" << endl;
    cout << day1.year << " ";   //圆点运算符"."叫成员访问符号
    cout << day1.month << " ";
    cout << day1.day << endl;
    cout << "Day1--------------------------------" << endl;

    cout << "Day2--------------------------------" << endl;
    cout << day2.year << " ";
    cout << day2.month << " ";
    cout << day2.day << endl;
    cout << "Day2--------------------------------" << endl;
}

2.结构体的定义及变量声明

struct time1
{
    int hour;
    int minute;
    int second;
}t1,t2{0,0,0},t3={10,0,0};
//声明了三个变量t1,t2,t3,其中t2、t3通过初始化列表进行了初始化


struct time2
{
    int hour;
    int minute;
    int second;
    //Defaukt Constructor

    //不用C中struct的地方,C++struct允许定义成员函数,默认访问属性和数据成员一样,均为public
    void print1()
    {
        cout << hour << ", " << minute << ", " << second << endl;
    }
    void print2();   //The declaration of a member function
}t21,t22{12,0,0},t23={20,0,0},t24{};
//声明了两个变量t21,t22,t23,t24其中t22、t23,t24通过初始化列表进行了初始化

//在外部定义结构体的成员函数,需要用到结构体类型+全局作用域运算符来限定print2属于结构体类型time2
void time2::print2()
{
    cout << hour << ": " << minute << ": " << second << endl;
}

int main()
{
    t1.hour=19;
    t1.minute=52;
    t1.second=58;
    cout << "t1: " << t1.hour << " " << t1.minute << " " << t1.second << endl;
    cout << "t2: " << t2.hour << " " << t2.minute << " " << t2.second << endl;
    cout << "t3: " << t3.hour << " " << t3.minute << " " << t3.second << endl;

    t21.print1();
    t22.print1();
    t23.print1();
    t24.print1();

    t21.print2();
    t22.print2();
    t23.print2();
    t24.print2();

    cout << endl;
}

3. 使用typedef为结构体声明别名

//使用typedef为结构体声明别名
typedef struct student1
{
      string name;   
	  int number;
	  int age;
	  Birthday birth;
	  struct student1 *pnext;   //自引用指针
}Student1,*Ptrstudent1;

//关于结构体成员 name 的定义
typedef struct student2
{
      char name[20];   //假设名字不超过19个字符,最后一位是"\0"
	  int number;
	  int age;
	  Birthday birth;
	  struct student2 *pnext;
}Student2,*Ptrstudent2;

//定义结构体
typedef struct student3{
      char *name; 
	  int number;
	  int age;
	  Birthday birth;
	  struct student2 *pnext;

	  student3()   //构造函数,结构体变量生成时自动调用
	  {
	      name = new char [20];
	  }

	  ~student3()   //析构函数,结构体变量销毁时自动调用
	  {
	      delete [] name;
	  }

	  void print()
	  {
        cout << "学生姓名" << name << endl;
        cout << "学生年龄" << age << endl;
        cout << "学生学号" << number << endl;
        cout << "学生生日年份" << birth.year << endl;
        cout << "学生生日月份" << birth.month << endl;
        cout << "学生生日日子" << birth.day << endl;
        cout << "\n\n";
	  }
}Student3,*Ptrstudent3;   //类型别名
 

【注意】\0 是一个转义字符,代表空字符(null character)。在C风格字符串(以 char 数组存储的字符串)中,\0 被用作字符串的终止符。 

4.位字段是一种在结构体中定义的特殊成员,它允许你指定成员所占用的位数,而不是整个字节。这在节省内存空间方面非常有用,特别是在处理大量具有相同结构的数据,并且某些数据项只需要较少位数来表示的时候。

//示例
struct test
{
    unsigned int SN : 4;   //无符号整型位字段
    unsigned int : 4;
    bool goodIn : 1;      //布尔型位字段
    bool goodTorgle : 1;
};

int main()
{
    test t;
    t.SN = 5;
    t.goodIn = true;
    t.goodTorgle = false;

    cout << "SN: " << t.SN << endl;
    cout << "goodIn: " << t.goodIn << endl;
    cout << "goodTorgle: " << t.goodTorgle << endl;

    return 0;
}

【注意】位字段必须是整型(有符号或无符号)或枚举类型。

你可能感兴趣的:(conclusion,c++,算法,开发语言)