Skip to content

结构体

1.语法

c++中的结构体和类基本一致,可以定义属性,构造函数,构造函数重载,析构函数,成员函数,成员访问也一样

在创建对象时,可以使用struct关键字,不过也可以省略

c++
#include <iostream>
using namespace std;
// 定义结构体
struct Animal {
    // 定义数据
	int age;
	string name;
	string sex;
    // 构造函数
	Animal() {
		cout << "无参构造函数" << endl;
	}
    // 构造函数重载
	Animal(int age, string name,string sex):age(age),name(name),sex(sex) {
		cout << "无参构造函数" << endl;
	}
	// 成员函数
	void eat() {
		cout << "吃饭" << endl;
	}
    // 析构函数
	~Animal() {
		cout << "析构函数" << endl;
	}
};
int main() {
    // 创建对象
	struct Animal dog;
	Animal cat(2,"咪咪","母");

	Animal* cow = new Animal();

    // 成员访问
	dog.age = 6;
	cat.name = "胖胖";
	cow->sex = "公";

	// 释放空间
	delete cow;
}

2.不同之处

结构体和类的成员默认的访问权限不同,类成员的默认访问权限是private,结构体是public

c++
class Dog {
	int age;
};

struct Cat {
	int age;
};

int main() {
	Dog dog;
	Cat cat;

	dog.age = 5;		// 错误,不可访问
	cat.age = 5;
}

3.基本用法

以下用法在c++ class中也通用

3.1 定义结构体

创建一个结构体,通过实例化来使用

c++
struct Student
{
	string name;
	int age;
};

int main(){
	Student s1;
    s1.name="xiazhi";
    s1.age=18;
    
    // 也可以在创建实例时直接赋值
    Student s2={"暴龙战士", 24};
    
}

创建一个结构体,顺变创建实例,多个实例用逗号隔开,并且也可以直接赋值

c++
struct Student
{
	string name;
	int age;
}s1, s2={"小明", 18}, s3={"小黄", 20};


// 也可以创建结构体数组和指针
struct Person
{
	string name;
	int age;
}p[5], *p2;

3.2 声明结构体变量

声明结构体变量有三种方式

  1. 在定义结构体时声明结构体变量,通过,可以定义多个变量,并且可以在声明时赋值

    c++
    struct Student
    {
    	string name;
    	int age;
    } student,student2={"ikun",18};
  2. 定义结构体之后,在需要用到的地方使用struct 结构体名 结构体变量名声明结构体变量

    c++
    struct Student
    {
    	string name;
    	int age;
    } student,student2=;
    
    int main(){
        struct Student student1;  // 声明变量不赋值
        struct Student student2={};  // 声明变量,初始化数据为空
        struct Student student2={"ikun"};  // 声明变量,不完全赋值
        struct Student student3={"ikun",18};  // 声明变量,完全赋值
        
        // 也可以单独给某一项赋值
       	struct Student s1;
        s1.name="xiazhi";
        s1.age=18;
    }
  3. 直接定义匿名结构体变量(不推荐,若后续还需使用此结构体,无法直接声明)

    c++
    struct {        
        int x;
        int y;
    } point;

默认值问题

C++11 起支持在结构体内部直接为成员变量赋默认值:

c++
struct Config {
    int timeout = 30;  // C++11起支持成员默认值
};

3.3 typedef创建类型别名

通过typedef定义的结构体,可以简化结构体变量的声明

c++
typedef struct {    // 匿名结构体+typedef
    char model[20];
    double price;
} Device;           // Device成为类型别名

Device phone;       // 直接定义变量(无需struct关键字)

3.4 结构体数组

c++
#include<iostream>
using namespace std;

struct Student
{
    string name;
    int age;
};

int main(){
    Student stu[3];   // 创建一个包含三个Student成员的数组
	stu[1].name='小王';  // 结构体成员赋值
    
    // 直接赋值
    Student stu1[3] = {
        {"小明",18},
        {"小黄",19},
        {"小黑",20},
    };
	
    // 访问结构体数组成员
    cout << stu1[1].name << endl;
}

3.5 结构体指针

从结构体指针取值需要使用->操作符

c++
#include<iostream>
using namespace std;

struct Student
{
	string name;
	int age;
};

int main() {
	Student stu = { "小明",18 };
	Student* p = &stu;

	cout << p->name << "," << p -> age << endl;
	return 0;
}

3.6 结构体嵌套

c++
struct Kitchen {
    double area;
};

struct BedRoom {
    double area;
    string bed;
    string Tv;
};

typedef struct Hourse
{
    double money;
    string location;
    double area;
    struct Kitchen  bedroom;
    struct BedRoom bedroom;
}Hourse;

在被嵌套的内容不会被多次使用的情况下,可以直接嵌套,使用typedef关键词方便声明结构体变量

c++
typedef struct Hourse
{
    double money;
    string location;
    double area;

    struct Kitchen {
        double area;
    }kitchen;

    struct BedRoom {
        double area;
        string bed;
        string Tv;
    }bedroom;
}Hourse;

嵌套的结构体需要如下方式声明结构体变量

c++
int main() {
    Hourse hourse = { 500,"北京二环",200,{50},{80,"席梦思","小米电视机"} };
    Hourse hourse = { 600,"上海王家村" };			// 也可不完全赋值
}

3.7 结构体作为函数参数

c++
#include<iostream>
using namespace std;

struct Student
{
	string name;
	int age;
};

// 值传递
void print(Student stu) {
	cout << stu.name << endl;
}

// 地址传递,指针
void print(Student* stu) {
	cout << stu->name << endl;
}


// 地址传递,引用
void print_one(Student& stu) {
	cout << stu.name << endl;
}

int main() {
	Student stu = { "小明",18 };
	print(stu);


	Student stu1 = { "小黑",19 };
	print(&stu1);

	Student stu2 = { "小白",20 };
	print_one(stu2);

	return 0;
}

TIP

当函数传参为地址传递时,函数内部可以修改结构体的值,根据需求,若想限制在函数内不可修改值,可以使用const

c++
#include<iostream>
using namespace std;

struct Student
{
	string name;
	int age;
};

// 地址传递,指针
void print(const Student* stu) {
	cout << stu->name << endl;
}

// 地址传递,引用
void print_one(const Student& stu) {
	cout << stu.name << endl;
}

int main() {
	Student stu1 = { "小黑",19 };
	print(&stu1);

	Student stu2 = { "小白",20 };
	print_one(stu2);

	return 0;
}

MIT Licensed