8-1 struct

1. 自訂型別 struct

在 C++ 中,我們可以把多個彼此相關的資料包在一起,創造出一個全新的型別。

例如,一位學生可能有以下資料:

如果分別用三個變數來表示,資料容易分散,必須想辦法維持追蹤同一個學生的姓名、年齡和成績。

struct 是一種自訂型別,可以把多個彼此相關的資料包在一起,創造出一個全新的型別。

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

這表示我們建立了一個名為 Student 的型別,它裡面有三個成員:nameagescore


2. 為什麼需要 struct

struct 很適合用來表示「一筆完整資料」,例如:

學會 struct 之後,你會開始從「很多零散變數」進步到「有結構的資料設計」,這是寫大型程式的重要基礎。


3. 基本語法

宣告

struct Person {
    std::string name;
    int age;
};

語法重點:


4. 建立變數與存取成員

宣告完 struct 之後,就可以像一般型別一樣建立變數。

Person p1;

使用 . 來存取成員:

p1.name = "Alice";
p1.age = 18;
範例:
#include <iostream>

using namespace std;

struct Person {
    string name;
    int age;
};

int main() {
    Person p1;
    p1.name = "Alice";
    p1.age = 18;

    cout << "Name: " << p1.name << endl;
    cout << "Age: " << p1.age << endl;

    return 0;
}

執行結果:

Name: Alice Age: 18

5. 初始化(給定初值)

除了先宣告再指定值,也可以在建立時直接初始化。

Person p1 = {"Bob", 20};

這相當於:

Person p2 = {"Cindy", 22};
cout << p2.name << ", " << p2.age << endl;

6. struct 用於函數的參數

你可以把整個 struct 傳進函式,讓程式更清楚。

#include <iostream>

using namespace std;

struct Person {
    string name;
    int age;
};

void printPerson(Person p) {
    cout << "Name: " << p.name << endl;
    cout << "Age: " << p.age << endl;
}

int main() {
    Person p = {"David", 25};
    printPerson(p);
  
    return 0;
}

由於函數預設是以 傳值呼叫(call by value) 方式將參數傳入,如果該 struct 裡的資料很大,例如包含學生照片,通常會改用 傳參考呼叫(call by reverence),避免整份資料被複製:

void printPerson(Person& p) {
    cout << "Name: " << p.name << endl;
    cout << "Age: " << p.age << endl;
}

由於傳參考會讓被傳入的外部 struct 變數,可以在函數內被修改。若要確保不被修改,可以像這檥在參數前加上 const 標示其為常數。

void printPerson(const Person& p) {
    cout << "Name: " << p.name << endl;
    cout << "Age: " << p.age << endl;
}

7. 陣列中的 struct

struct 很常和陣列搭配使用,用來儲存多筆同類型資料。

#include <iostream>

using namespace std;

struct Student {
    string name;
    int score;
};

int main() {
    Student students[3] = {
        {"Amy", 90},
        {"Brian", 85},
        {"Chris", 92}
    };

    for (int i = 0; i < 3; i++)
    {
        cout << students[i].name << ": " << students[i].score << endl;
    }

    return 0;
}

這種寫法在成績系統、通訊錄、商品清單中都很常見。


8. 巢狀 struct

struct 裡面還可以再放另一個 struct

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

struct Student {
    string name;
    Date birthday;
};

使用方式:

Student s;
s.name = "Helen";
s.birthday.year = 2005;
s.birthday.month = 8;
s.birthday.day = 15;

這讓複雜資料可以分層整理,結構更清楚。


9. 指標與 struct

如果你有一個指向 struct 的指標,在存取其成員時要用 -> 而不是 .

Person p = {"Ivy", 21};
Person* ptr = &p;

cout << ptr->name << endl;
cout << ptr->age << endl;

比較

p.name.       // 物件存取成員
(*ptr).name   // 物件存取成員
ptr->name     // 指標存取成員

10. 一個完整綜合範例

#include <iostream>

using namespace std;

struct Student {
    string name;
    int age;
    double score;

    void printInfo() {
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
        cout << "Score: " << score << endl;
    }
};

int main() {
    Student s1 = {"Jack", 18, 95.5};
    s1.printInfo();

    return 0;
}

這個例子同時用到了:


11. struct 的使用時機

當你遇到以下情境時,可以考慮使用 struct

例如:


12. 小專案練習:

專案背景

你要做一個小型的收銀系統。系統會:

你會學到什麼

專案檔案

功能需求

  1. 初始化發票
  1. 掃描商品
  1. 列印發票

建議輸入格式

第一行輸入整數 n(要掃描幾筆) 接著輸入 n 行,每行一筆:

範例:

10
Apple 35.5
Banana 12
Milk 45
Bread 30
Apple 35.5
Eggs 68
Coffee 120
Bread 30
Orange 25
Milk 45

建議輸出格式

Date: 2026/4/10
Time: 10:7:56
------------------------------
Items:
- Apple: $35.5 x 2
- Banana: $12 x 1
- Milk: $45 x 2
- Bread: $30 x 2
- Eggs: $68 x 1
- Coffee: $120 x 1
- Orange: $25 x 1
------------------------------
Amount: $446
Tax Rate: 5%
Total Due: $468.3

如何測試

你可以用輸入重導向測試:

main.exe < in.txt

驗收標準

提示

額外挑戰


Revision #7
Created 19 April 2026 06:44:34 by huihui
Updated 20 April 2026 02:13:14 by huihui