C++基礎入門---8.結構體【P64~P71】
C++基礎入門---8.結構體【P64~P71】
8. 結構體
結構體屬於使用者自定義的資料型別,允許使用者儲存不同的資料型別
8.1 結構體的定義和使用
注:
1、定義結構體時的關鍵字是struct,不可以省略;
2、建立結構體變數時,關鍵字 struct 可以省略;
3、結構體變數利用操作符 “.” 訪問成員。
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int age;
int score;
}s3;
int main()
{
//通過學生型別建立具體學生
struct student s1;
s1.age = 16;
s1.name = "張三";
s1.score = 60;
cout << "姓名:" << s1.name << " 年齡:" << s1.age << " 分數:" << s1.score << endl;
struct student s2 = { "李四",78,50 };
cout << "姓名:" << s2.name << " 年齡:" << s2.age << " 分數:" << s2.score << endl;
s3.name = "王五";
s3.age = 20;
s3.score = 60;
cout << "姓名:" << s3.name << " 年齡:" << s3.age << " 分數:" << s3.score << endl;
system("pause");
return 0;
}
8.2 結構體陣列
#include<iostream>
#include<string>
using namespace std;
//1、定義結構體
struct student
{
string name;
int age;
int score;
};
int main()
{
//2、建立結構體陣列
struct student stuArray[3]
{
{"張三",18,80},
{"李四", 28, 90},
{"王五", 38, 99}
};
//3、給結構體陣列中的元素賦值
stuArray[2].name = "趙六";
stuArray[2].age = 19;
stuArray[2].score = 85;
//4、遍歷結構體陣列
for (int i = 0; i < 3; i++)
{
cout << "姓名:" <<stuArray[i].name
<< "年齡:" <<stuArray[i].age
<< "分數:" <<stuArray[i].score
<< endl;
}
system("pause");
return 0;
}
8.3 結構體指標
作用:利用指標訪問結構體中的成員。
- 利用操作符 -> 可以通過結構體指標訪問結構體屬性。
# include<iostream>
# include<string>
using namespace std;
//建立結構體變數
struct student
{
string name;
int age;
int score;
};
int main()
{
//建立學生結構體變數
struct student s = { "張三",48,80 };
//通過指標指向結構體變數
struct student *p = &s;
//通過指標訪問結構體變數中的資料
cout << "姓名:" << p->name << "年齡:" << p->age << "成績:" << p->score << endl;
system("pause");
return 0;
}
8.4 結構體巢狀結構體
作用:結構體中的成員可以是另一個結構體。
例如:每個老師輔導一個學員,一個老師的結構體中,記錄一個學生的結構體。
# include<iostream>
# include<string>
using namespace std;
//定義結構體
struct student
{
string name;
int age;
int score;
};
struct teacher
{
int id;
string name;
int age;
struct student stu;
};
int main()
{
//建立老師結構體變數
teacher t;
t.id = 10000;
t.name = "老王";
t.age = 50;
t.stu.age = 18;
t.stu.name="小李";
t.stu.score = 90;
cout << "老師姓名: " << t.name << "老師id: " << t.id
<< "老師年齡:" << t.age << "學生年齡:" << t.stu.age
<< "學生姓名:" << t.stu.name
<< "學生成績:" << t.stu.score << endl;
system("pause");
return 0;
}
8.5 結構體作函式引數
# include<iostream>
# include<string>
using namespace std;
//定義結構體
struct student
{
string name;
int age;
int score;
};
//1、值傳遞
void printstudent(struct student s)
{
cout << "姓名:" << s.name << "年齡:" << s.age << "分數:" << s.score << endl;
}
//2、地址傳遞
void printstudent2(struct student *p)
{
cout << "姓名:" << p->name << "年齡:" << p->age << "分數:" << p->score << endl;
}
int main()
{
//建立老師結構體變數
struct student s;
s.name = "張三";
s.age = 20;
s.score = 60;
printstudent(s);
printstudent2(&s);
system("pause");
return 0;
}
8.6 結構體中const使用場景
# include<iostream>
# include<string>
using namespace std;
//定義結構體
struct student
{
string name;
int age;
int score;
};
//將函式中的形參改為指標,可以節省記憶體空間,而且不會賦值新的副本出來
void printstudent( const struct student *s)
{
cout << "姓名:" << s->name << "年齡:" << s->age << "分數:" << s->score << endl;
}
int main()
{
//建立結構體變數
struct student s;
s.name = "張三";
s.age = 20;
s.score = 60;
printstudent(&s);
system("pause");
return 0;
}
8.7 結構體案例1
# include<iostream>
# include<string>
# include<ctime>
using namespace std;
//定義結構體
struct student
{
string name;
int score;
};
struct teacher
{
string name;
struct student sArray[5];
};
void allocateSpace(struct teacher tArray[] ,int len)
{
string nameseed = "ABCDE";
for (int i = 0; i < len; i++)
{
tArray[i].name = "Teacher_";
tArray[i].name += nameseed[i];
for (int j = 0; j < 5; j++)
{
tArray[i].sArray[j].name = "Student_";
tArray[i].sArray[j].name += nameseed[j];
int random = rand() % 61 + 40;//產生40~100之間的隨機數
tArray[i].sArray[j].score = random;
}
}
}
void printInfo(struct teacher tArray[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "老師姓名:" << tArray[i].name << endl;
for (int j = 0; j < 5; j++)
{
cout << "\t學生姓名: " << tArray[i].sArray[j].name;
cout << " 考試分數:" << tArray[i].sArray[j].score << endl;
}
}
}
int main()
{
//隨機數種子
srand((unsigned int)time(NULL));
//1、建立3名老師的陣列
struct teacher tArray[3];
//2、通過函式給3名老師的資訊賦值,並給老師帶的學生資訊賦值
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
//3、列印所有老師及所帶學生的資訊
printInfo(tArray,len);
system("pause");
return 0;
}
8.8 結構體案例2
# include<iostream>
# include<string>
using namespace std;
//1、定義英雄結構體
struct hero
{
string name;
int age;
string sex;
};
void bubbleSort(struct hero array[], int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (array[j].age > array[j + 1].age)
{
string temp = array[j + 1].name;
array[j + 1].name = array[j].name;
array[j].name = temp;
}
}
}
}
void printhero(struct hero array[], int len)
{
for (int i = 0; i < len; i++)
{
cout << "英雄姓名:" << array[i].name
<< " 英雄年齡:" << array[i].age
<< " 英雄性別:" << array[i].sex << endl;
}
}
int main()
{
//2、建立英雄結構體陣列存放5名英雄
struct hero array[5]
{
{"劉備",23,"男"},
{"關羽",22,"男"},
{"張飛",20,"男"},
{"趙雲",21,"男"},
{"貂蟬",19,"女"},
};
int len = sizeof(array) / sizeof(array[0]);
/*for (int i = 0; i < len; i++)
{
cout << "英雄姓名:" << array[i].name
<< " 英雄年齡:" << array[i].age
<< " 英雄性別:" << array[i].sex << endl;
}*/
//3、對陣列進行排序,按照年齡進行升序排序
bubbleSort(array, len);
//4、將排序結果列印輸出
printhero(array, len);
system("pause");
return 0;
}
相關文章
- 【C++基礎複習01】結構體和連結串列C++結構體
- c++基礎十(流程結構)C++
- python入門(需要C++基礎)PythonC++
- BootStrap基礎入門概述總結boot
- MySQL入門--體系結構MySql
- Android NDK入門:C++ 基礎知識AndroidC++
- 【WEB基礎】HTML & CSS 基礎入門(6)超連結WebHTMLCSS
- 【重溫基礎】8.字串字串
- C++基礎總結C++
- Linux常見目錄結構有哪些?Linux基礎入門Linux
- RabbitMQ基礎入門MQ
- mongodb基礎入門MongoDB
- MySQL 基礎入門MySql
- ZooKeeper 基礎入門
- Elasticsearch 基礎入門Elasticsearch
- Vim 入門:基礎
- Bootstrap基礎入門boot
- Html基礎入門HTML
- ElasticSearch基礎入門Elasticsearch
- HTML 基礎入門HTML
- Dart 基礎入門Dart
- SQL入門基礎SQL
- Nginx 基礎入門Nginx
- Kafka基礎入門Kafka
- Redis入門基礎Redis
- Java 入門基礎Java
- Maven入門基礎Maven
- JavaScript入門基礎JavaScript
- SQL基礎入門SQL
- Zookeeper基礎入門
- goalng 基礎入門Go
- TypeScript 基礎入門TypeScript
- Mongoose基礎入門Go
- systemtap基礎入門
- sqlServer 基礎入門SQLServer
- 【FastAPI】入門基礎ASTAPI
- shell入門基礎
- go基礎入門Go