二维数组(在一维数组上多加一个维度)
二维数组定义方式:
? 1、数据类型 数组名 [ 行数 ] [ 列数 ]; ? 2、数据类型 数组名 [ 行数 ] [ 列数 ] = { {数据1,数据2},{数据3,数据4} }; ? 3、数据类型 数组名 [ 行数 ] [ 列数 ] = { 数据1,数据2,数据3,数据4 }; ? 4、数据类型 数组名 [ ] [ 列数 ] = { 数据1,数据2,数据3,数据4 }; 建议:第二种更直观,提高代码的可读性
#include <iostream>
using namespace std;
int main() {
int arr[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 4;
arr[1][1] = 5;
arr[1][2] = 6;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
int arr2[2][3] = { {1,2,3},{4,5,6} };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr2[i][j] << " ";
}
cout << endl;
}
int arr3[2][3] = { 1,2,3,4,5,6 };
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr3[i][j] << " ";
}
cout << endl;
}
int arr4[][3] = {1, 2, 3, 4, 5, 6};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr4[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}
二维数组数组名
作用: 1、查看二维数组所占内存空间 2、获取二维数组首地址
#include <iostream>
using namespace std;
int main() {
int arr[2][3] =
{
{1,2,3},
{4,5,6}
};
cout << "二维数组占用内存空间为:" << sizeof(arr) << endl;
cout << "二位数组第一行占用内存为:" << sizeof(arr[0]) << endl;
cout << "二位数组第一个元素占用内存为:" << sizeof(arr[0][0]) << endl;
cout << "二维数组的行数为:" << sizeof(arr) / sizeof(arr[0]) << endl;
cout << "二维数组的列数为:" << sizeof(arr[0]) / sizeof(arr[0][0]) << endl;
cout << "二维数组的首地址为:" << (int)arr << endl;
cout << "二维数组第一行的首地址为:" << (int)arr[0] << endl;
cout << "二维数组第一个元素的地址为:" << (int)&arr[0][0] << endl;
cout << "二维数组第二行的首地址为:" << (int)arr[1] << endl;
system("pause");
return 0;
}
函数
作用:将一段经常使用的代码封装起来,减少重复代码 一个较大的程序,一般分为若干个程序块,每个模块实现特定的功能
函数的定义
定义步骤: 1、返回值类型 2、函数名 3、参数列表(形参) 4、函数体语句 5、return表达式 语法:
返回值类型 函数名 (参数列表)
{
函数体语句
return 表达式
}
定义示例
int add(int num1, int num2)
{
int sum;
sum = num1 + num2;
return sum;
}
函数的调用
功能:使用定义好的函数 语法:函数名(实际参数)
#include <iostream>
using namespace std;
int add(int num1, int num2)
{
int sum;
sum = num1 + num2;
return sum;
}
int main() {
int a = 10;
int b = 20;
int c;
c = add(a, b);
cout << "和为:" << c << endl;
system("pause");
return 0;
}
值传递
定义:在函数调用时,实参将数值传入形参 值传递时,如果形参发生变化,并不会影响实参
#include <iostream>
using namespace std;
void swap(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;
}
int main() {
int a = 10;
int b = 20;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
函数常见样式
1、无参无返
2、有参无返
3、无参有返
4、有参有返
#include <iostream>
using namespace std;
void test01( )
{
cout << "this is test01" << endl;
}
void test02(int a)
{
cout << "this is test02 a = " << a << endl;
}
int test03()
{
cout << "this is test03" << endl;
return 1000;
}
int test04(int a)
{
cout << "this is test04 a = " << a << endl;
return a;
}
int main() {
test01();
test02(100);
int a = test03();
cout << "a = " << a << endl;
int b = test04(10000);
cout << "b = " << b << endl;
system("pause");
return 0;
}
函数的声明
作用:告诉编译器函数名称及如何调用函数。函数的实际主体可以单独定义 注函数的声明可以多次,但是函数的定义只能有一次
#include <iostream>
using namespace std;
int max(int a, int b);
int main() {
int a = 10;
int b = 20;
cout << max(a, b) << endl;
system("pause");
return 0;
}
int max(int a, int b)
{
return a > b ? a : b;
}
函数的分文件编写
作用:让代码结构更加清晰 原文件:
#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;
return;
}
int main() {
int a = 10;
int b = 20;
swap(a, b);
system("pause");
return 0;
}
编写步骤: 1、创建后缀名为.h的头文件   2、创建后缀名为.cpp的源文件   3、在头文件中写函数的声明  4、在源文件中写函数的定义  调用: 
指针
指针的基本概念
指针的作用:可以通过指针间接访问内存 内存编号是从0开始记录的,一般用十六进制数字表示 可以利用指针变量保存地址
指针变量的定义和使用
指针变量定义语法:数据类型 * 变量名
#include <iostream>
using namespace std;
int main() {
int a = 10;
int * p;
p = &a;
cout << "a的地址为:" << &a << endl;
cout << "指针p的值为:" << p << endl;
*p = 1000;
cout << "a = " << a << endl;
cout << "*p = " << *p << endl;
system("pause");
return 0;
}
指针所占内存空间
在32位操作系统下,指针占4个字节空间大小,不管是什么数据类型 在64位操作系统下,指针占8个字节空间大小
#include <iostream>
using namespace std;
int main() {
int a = 10;
int * p;
p = &a;
cout << "sizeof(int *) = " << sizeof(p) << endl;
cout << "sizeof(float *) = " << sizeof(float *) << endl;
cout << "sizeof(double *) = " << sizeof(double *) << endl;
cout << "sizeof(char *) = " << sizeof(char *) << endl;
system("pause");
return 0;
}
空指针
定义:指针变量指向内存中编号为0的空间 作用:初始化指针变量 注意:空指针指向的内存是不可以访问的 原因:0~255之间的内存编号是系统占用的,因此不可以访问
#include <iostream>
using namespace std;
int main() {
int * p = NULL;
system("pause");
return 0;
}
野指针
定义:指针变量指向非法的内存空间
#include <iostream>
using namespace std;
int main() {
int * p = (int *)0x1100;
cout << *p << endl;
system("pause");
return 0;
}
const修饰指针
const修饰指针的三种情况: 1、const修饰指针 ——— 常量指针[const int * p = &a] 特点:指针的指向可以修改,但指针指向的值不可以修改 2、const修饰常量 ———指针常量[int * cont p = &a] 特点:指针的指向不可以修改,但指针指向的值可以修改 3、const既修饰指针,又修饰常量[const int *const p = &a] 特点:指针的指向和指针指向的值都不可以修改
#include <iostream>
using namespace std;
int main() {
int a = 10;
int b = 10;
const int * p = &a;
p = &b;
int * const p2 = &a;
*p2 = 20;
const int * const p3 = &a;
system("pause");
return 0;
}
指针和数组
作用:利用指针访问数组中元素
#include <iostream>
using namespace std;
int main() {
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
cout << "第一个元素为:" << arr[0] << endl;
int * p = arr;
cout << "利用指针访问第一个元素:" << *p << endl;
p++;
cout << "利用指针访问第二个元素:" << *p << endl;
int *p2 = arr;
cout << "利用指针遍历数组" << endl;
for (int i = 0; i < 10; i++)
{
cout << *p2 << endl;
p2++;
}
system("pause");
return 0;
}
指针和函数
作用:利用指针作为函数参数,可以修改实参的值
#include <iostream>
using namespace std;
void swap01(int a, int b)
{
int temp = a;
a = b;
b = temp;
cout << "swap01 a = " << a << endl;
cout << "swap01 b = " << b << endl;
}
void swap02(int *p1, int *p2)
{
int temp = *p1;
*p1 = *p2;
*p2 = temp;
cout << "swap02 a = " << *p1 << endl;
cout << "swap02 b = " << *p2 << endl;
}
int main() {
int a = 10;
int b = 20;
swap01(a, b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap02(&a, &b);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
指针、数组、函数
案例:封装一个函数,利用冒泡排序实现对整形数组的升序排序
#include <iostream>
using namespace std;
void bubbleSort(int *arr,int len)
{
for (int i = 0; i < len - 1; i++)
{
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void printArry(int *arr, int len)
{
for (int i = 0; i < len; i++)
{
cout << arr[i] << endl;
}
}
int main() {
int arr[10] = { 4,3,6,9,1,2,10,8,7,5 };
int len = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, len);
printArry(arr, len);
system("pause");
return 0;
}
结构体
结构体的基本概念
结构体数与用户自定义的数据类型,允许用户存储不同的数据类型
结构体的定义和使用
语法:struct 结构体名 { 结构体成员列表 }; 通过结构体创建变量的方式: 1、struct 结构体名 变量名 2、struct 结构体名 变量名 = { 成员1值,成员2值…} 3、定义结构体时顺便创建变量不建议使用
#include <iostream>
using namespace std;
#include <string>
struct Student
{
string name;
int age;
int score;
}s3;
int main() {
struct Student s1;
s1.name = "张三";
s1.age = 18;
s1.score = 100;
cout << "姓名:" << s1.name << "年龄:" << s1.age
<< "分数: " << s1.score << endl;
struct Student s2 = { "李四",19,80 };
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;
}
结构体数组
作用:将自定义的结构体放入到数组中方便维护 语法:struct 结构体名 数组名[元素个数] = { {}, {} ,…{} }
#include <iostream>
using namespace std;
#include <string>
struct Student
{
string name;
int age;
int score;
};
int main() {
struct Student stuArray[3]=
{
{"张三",18,100},
{"李四",19,99},
{"王五",20,66}
};
stuArray[2].age = 19;
for (int i = 0; i < 3; i++)
{
cout << " 姓名: " << stuArray[i].name
<< " 年龄:" << stuArray[i].age
<< " 分数:" << stuArray[i].score << endl;
}
system("pause");
return 0;
}
结构体指针
作用:通过指针访问结构体中的成员 利用操作符->可以通过结构体指针访问结构体属性
#include <iostream>
using namespace std;
#include <string>
struct Student
{
string name;
int age;
int score;
};
int main() {
struct Student s = { "张三",18,100 };
struct Student * p = &s;
cout << " 姓名:" << p->name
<< " 年龄:" << p->age
<< " 分数:" << p->score << endl;
system("pause");
return 0;
}
结构体嵌套结构体
作用:结构体中的成员可以是另一个结构体 在结构体中定义另一个结构体作为成员,用以解决实际问题
#include <iostream>
using namespace std;
#include <string>
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.name = "小王";
t.stu.age = 20;
t.stu.score = 60;
cout << " 老师姓名:" << t.name
<< " 老师标号:" << t.id
<< " 老师年龄:" << t.age
<< " 老师辅导的学生姓名:" << t.stu.name
<< " 老师辅导的学生年龄:" << t.stu.age
<< " 老师辅导的学生分数:" << t.stu.score << endl;
system("pause");
return 0;
}
结构体做函数参数
作用:将结构体作为参数向函数中传递 传递方式: 1、值传递 2、地址传递
#include <iostream>
using namespace std;
#include <string>
struct Student
{
string name;
int age;
int score;
};
void printStudent1(struct Student s)
{
s.age = 100;
cout << "子函数1中 姓名:" << s.name
<< " 年龄:" << s.age << " 分数:" << s.score << endl;
}
void printStudent2(struct Student * p)
{
p->age = 100;
cout << "子函数2中 姓名:" << p->name
<< " 年龄:" << p->age << " 分数:" << p->score << endl;
}
int main() {
Student s;
s.name = "张三";
s.age = 20;
s.score = 85;
cout << "main函数中 姓名:" << s.name
<< " 年龄:" << s.age << " 分数:" << s.score << endl;
printStudent1(s);
cout << "main函数中 姓名:" << s.name
<< " 年龄:" << s.age << " 分数:" << s.score << endl;
printStudent2(&s);
cout << "main函数中 姓名:" << s.name
<< " 年龄:" << s.age << " 分数:" << s.score << endl;
system("pause");
return 0;
}
结构体中const使用场景
作用:用const来防止误操作
#include <iostream>
using namespace std;
#include <string>
struct Student
{
string name;
int age;
int score;
};
void printStudents(const Student * s)
{
cout << " 姓名:" << s->name << " 年龄:" << s->age
<< " 分数:" << s->score << endl;
}
int main() {
struct Student s = { "张三",15,70 };
cout << "main函数中张三的年龄为:" << s.age << endl;
printStudents(&s);
cout << "main函数中张三的年龄为:" << s.age << endl;
system("pause");
return 0;
}
结构体案例1

#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;
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 << "\t学生姓名:" << tArray[i].sArray[j].sName
<< " 考试分数:" << tArray[i].sArray[j].score << endl;
}
}
}
int main() {
srand((unsigned int)time(NULL));
Teacher tArray[3];
int len = sizeof(tArray) / sizeof(tArray[0]);
allocateSpace(tArray, 3);
printInfo(tArray, 3);
system("pause");
return 0;
}
结构体案例2

#include <iostream>
using namespace std;
#include <string>
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++)
{
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() {
Hero heroArray[5] =
{
{"刘备",23,"男"},
{"关羽",22,"男"},
{"张飞",20,"男"},
{"赵云",21,"男"},
{"貂蝉",19,"女"}
};
int len = sizeof(heroArray) / sizeof(heroArray[0]);
cout << "排序前:" << endl;
printHero(heroArray, len);
bubbleSort(heroArray, len);
cout << "排序后:" << endl;
printHero(heroArray, len);
system("pause");
return 0;
}
|