日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

【C++】【一】结构体数组

發(fā)布時間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C++】【一】结构体数组 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?

demo7:函數(shù)份文件編寫

swap.h

#include <iostream>
using namespace std;//函數(shù)的聲明
void swap(int a, int b);

swap.cpp

#include "swap.h"//函數(shù)的定義
void swap(int a, int b)
{int temp = a;a = b;b = temp;cout << "a = " << a << endl;cout << "b = " << b << endl;
}

?main函數(shù)

#include<iostream>
using namespace std;
#include "swap.h"//函數(shù)的分文件編寫
//實現(xiàn)兩個數(shù)字進行交換的函數(shù)函數(shù)的聲明
//void swap(int a, int b);
函數(shù)的定義
//void swap(int a, int b)
//{
//	int temp = a;
//	a = b; 
//	b = temp;
//
//	cout << "a = " << a << endl;
//	cout << "b = " << b << endl;
//}//1、創(chuàng)建.h后綴名的頭文件
//2、創(chuàng)建.cpp后綴名的源文件
//3、在頭文件中寫函數(shù)的聲明
//4、在源文件中寫函數(shù)的定義int main() {int a = 10;int b = 20;swap(a, b);system("pause");return 0;
}

demo6:形參不改變?,當(dāng)我們做值傳遞的時候,函數(shù)的形參發(fā)生改變,并不會影響實參

#include<iostream>
using namespace std;//值傳遞
//定義函數(shù),實現(xiàn)兩個數(shù)字進行交換函數(shù)//如果函數(shù)不需要返回值,聲明的時候可以寫void
void swap3(int num1, int num2)
{cout << "交換前: " << endl;cout << "num1 = " << num1 << endl;cout << "num2 = " << num2 << endl;int temp = num1;num1 = num2;num2 = temp;cout << "交換后: " << endl;cout << "num1 = " << num1 << endl;cout << "num2 = " << num2 << endl;//return; 返回值不需要的時候,可以不寫return
}int main() {int a = 10;int b = 20;cout << "a = " << a << endl;cout << "b = " << b << endl;//當(dāng)我們做值傳遞的時候,函數(shù)的形參發(fā)生改變,并不會影響實參swap3(a, b);cout << "a = " << a << endl;cout << "b = " << b << endl;system("pause");return 0;
}

demo5:結(jié)構(gòu)體中const:?將函數(shù)中的形參改為指針,可以減少內(nèi)存空間,而且不會復(fù)制新的副本出來

#include<iostream>
using namespace std;
#include <string>//const的使用場景struct student
{//姓名string name;//年齡int age;//分?jǐn)?shù)int score;
};//將函數(shù)中的形參改為指針,可以減少內(nèi)存空間,而且不會復(fù)制新的副本出來
void printStudents(const student *s)
{//s->age = 150; //加入const之后,一旦有修改的操作就會報錯,可以防止我們的誤操作cout << "姓名: " << s->name << " 年齡: " << s->age << " 得分: " << s->score << endl;
}int main() {//創(chuàng)建結(jié)構(gòu)體變量struct student s = { "張三" , 15 , 70 };//通過函數(shù)打印結(jié)構(gòu)體變量信息printStudents(&s);cout << "main中張三年齡為: " << s.age << endl;system("pause");return 0;
}

?demo4:結(jié)構(gòu)體做參數(shù)(值傳遞和地址傳遞)

#include<iostream>
using namespace std;
#include <string>//定義學(xué)生結(jié)構(gòu)體
struct student
{//姓名string name;//年齡int age;//分?jǐn)?shù)int score;
};//打印學(xué)生信息函數(shù)
//1、值傳遞
void printStudent1(struct student s)
{s.age = 100;cout << "子函數(shù)中 姓名: " << s.name << " 年齡: " << s.age << " 分?jǐn)?shù): " << s.score << endl;
}//2、地址傳遞
void printStudent2(struct student * p)
{p->age = 200;cout << "子函數(shù)2中 姓名:" << p->name << " 年齡: " << p->age << " 分?jǐn)?shù):" << p->score << endl;}int main() {//結(jié)構(gòu)體做函數(shù)參數(shù)//將學(xué)生傳入到一個參數(shù)中,打印學(xué)生身上的所有信息//創(chuàng)建結(jié)構(gòu)體變量struct student s;s.name = "張三";s.age = 20;s.score = 85;//printStudent1(s);printStudent2(&s);cout << "main函數(shù)中打印 姓名: " << s.name << " 年齡: " << s.age << " 分?jǐn)?shù):" << s.score << endl;system("pause");return 0;
}

?demo3:結(jié)構(gòu)體指針;使用一個指針接受結(jié)構(gòu)體對象,類似與數(shù)組的聲明?? ?//通過結(jié)構(gòu)體指針 訪問結(jié)構(gòu)體中的屬性,需要利用 ' -> '

#include<iostream>
using namespace std;
#include <string>//結(jié)構(gòu)體指針//定義學(xué)生結(jié)構(gòu)體
struct stu
{string name;  //姓名int age;   //年齡int score; //分?jǐn)?shù)
};int main() {//1、創(chuàng)建學(xué)生結(jié)構(gòu)體變量stu s = { "張三" , 18 , 100 };//2、通過指針指向結(jié)構(gòu)體變量stu * p = &s;//3、通過指針訪問結(jié)構(gòu)體變量中的數(shù)據(jù)//通過結(jié)構(gòu)體指針 訪問結(jié)構(gòu)體中的屬性,需要利用 ' -> 'cout << "姓名: " << p->name << " 年齡: " << p->age << " 分?jǐn)?shù): " << p->score << endl;system("pause");return 0;
}

?demo2:結(jié)構(gòu)體數(shù)組復(fù)制,替換stuArray[2]覆蓋原始值,重新計算得到新值賦值

#include<iostream>
using namespace std;
#include <string>//結(jié)構(gòu)體數(shù)組
//1、定義結(jié)構(gòu)體
struct Student
{//姓名string name;//年齡int age;//分?jǐn)?shù)int score;
};int main() {//2、創(chuàng)建結(jié)構(gòu)體數(shù)組struct Student stuArray[3] ={{ "張三", 18 , 100 },{ "李四",28 , 99 },{ "王五",38 , 66 }};//3、給結(jié)構(gòu)體數(shù)組中的元素賦值stuArray[2].name = "趙六";stuArray[2].age = 80;stuArray[2].score = 60;//4、遍歷結(jié)構(gòu)體數(shù)組for (int i = 0; i < 3; i++){cout << " 姓名: " << stuArray[i].name<< " 年齡: " << stuArray[i].age<< " 分?jǐn)?shù): " << stuArray[i].score << endl;}system("pause");return 0;
}

?demo1(結(jié)構(gòu)體嵌套,結(jié)構(gòu)體數(shù)組做參數(shù)):教師結(jié)構(gòu)體:包含教師的學(xué)生,姓名;學(xué)生結(jié)構(gòu)體包含:學(xué)生姓名分?jǐn)?shù)。注:結(jié)構(gòu)體有交叉。

#include<iostream>
using namespace std;
#include <string>
#include <ctime>//學(xué)生的結(jié)構(gòu)體
struct Student
{//姓名string sName;//分?jǐn)?shù)int score;
};//老師的結(jié)構(gòu)體定義
struct Teacher
{//姓名string tName;//學(xué)生數(shù)組struct Student sArray[5];
};//給老師和學(xué)生賦值的函數(shù)
void allocateSpace(struct Teacher tArray[], int len)
{string nameSeed = "ABCDE";//給老師開始賦值for (int i = 0; i < len; i++){tArray[i].tName = "Teacher_";tArray[i].tName += nameSeed[i];//通過循環(huán)給每名老師所帶的學(xué)生賦值for (int j = 0; j < 5; j++){tArray[i].sArray[j].sName = "Student_";tArray[i].sArray[j].sName += nameSeed[j];int random = rand() % 61 + 40; // 40 ~ 100tArray[i].sArray[j].score = random;}}
}//打印所有信息
void printInfo(struct Teacher tArray[], int len)
{for (int i = 0; i < len; i++){cout << "老師姓名: " << tArray[i].tName << endl;for (int j = 0; j < 5; j++){cout << "\t學(xué)生姓名: " << tArray[i].sArray[j].sName <<" 考試分?jǐn)?shù): " << tArray[i].sArray[j].score << endl;}}
}int main() {//隨機數(shù)種子srand((unsigned int)time(NULL));//1、創(chuàng)建3名老師的數(shù)組struct Teacher tArray[3];//2、通過函數(shù)給3名老師的信息賦值,并給老師帶的學(xué)生信息賦值int len = sizeof(tArray) / sizeof(tArray[0]);allocateSpace(tArray, len);//3、打印所有老師及所帶的學(xué)生信息printInfo(tArray, len);system("pause");return 0;
}

demo0: 游戲英雄(結(jié)構(gòu)體)編程練習(xí)。使用冒泡排序,年齡升序排列。

#include<iostream>
using namespace std;
#include <string>//1、設(shè)計英雄結(jié)構(gòu)體
//英雄結(jié)構(gòu)體
struct Hero
{//姓名string name;//年齡int age;//性別string sex;
};//冒泡排序 實現(xiàn)年齡升序排列
void bubbleSort(struct Hero heroArray[], int len)
{for (int i = 0; i < len - 1; i++){for (int j = 0; j < len - i - 1; j++){//如果j 下標(biāo)的元素年齡 大于 j+1下標(biāo)的元素的年齡 ,交換兩個元素if (heroArray[j].age > heroArray[j + 1].age){struct Hero temp = heroArray[j];heroArray[j] = heroArray[j + 1];heroArray[j + 1] = temp;}}}
}//打印排序后數(shù)組中的信息
void printHero(struct Hero heroArray[], int len)
{for (int i = 0; i < len; i++){cout << "姓名: " << heroArray[i].name << " 年齡: " << heroArray[i].age<< " 性別: " << heroArray[i].sex << endl;}}int main() {//2、創(chuàng)建數(shù)組存放5名英雄struct Hero heroArray[5] ={{ "劉備",23,"男" },{ "關(guān)羽",22,"男" },{ "張飛",20,"男" },{ "趙云",21,"男" },{ "貂蟬",19,"女" },};int len = sizeof(heroArray) / sizeof(heroArray[0]);cout << "排序前打印: " << endl;for (int i = 0; i < len; i++){cout << "姓名: " << heroArray[i].name << " 年齡: " << heroArray[i].age<< " 性別: " << heroArray[i].sex << endl;}//3、對數(shù)組進行排序,按照年齡進行升序排序bubbleSort(heroArray, len);cout << "排序后打印: " << endl;//4、將排序后結(jié)果打印輸出printHero(heroArray, len);system("pause");return 0;
}

?

總結(jié)

以上是生活随笔為你收集整理的【C++】【一】结构体数组的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。