Skip to content

L4_03 结构体

一、结构体定义和使用

1.1 定义结构体

cpp
#include <iostream>
#include <string>
using namespace std;

struct Student {
    string name;
    int age;
    float score;
};

1.2 声明结构体变量

cpp
// 方法1:直接声明
Student stu1;

// 方法2:声明并初始化
Student stu2 = {"Alice", 18, 95.5};

// 方法3:使用列表初始化
Student stu3{"Bob", 19, 88.0};

1.3 访问结构体成员

cpp
Student stu = {"Alice", 18, 95.5};

cout << "姓名: " << stu.name << endl;
cout << "年龄: " << stu.age << endl;
cout << "成绩: " << stu.score << endl;

二、结构体数组

2.1 定义结构体数组

cpp
Student students[3] = {
    {"Alice", 18, 95.5},
    {"Bob", 19, 88.0},
    {"Charlie", 20, 92.0}
};

2.2 访问结构体数组元素

cpp
for (int i = 0; i < 3; i++) {
    cout << "学生" << i+1 << ":" << endl;
    cout << "  姓名: " << students[i].name << endl;
    cout << "  年龄: " << students[i].age << endl;
    cout << "  成绩: " << students[i].score << endl;
}

三、结构体指针

3.1 定义结构体指针

cpp
Student stu = {"Alice", 18, 95.5};
Student *p = &stu;

3.2 使用指针访问成员

cpp
// 使用 -> 运算符
cout << "姓名: " << p->name << endl;
cout << "年龄: " << p->age << endl;

// 等价于 (*p).name
cout << "成绩: " << (*p).score << endl;

四、结构体嵌套结构体

4.1 定义嵌套结构体

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

struct Person {
    string name;
    Date birthday;  // 嵌套Date结构体
};

4.2 使用嵌套结构体

cpp
Person p;
p.name = "Alice";
p.birthday.year = 2000;
p.birthday.month = 5;
p.birthday.day = 20;

cout << p.name << "的生日: " 
     << p.birthday.year << "-" 
     << p.birthday.month << "-" 
     << p.birthday.day << endl;

五、结构体做函数参数

5.1 值传递

cpp
void printStudent(Student stu) {
    cout << "姓名: " << stu.name << endl;
    cout << "年龄: " << stu.age << endl;
}

int main() {
    Student stu = {"Alice", 18, 95.5};
    printStudent(stu);  // 值传递,会拷贝整个结构体
    return 0;
}

5.2 引用传递(推荐)

cpp
void updateScore(Student &stu, float newScore) {
    stu.score = newScore;
}

int main() {
    Student stu = {"Alice", 18, 95.5};
    updateScore(stu, 98.0);  // 引用传递,直接修改原对象
    cout << stu.score << endl;  // 输出98.0
    return 0;
}

六、结构体中 const 使用场景

6.1 const 成员函数

cpp
struct Rectangle {
    int width;
    int height;
    
    // const成员函数,不能修改成员变量
    int getArea() const {
        return width * height;
    }
};

6.2 const 结构体参数

cpp
void printRectangle(const Rectangle &r) {
    // 只能读取,不能修改
    cout << "面积: " << r.getArea() << endl;
}

七、示例程序

cpp
#include <iostream>
#include <string>
using namespace std;

struct Book {
    string title;
    string author;
    int pages;
    float price;
};

void printBook(const Book &book) {
    cout << "书名: " << book.title << endl;
    cout << "作者: " << book.author << endl;
    cout << "页数: " << book.pages << endl;
    cout << "价格: " << book.price << endl;
}

int main() {
    Book books[2] = {
        {"C++ Primer", "Lippman", 1000, 99.0},
        {"Python学习手册", "Lutz", 800, 89.0}
    };
    
    for (int i = 0; i < 2; i++) {
        cout << "=== 第" << i+1 << "本书 ===" << endl;
        printBook(books[i]);
        cout << endl;
    }
    
    // 使用指针
    Book *p = &books[0];
    cout << "第一本书的书名: " << p->title << endl;
    
    return 0;
}

百炼成钢,融会贯通