【C++】【一】结构体数组
生活随笔
收集整理的這篇文章主要介紹了
【C++】【一】结构体数组
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
demo7:函數份文件編寫
swap.h
#include <iostream>
using namespace std;//函數的聲明
void swap(int a, int b);
swap.cpp
#include "swap.h"//函數的定義
void swap(int a, int b)
{int temp = a;a = b;b = temp;cout << "a = " << a << endl;cout << "b = " << b << endl;
}
?main函數
#include<iostream>
using namespace std;
#include "swap.h"//函數的分文件編寫
//實現兩個數字進行交換的函數函數的聲明
//void swap(int a, int b);
函數的定義
//void swap(int a, int b)
//{
// int temp = a;
// a = b;
// b = temp;
//
// cout << "a = " << a << endl;
// cout << "b = " << b << endl;
//}//1、創建.h后綴名的頭文件
//2、創建.cpp后綴名的源文件
//3、在頭文件中寫函數的聲明
//4、在源文件中寫函數的定義int main() {int a = 10;int b = 20;swap(a, b);system("pause");return 0;
}
demo6:形參不改變?,當我們做值傳遞的時候,函數的形參發生改變,并不會影響實參
#include<iostream>
using namespace std;//值傳遞
//定義函數,實現兩個數字進行交換函數//如果函數不需要返回值,聲明的時候可以寫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;//當我們做值傳遞的時候,函數的形參發生改變,并不會影響實參swap3(a, b);cout << "a = " << a << endl;cout << "b = " << b << endl;system("pause");return 0;
}
demo5:結構體中const:?將函數中的形參改為指針,可以減少內存空間,而且不會復制新的副本出來
#include<iostream>
using namespace std;
#include <string>//const的使用場景struct student
{//姓名string name;//年齡int age;//分數int score;
};//將函數中的形參改為指針,可以減少內存空間,而且不會復制新的副本出來
void printStudents(const student *s)
{//s->age = 150; //加入const之后,一旦有修改的操作就會報錯,可以防止我們的誤操作cout << "姓名: " << s->name << " 年齡: " << s->age << " 得分: " << s->score << endl;
}int main() {//創建結構體變量struct student s = { "張三" , 15 , 70 };//通過函數打印結構體變量信息printStudents(&s);cout << "main中張三年齡為: " << s.age << endl;system("pause");return 0;
}
?demo4:結構體做參數(值傳遞和地址傳遞)
#include<iostream>
using namespace std;
#include <string>//定義學生結構體
struct student
{//姓名string name;//年齡int age;//分數int score;
};//打印學生信息函數
//1、值傳遞
void printStudent1(struct student s)
{s.age = 100;cout << "子函數中 姓名: " << s.name << " 年齡: " << s.age << " 分數: " << s.score << endl;
}//2、地址傳遞
void printStudent2(struct student * p)
{p->age = 200;cout << "子函數2中 姓名:" << p->name << " 年齡: " << p->age << " 分數:" << p->score << endl;}int main() {//結構體做函數參數//將學生傳入到一個參數中,打印學生身上的所有信息//創建結構體變量struct student s;s.name = "張三";s.age = 20;s.score = 85;//printStudent1(s);printStudent2(&s);cout << "main函數中打印 姓名: " << s.name << " 年齡: " << s.age << " 分數:" << s.score << endl;system("pause");return 0;
}
?demo3:結構體指針;使用一個指針接受結構體對象,類似與數組的聲明?? ?//通過結構體指針 訪問結構體中的屬性,需要利用 ' -> '
#include<iostream>
using namespace std;
#include <string>//結構體指針//定義學生結構體
struct stu
{string name; //姓名int age; //年齡int score; //分數
};int main() {//1、創建學生結構體變量stu s = { "張三" , 18 , 100 };//2、通過指針指向結構體變量stu * p = &s;//3、通過指針訪問結構體變量中的數據//通過結構體指針 訪問結構體中的屬性,需要利用 ' -> 'cout << "姓名: " << p->name << " 年齡: " << p->age << " 分數: " << p->score << endl;system("pause");return 0;
}
?demo2:結構體數組復制,替換stuArray[2]覆蓋原始值,重新計算得到新值賦值
#include<iostream>
using namespace std;
#include <string>//結構體數組
//1、定義結構體
struct Student
{//姓名string name;//年齡int age;//分數int score;
};int main() {//2、創建結構體數組struct Student stuArray[3] ={{ "張三", 18 , 100 },{ "李四",28 , 99 },{ "王五",38 , 66 }};//3、給結構體數組中的元素賦值stuArray[2].name = "趙六";stuArray[2].age = 80;stuArray[2].score = 60;//4、遍歷結構體數組for (int i = 0; i < 3; i++){cout << " 姓名: " << stuArray[i].name<< " 年齡: " << stuArray[i].age<< " 分數: " << stuArray[i].score << endl;}system("pause");return 0;
}
?demo1(結構體嵌套,結構體數組做參數):教師結構體:包含教師的學生,姓名;學生結構體包含:學生姓名分數。注:結構體有交叉。
#include<iostream>
using namespace std;
#include <string>
#include <ctime>//學生的結構體
struct Student
{//姓名string sName;//分數int score;
};//老師的結構體定義
struct Teacher
{//姓名string tName;//學生數組struct Student sArray[5];
};//給老師和學生賦值的函數
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];//通過循環給每名老師所帶的學生賦值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學生姓名: " << tArray[i].sArray[j].sName <<" 考試分數: " << 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;
}
demo0: 游戲英雄(結構體)編程練習。使用冒泡排序,年齡升序排列。
#include<iostream>
using namespace std;
#include <string>//1、設計英雄結構體
//英雄結構體
struct Hero
{//姓名string name;//年齡int age;//性別string sex;
};//冒泡排序 實現年齡升序排列
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 下標的元素年齡 大于 j+1下標的元素的年齡 ,交換兩個元素if (heroArray[j].age > heroArray[j + 1].age){struct Hero temp = heroArray[j];heroArray[j] = heroArray[j + 1];heroArray[j + 1] = temp;}}}
}//打印排序后數組中的信息
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、創建數組存放5名英雄struct Hero heroArray[5] ={{ "劉備",23,"男" },{ "關羽",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、對數組進行排序,按照年齡進行升序排序bubbleSort(heroArray, len);cout << "排序后打印: " << endl;//4、將排序后結果打印輸出printHero(heroArray, len);system("pause");return 0;
}
?
總結
以上是生活随笔為你收集整理的【C++】【一】结构体数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB【九】————ICP算法实现
- 下一篇: 【匹配算法】渐进一致采样 PROSAC(