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;
}
相關文章
- python入門(需要C++基礎)PythonC++
- c++基礎十(流程結構)C++
- Android NDK入門:C++ 基礎知識AndroidC++
- BootStrap基礎入門概述總結boot
- MySQL入門--體系結構MySql
- Linux常見目錄結構有哪些?Linux基礎入門Linux
- 【WEB基礎】HTML & CSS 基礎入門(6)超連結WebHTMLCSS
- C++基礎總結C++
- 【重溫基礎】8.字串字串
- HTML 基礎入門HTML
- Maven入門基礎Maven
- Java 入門基礎Java
- Redis入門基礎Redis
- goalng 基礎入門Go
- sqlServer 基礎入門SQLServer
- Ansible 基礎入門
- shell入門基礎
- mongodb基礎入門MongoDB
- MySQL 基礎入門MySql
- RabbitMQ基礎入門MQ
- JavaScript入門基礎JavaScript
- SpringCloud基礎入門SpringGCCloud
- ZooKeeper 基礎入門
- Bootstrap基礎入門boot
- Html基礎入門HTML
- Vim 入門:基礎
- ElasticSearch基礎入門Elasticsearch
- Dart 基礎入門Dart
- Elasticsearch 基礎入門Elasticsearch
- 【FastAPI】入門基礎ASTAPI
- Linux基礎入門Linux
- go基礎入門Go
- Zookeeper基礎入門
- Nginx 基礎入門Nginx
- SQL入門基礎SQL
- Kubernetes 入門基礎
- SQL基礎入門SQL
- Kafka基礎入門Kafka