四、指针
1、指针和数组
#include<iostream>
using namespace std;
void swap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
void swap02(int *a,int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//冒泡排序函数,参数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;
}
}
}
}
void printArray(int* arr, int len)
{
for (int i = 0; i < len-1; i++)
{
cout << arr[i] << " ";
}
}
int main() {
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* p = arr;
cout << "利用指针显示第一个元素" << *p << endl;
p++;
cout << "利用指针显示第二个元素" << *p << endl;
//指针和函数
//1.值传递 -- 不修改实参
int a = 10, b = 20;
swap01(a, b);
cout << "a=" << a << endl;
//2.地址传递 -- 可以修改实参
swap02(&a, &b);
cout << "a=" << a << endl;
//指针数组函数案例
int arr2[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr2) / sizeof(arr2[0]);
bubbleSort(arr2, len);
printArray(arr2, len);
return 0;
}
2、指针和结构体
#include<iostream>
#include<string>
using namespace std;
//结构体 -- 自定义数据类型
struct Student
{
string name;
int age;
double score;
}s3;
struct Teacher
{
int id;
string name;
int age;
Student stu;
};
//值传递
void printStu01(Student s1) {
s1.name = "kk";
}
//地址传递 -- 可以减少内存空间,而且不会复制新的副本出来
void printStu01(Student* s1) {
s1->name = "haha";
}
//const 可以避免一些数据的误修改
void printStu01(const Student* s2) {
// s2->name = "haha";
}
int main() {
//1.结构体定义 -- 3种初始化方式
Student s1; s1.name = "张三";
Student s2 = { "里斯",12,12.23 };
s3.name = "王五";
//2.结构体数组
Student arr[3] = { {"里斯",12,12.23},{"里斯2",12,12.23},{"里斯3",12,12.23} };
//3.结构体指针 -- 通过操作符 -> 可以获取结构体属性
const Student* p = &s1;
cout << p->name << endl;
Teacher t = { 100,"老师",10,s1 };
cout << t.stu.name << endl;
//结构体做函数参数
printStu01(s2);
cout << s2.name << endl;
printStu01(&s2);
cout << s2.name << endl;
return 1;
}
3、结构体案例
#include<iostream>
#include"string"
using namespace std;
struct Hero
{
string name;
int age;
string sex;
};
struct Student
{
string sName;
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].sName = "Student_";
tArray[i].sArray[j].sName += nameSeed[j];
int random = rand() % 61 + 40;
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].sName << " 考试分数:" << tArray[i].sArray[j].score << endl;
}
}
}
void SetHero(Hero* hero, int len) {
string name[] = { "刘","关","张","赵 ","貂" };
int age[] = { 22,25,16,33,28 };
string sex[] = { "男","男","男","男 ","女" };
for (int i = 0; i < len; i++)
{
hero[i].name = name[i];
hero[i].age = age[i];
hero[i].sex = sex[i];
}
}
void swap_hero(Hero* hero1, Hero* hero2)
{
Hero hero;
hero = *hero1;
*hero1 = *hero2;
*hero2 = hero;
}
void printHero(Hero* hero, int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j< len-1-i; j++)
{
if (hero[j].age>hero[j+1].age)
{
swap_hero(&hero[j], &hero[j + 1]);
}
}
}
for (int i = 0; i < len; i++)
{
cout << hero[i].name << " " << hero[i].age << " " << hero[i].sex << endl;
}
}
int main()
{
//随机数种子
srand((unsigned int)time(NULL));
Teacher tArray[3];
allocateSpace(tArray, sizeof(tArray) / sizeof(tArray[0]));
printInfo(tArray, sizeof(tArray) / sizeof(tArray[0]));
Hero hero[5];
SetHero(hero, sizeof(hero) / sizeof(hero[0]));
printHero(hero, sizeof(hero) / sizeof(hero[0]));
//new返回的是该 数据类型的指针
int* a = new int(10);
delete(a);
system("pause");
return 1;
}