struct筆記
二月 14, 2025
- struct可以用來實作linked list:
1
2
3
4
5struct {
int c, h; // center and height
int pre, next; // linked list
int alive;
} tree[N]; - struct基本語法ex:
1
2
3
4
5struct 結構名稱 {
成員資料型態 成員變數1;
成員資料型態 成員變數2;
...
};1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using namespace std;
struct Student {
string name;
int age;
double score;
};
int main() {
Student s1;
s1.name = "小明";
s1.age = 18;
s1.score = 95.5;
cout << "姓名: " << s1.name << endl;
cout << "年齡: " << s1.age << endl;
cout << "分數: " << s1.score << endl;
return 0;
}
查看评论