Skip to content

C++ 结构体(struct)

结构体(struct)是用户自定义的数据类型,用于组合不同类型的数据。结构体是 C++ 中重要的数据结构之一。

1. 结构体的定义

1.1 基本语法

cpp
struct 结构体名 {
    成员变量;
    成员函数;
};

示例

cpp
#include <iostream>

struct Student {
    std::string name;
    int age;
    float gpa;
};

int main() {
    Student student;
    student.name = "张三";
    student.age = 20;
    student.gpa = 3.8f;
    
    std::cout << "姓名: " << student.name << std::endl;
    std::cout << "年龄: " << student.age << std::endl;
    std::cout << "GPA: " << student.gpa << std::endl;
    
    return 0;
}

1.2 初始化结构体

cpp
#include <iostream>

struct Student {
    std::string name;
    int age;
    float gpa;
};

int main() {
    // 方式1:逐个初始化
    Student student1;
    student1.name = "张三";
    student1.age = 20;
    student1.gpa = 3.8f;
    
    // 方式2:使用初始化列表
    Student student2 = {"李四", 21, 3.5f};
    
    // 方式3:使用指定初始化(C++11)
    Student student3 = {.name = "王五", .age = 22, .gpa = 3.9f};
    
    std::cout << student1.name << std::endl;
    std::cout << student2.name << std::endl;
    std::cout << student3.name << std::endl;
    
    return 0;
}

2. 结构体成员

2.1 访问结构体成员

cpp
#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point point;
    point.x = 10;
    point.y = 20;
    
    std::cout << "x = " << point.x << std::endl;
    std::cout << "y = " << point.y << std::endl;
    
    return 0;
}

2.2 结构体指针

cpp
#include <iostream>

struct Point {
    int x;
    int y;
};

int main() {
    Point point = {10, 20};
    Point* ptr = &point;
    
    // 使用 -> 运算符访问成员
    std::cout << "x = " << ptr->x << std::endl;
    std::cout << "y = " << ptr->y << std::endl;
    
    return 0;
}

3. 结构体和函数

3.1 结构体作为函数参数

cpp
#include <iostream>

struct Student {
    std::string name;
    int age;
    float gpa;
};

void print_student(Student student) {
    std::cout << "姓名: " << student.name << std::endl;
    std::cout << "年龄: " << student.age << std::endl;
    std::cout << "GPA: " << student.gpa << std::endl;
}

int main() {
    Student student = {"张三", 20, 3.8f};
    print_student(student);
    
    return 0;
}

3.2 结构体引用作为函数参数

cpp
#include <iostream>

struct Student {
    std::string name;
    int age;
    float gpa;
};

void print_student(const Student& student) {
    std::cout << "姓名: " << student.name << std::endl;
    std::cout << "年龄: " << student.age << std::endl;
    std::cout << "GPA: " << student.gpa << std::endl;
}

void update_age(Student& student, int new_age) {
    student.age = new_age;
}

int main() {
    Student student = {"张三", 20, 3.8f};
    print_student(student);
    
    update_age(student, 21);
    print_student(student);
    
    return 0;
}

3.3 函数返回结构体

cpp
#include <iostream>

struct Point {
    int x;
    int y;
};

Point create_point(int x, int y) {
    Point point;
    point.x = x;
    point.y = y;
    return point;
}

int main() {
    Point point = create_point(10, 20);
    std::cout << "x = " << point.x << std::endl;
    std::cout << "y = " << point.y << std::endl;
    
    return 0;
}

4. 结构体数组

4.1 声明和初始化结构体数组

cpp
#include <iostream>

struct Student {
    std::string name;
    int age;
    float gpa;
};

int main() {
    Student students[3] = {
        {"张三", 20, 3.8f},
        {"李四", 21, 3.5f},
        {"王五", 22, 3.9f}
    };
    
    for (int i = 0; i < 3; i++) {
        std::cout << "姓名: " << students[i].name << std::endl;
        std::cout << "年龄: " << students[i].age << std::endl;
        std::cout << "GPA: " << students[i].gpa << std::endl;
        std::cout << std::endl;
    }
    
    return 0;
}

4.2 遍历结构体数组

cpp
#include <iostream>

struct Student {
    std::string name;
    int age;
    float gpa;
};

int main() {
    Student students[3] = {
        {"张三", 20, 3.8f},
        {"李四", 21, 3.5f},
        {"王五", 22, 3.9f}
    };
    
    for (int i = 0; i < 3; i++) {
        std::cout << students[i].name << " ";
    }
    std::cout << std::endl;
    
    return 0;
}

5. 结构体嵌套

5.1 结构体中包含结构体

cpp
#include <iostream>

struct Date {
    int day;
    int month;
    int year;
};

struct Student {
    std::string name;
    int age;
    Date birthday;
};

int main() {
    Student student;
    student.name = "张三";
    student.age = 20;
    student.birthday.day = 1;
    student.birthday.month = 1;
    student.birthday.year = 2004;
    
    std::cout << "姓名: " << student.name << std::endl;
    std::cout << "年龄: " << student.age << std::endl;
    std::cout << "生日: " << student.birthday.year << "-" << student.birthday.month << "-" << student.birthday.day << std::endl;
    
    return 0;
}

6. 结构体和类

在 C++ 中,结构体和类非常相似,主要区别在于默认的访问权限:

  • 结构体:默认访问权限为public
  • 类:默认访问权限为private

6.1 结构体示例

cpp
#include <iostream>

struct Point {
    int x;
    int y;
    
    void print() {
        std::cout << "x = " << x << ", y = " << y << std::endl;
    }
};

int main() {
    Point point;
    point.x = 10;
    point.y = 20;
    point.print();
    
    return 0;
}

6.2 类示例

cpp
#include <iostream>

class Point {
private:
    int x;
    int y;

public:
    void set_x(int x) {
        this->x = x;
    }
    
    void set_y(int y) {
        this->y = y;
    }
    
    void print() {
        std::cout << "x = " << x << ", y = " << y << std::endl;
    }
};

int main() {
    Point point;
    point.set_x(10);
    point.set_y(20);
    point.print();
    
    return 0;
}

7. 结构体的应用

7.1 表示复数

cpp
#include <iostream>

struct Complex {
    double real;
    double imag;
    
    Complex add(Complex other) {
        Complex result;
        result.real = real + other.real;
        result.imag = imag + other.imag;
        return result;
    }
    
    void print() {
        std::cout << real << " + " << imag << "i" << std::endl;
    }
};

int main() {
    Complex c1 = {1.0, 2.0};
    Complex c2 = {3.0, 4.0};
    Complex c3 = c1.add(c2);
    
    c3.print();
    
    return 0;
}

7.2 表示矩形

cpp
#include <iostream>

struct Rectangle {
    double width;
    double height;
    
    double area() {
        return width * height;
    }
    
    double perimeter() {
        return 2 * (width + height);
    }
};

int main() {
    Rectangle rect = {5.0, 3.0};
    
    std::cout << "面积: " << rect.area() << std::endl;
    std::cout << "周长: " << rect.perimeter() << std::endl;
    
    return 0;
}

8. 示例:综合运用

现在,让我们看一个综合运用各种结构体特性的例子:

cpp
#include <iostream>
#include <string>

// 定义日期结构体
struct Date {
    int day;
    int month;
    int year;
    
    void print() {
        std::cout << year << "-" << month << "-" << day << std::endl;
    }
};

// 定义学生结构体
struct Student {
    std::string name;
    int age;
    float gpa;
    Date birthday;
    
    void print() {
        std::cout << "姓名: " << name << std::endl;
        std::cout << "年龄: " << age << std::endl;
        std::cout << "GPA: " << gpa << std::endl;
        std::cout << "生日: ";
        birthday.print();
    }
};

// 函数声明
void print_student(const Student& student);
void update_age(Student& student, int new_age);
Student create_student(std::string name, int age, float gpa, Date birthday);

int main() {
    // 创建学生
    Date birthday = {1, 1, 2004};
    Student student = create_student("张三", 20, 3.8f, birthday);
    
    // 打印学生信息
    std::cout << "学生信息:" << std::endl;
    print_student(student);
    std::cout << std::endl;
    
    // 更新年龄
    update_age(student, 21);
    std::cout << "更新后的学生信息:" << std::endl;
    print_student(student);
    std::cout << std::endl;
    
    // 结构体数组
    std::cout << "学生数组:" << std::endl;
    Student students[3] = {
        {"张三", 20, 3.8f, {1, 1, 2004}},
        {"李四", 21, 3.5f, {2, 2, 2003}},
        {"王五", 22, 3.9f, {3, 3, 2002}}
    };
    
    for (int i = 0; i < 3; i++) {
        students[i].print();
        std::cout << std::endl;
    }
    
    return 0;
}

void print_student(const Student& student) {
    std::cout << "姓名: " << student.name << std::endl;
    std::cout << "年龄: " << student.age << std::endl;
    std::cout << "GPA: " << student.gpa << std::endl;
    std::cout << "生日: ";
    student.birthday.print();
}

void update_age(Student& student, int new_age) {
    student.age = new_age;
}

Student create_student(std::string name, int age, float gpa, Date birthday) {
    Student student;
    student.name = name;
    student.age = age;
    student.gpa = gpa;
    student.birthday = birthday;
    return student;
}

小结

C++ 结构体包括:

  1. 结构体的定义

    • 基本语法:struct 结构体名 { 成员变量; 成员函数; };
    • 初始化结构体:逐个初始化、使用初始化列表、使用指定初始化(C++11)
  2. 结构体成员

    • 访问结构体成员:使用.运算符
    • 结构体指针:使用->运算符
  3. 结构体和函数

    • 结构体作为函数参数
    • 结构体引用作为函数参数
    • 函数返回结构体
  4. 结构体数组

    • 声明和初始化结构体数组
    • 遍历结构体数组
  5. 结构体嵌套

    • 结构体中包含结构体
  6. 结构体和类

    • 结构体:默认访问权限为public
    • 类:默认访问权限为private
  7. 结构体的应用

    • 表示复数
    • 表示矩形
    • 表示其他复杂的数据结构

关键概念:

  • 结构体:用户自定义的数据类型
  • 成员变量:结构体中的数据成员
  • 成员函数:结构体中的函数成员
  • 结构体数组:包含多个结构体的数组
  • 结构体嵌套:结构体中包含其他结构体

掌握结构体是编写 C++ 程序的基础,在后续章节中,我们将学习 C++ 的 vector 容器。