#include<iostream>
using namespace std;
//值传递
void swap(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "使用值传递a的值为:" << a << endl;
cout << "使用值传递b的值为:" << b << endl;
cout << "---------------" << endl;
}
//地址传递
void swap2(int *p1, int *p2) {
int temp = *p1;
*p1 = *p2;
*p2 = temp;
}
void swap3(int& a, int& b) //引用传递
{
int t = a;
a = b;
b = t;
}
int main() {
int a = 10;
int b = 20;
swap(a, b);
swap2(&a, &b); //取地址
cout << "使用值传递a的值为:" << a<< endl; //a =10,b=20 值传递不改变实际参数
cout << "使用值传递b的值为:" << b << endl;
cout << "---------------" << endl;
cout << "使用地址传递c的值为:" << a << endl;//a =20,b=10,改变实际参数
cout << "使用地址传递d的值为:" <<b<< endl;
cout << "---------------" << endl;
cout << "使用引用传递a的值为:" << a << endl;//a =20,b=10,改变实际参数
cout << "使用引用传递b的值为:" << b << endl;
return 0;
}
//指针配合函数和数组
#include<iostream>
using namespace std;
//参数1:数组首地址,参数2 数组长度!
void bubbleSort(int *arr, int len) {
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int printArry(int *arr,int len) { //第一个参数为 数组地址。第二个为 数组长度
for (int i = 0; i < len ; i++) {
cout << arr[i] << endl;
}
return 0;
}
int main() {
int arr[] = { 3,2,1,5,4,6,8,7,9 };//创建数组
//数组长度
int len = sizeof(arr) / sizeof(arr[0]);//总长度除以第一个元素的长度
//创建函数实现冒泡排序
bubbleSort(arr, len);
//打印排序
printArry(arr, len);
return 0;
}
//结构体
#include<iostream>
#include<string>
using namespace std;
struct student
{
//成员列表
string name;
int age;
int score;
};
student s2 = { "lisi",19,30 };
int main() {
student s1;
s1.age = 23;
s1.name = "zhangsan";
s1.score = 30;
cout << "name:" << s1.name << "nianlin" << s1.age << "fenshu" << s1.score<<endl;
cout << "name:" << s2.name << "nianlin" << s2.age << "fenshu" << s2.score << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
struct student
{
string name;
int age;
int score;
};
int main() {
student s1 = { "张三",32,45 };//创建变量并初始化
student* p = &s1; //通过指针指向结构体变量
cout <<"姓名: "<< p->name << endl;//通过指针访问结构体中的数据
return 0;
}
//结构体做函数参数
#include<iostream>
using namespace std;
class student {
public:
int age;
};
void printStudent(student* s) {
s->age = 100;
cout << "子函数 中的姓名: " << s->age << endl;
}
int main() {
student s;
s.age = 10;
cout << "主函数中的姓名: "<<s.age<<endl;
//地址传递
printStudent(&s);
return 0;
}
//const的使用场景
#include<iostream>
using namespace std;
struct student
{
int age;
};
//const使用场景
void printStudent(const student *s) //使用指针可以减少内存的开销,不用复制全部数据。加const防止函数体中的误操作
{
cout << "年龄为" << s->age << endl;
}
int main() {
student s = { 20 };
printStudent(&s); //传进地址,用指针可以访问
return 0;
}
#include<iostream>
using namespace std;
#include<ctime>
#include<string>
struct Student {
int score;
string sName;
// struct Student sArray[5];
};
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[j].sArray[j].sName = "Student__ ";
tArray[j].sArray[j].sName += nameSeed[j];
int random = rand() % 60 + 40;//40~90
tArray[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 << "学生姓名 : " << tArray[i].sArray[j].sName << "考试分数" << tArray[i].sArray[j].score << endl;
}
}
}
int main() {
//随机数种子
srand((unsigned int)time(NULL));
//创建数组
struct Teacher tArray [3];
//赋值操作
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, len);
//打印所有老师和学生的信息
printInfo(tArray, len);
return 0;
}