传智播客C++视频学习笔记(1)

下载Visual Studio Community版本,

#include<iostream>

using namespace std;

int main()
{
    cout << "Hello World" << endl;

    system("pause");

    return 0;
}
Hello World

单行注释,多行注释,

main( )函数是一个程序的入口,每个程序都必须有这么一个函数,有且仅有一个,

变量给一段指定的内存空间起名,方便操作这段内存,

#include<iostream>

using namespace std;

int main()
{
    int a = 10;

    cout << "a = " << a << endl;

    system("pause");

    return 0;
}
变量

常量用于记录程序中不可更改的数据,

#include<iostream>

using namespace std;

//宏常量
#define Day 7

int main()
{
    //Day = 8;
    cout << "一周有" << Day << "" << endl;

    //const修饰的变量
    const int month = 12;
    //month = 13;
    cout << "一年有" << month << "个月" << endl;

    system("pause");

    return 0;
}
常量

关键字是C++中预先保留的单词,也叫标识符,不要用关键字给变量或常量起名称,

标识符命名规则:标识符不能是关键字,标识符只能由字母、数字、下划线组成,第一个字符必须为字母或下划线,标识符中字母区分大小写,见名知意,

C++规定在创建一个变量或常量时。必须要指定出相应的数据类型,否则无法给变量或常量分配内存,

#include<iostream>

using namespace std;

int main()
{
    //短整型,2字节
    short num1 = 10;

    //整型,4字节                           //四者取值范围也不同哦~
    int num2 = 10;

    //长整形,4字节
    long num3 = 10;

    //长长整型,8字节
    long long num4 = 10;

    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;
    cout << "num3 = " << num3 << endl;
    cout << "num4 = " << num4 << endl;

    system("pause");

    return 0;
}
整型

利用sizeof关键字可以统计数据类型所占内存大小,

#include<iostream>

using namespace std;

int main()
{
    short num1 = 10;
    cout << "short占用内存空间为" << sizeof(num1) << endl;

    int num2 = 10;
    cout << "int占用内存空间为" << sizeof(num2) << endl;

    long num3 = 10;
    cout << "long占用内存空间为" << sizeof(num3) << endl;

    long long num4 = 10;
    cout << "long long占用内存空间为" << sizeof(num4) << endl;

    system("pause");

    return 0;
}
sizeof
#include<iostream>

using namespace std;

int main()
{
    //单精度float,4字节
    float f1 = 3.1415926f;
    cout << "f1 = " << f1 << endl;

    //双精度double,8字节
    double d1 = 3.1415926;
    cout << "d1 = " << d1 << endl;

    //科学计数法
    float f2 = 3e2; //3*10^2
    cout << "f2 = " << f2 << endl;

    float f3 = 3e-2; //3*0.1^2
    cout << "f3 = " << f3 << endl;

    system("pause");

    return 0;
}
实型

字符型变量用于显示单个字符,

#include<iostream>

using namespace std;

int main()
{
    //C++中字符型变量只占用1个字节

    //字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放到存储单元

    //字符型变量创建方式
    char ch = 'a';
    cout << "ch = " << ch << endl;

    //字符型变量所占内存大小
    cout << "字符型变量所占内存大小:" << sizeof(ch) << endl;

    //在显示字符型变量时,用单引号将字符括起来,不要用双引号

    //单引号内只能有一个字符,不可以是字符串

    //字符型变量对应ASCII编码
    cout << (int)ch << endl;

    system("pause");

    return 0;
}
字符型

转义字符用于表示一些不能显示出来的ASCII字符,

#include<iostream>

using namespace std;

int main()
{
    //换行符
    cout << "Hello World\n";

    //反斜杠
    cout << "\\" << endl;

    //水平制表符
    cout << "a\tHello World" << endl;
    cout << "aa\tHello World" << endl;
    cout << "aaa\tHello World" << endl;
    cout << "aaaa\tHello World" << endl;
    cout << "aaaaa\tHello World" << endl;
    cout << "aaaaaa\tHello World" << endl;

    system("pause");

    return 0;
}
转义字符
#include<iostream>

using namespace std;

#include<string>

int main()
{
    //C风格字符串
    char str1[] = "Hello World 1";
    cout << str1 << endl;

    //C++风格字符串
    string str2 = "Hello World 2";
    cout << str2 << endl;

    system("pause");

    return 0;
}
字符串型
#include<iostream>

using namespace std;

int main()
{
    //布尔类型代表真或假的值

    //true,真,本质是1
    //false,假,本质是0

    //布尔类型占1个字节

    bool flag = true;
    cout << flag << endl;

    flag = false;
    cout << flag << endl;

    cout << "布尔类型所占字节为:" << sizeof(flag) << endl;

    system("pause");

    return 0;
}
布尔类型
#include<iostream>

using namespace std;

int main()
{
    //cin 用于从键盘获取数据

    int n = 0;
    cout << "请输入一个整型变量" << endl;
    cin >> n;
    cout << "整型变量n等于" << n << endl;

    system("pause");

    return 0;
}
数据输入
#include<iostream>

using namespace std;

int main()
{
    int a1 = 10;
    int b1 = 3;
    cout << a1 + b1 << endl;
    cout << a1 - b1 << endl;
    cout << a1 * b1 << endl;
    cout << a1 / b1 << endl; //两个整数相除结果依然是整数,将小数部分去除

    int a2 = 10;
    int b2 = 20;
    cout << a2 / b2 << endl;

    int a3 = 10;
    int b3 = 0;
    //cout << a3 / b3 << endl; //除数不可以为0

    //两个小数可以相除
    double d1 = 0.5;
    double d2 = 0.25;
    cout << d1 / d2 << endl;

    int a4 = 10;
    int b4 = 3;
    cout << a4 % b4 << endl;
    int a5 = 10;
    int b5 = 20;
    cout << a5 % b5 << endl; //取模运算本质就是求余数,两个小数是不可以做取模运算的

    //前置递增
    int a6 = 10;
    ++a6;
    cout << a6 << endl;

    //后置递增
    int b6 = 10;
    b6++;
    cout << b6 << endl;

    //前置与后置区别,前置递增变量先+1在运算,后置递增先运算变量在+1

    //前置
    int a7 = 10;
    int b7 = ++a7 * 10;
    cout << "a7 = " << a7 << endl;
    cout << "b7 = " << b7 << endl;

    //后置
    int a8 = 10;
    int b8 = a8++ * 10;
    cout << "a8 = " << a8 << endl;
    cout << "b8 = " << b8 << endl;

    system("pause");

    return 0;
}
算数运算符
#include<iostream>

using namespace std;

int main()
{
    //赋值运算符

    // =

    // +=

    // -=

    // *=

    // /=

    // %=

    //
    int a = 10;
    a += 2;                           //a = a + 2;
    cout << "a = " << a << endl;

    system("pause");

    return 0;
}
赋值运算符
#include<iostream>

using namespace std;

int main()
{
    //比较运算符,用于表达式的比较,并返回一个真值或假值

    int a = 10;
    int b = 20;

    // ==
    cout << (a == b) << endl; //0

    // !=
    cout << (a != b) << endl; //1

    // <
    cout << (a < b) << endl; //1

    // >
    cout << (a > b) << endl; //0

    // <=
    cout << (a <= b) << endl; //1

    // >=
    cout << (a >= b) << endl; //0

    system("pause");

    return 0;
}
比较运算符
#include<iostream>

using namespace std;

int main()
{
    //逻辑运算符,用于根据表达式的值返回真值或假值

    //C++中非0数字皆为真

    // 非 ! 取反

    // 与 && 同真为真,其余为假

    // 或 || 同假为假,其余为真

    system("pause");

    return 0;
}
逻辑运算符

程序流程结构:顺序结构,选择结构,循环结构,

#include<iostream>

using namespace std;

int main()
{
    //选择结构

    int score = 0;
    cout << "请输入一个分数:" << endl;
    cin >> score;
    cout << "您输入的分数为:" << score << endl;

    if (score > 600)
    {
        cout << "恭喜您考上一本大学哦" << endl;

        if (score > 700)
        {
            cout << "恭喜您考上清华大学哦" << endl;
        }
        else if (score > 650)
        {
            cout << "恭喜您考上北京大学哦" << endl;
        }
    }
    else if (score > 500)
    {
        cout << "恭喜您考上二本大学哦" << endl;
    }
    else if (score > 400)
    {
        cout << "恭喜您考上三本大学哦" << endl;
    }
    else
    {
        cout << "恭喜您考上家里蹲大学哦" << endl;
    }

    system("pause");

    return 0;
}
选择结构-if语句
#include<iostream>

using namespace std;

int main()
{
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;

    cout << "请输入小猪A的体重" << endl;
    cin >> num1;
    cout << "请输入小猪B的体重" << endl;
    cin >> num2;
    cout << "请输入小猪C的体重" << endl;
    cin >> num3;

    cout << "小猪A的体重为:" << num1 << endl;
    cout << "小猪B的体重为:" << num2 << endl;
    cout << "小猪C的体重为:" << num3 << endl;

    if (num1 > num2)
    {
        if (num1 > num3)
        {
            cout << "小猪A最重哦" << endl;
        }
        else
        {
            cout << "小猪C最重哦" << endl;
        }
    }
    else
    {
        if (num2 > num3)
        {
            cout << "小猪B最重哦" << endl;
        }
        else
        {
            cout << "小猪C最重哦" << endl;
        }
    }

    system("pause");

    return 0;
}
三只小猪称体重
#include<iostream>

using namespace std;

int main()
{
    //三目运算符

    //表达式1 ? 表达式2 : 表达式3

    //若表达式1值为真,执行表达式2并返回其结果
    //若表达式1值为假,执行表达式3并返回其结果

    //在C++中三目运算符返回的是变量,可以继续赋值

    int a = 10;
    int b = 20;
    int c = 0;

    c = (a > b ? a : b);

    cout << "c = " << c << endl;

    system("pause");

    return 0;
}
三目运算符
#include<iostream>

using namespace std;

int main()
{
    //switch语句中表达式类型只能是整型或字符型
    //case中如果没有break,程序会一直向下执行
    //与if语句相比,switch语句结构清晰,执行效率高,缺点是不可以判断区间

    int score = 0;

    cout << "请您给此电影评分" << endl;

    cin >> score;

    switch (score)
    {
    case 10:
        cout << "经典电影" << endl;
        break;
    case 9:
        cout << "经典电影" << endl;
        break;
    case 8:
        cout << "一般电影" << endl;
        break;
    case 7:
        cout << "一般电影" << endl;
        break;
    default:
        cout << "烂片" << endl;
        break;
    }

    system("pause");

    return 0;
}
选择结构-switch语句
#include<iostream>

using namespace std;

int main()
{
    //在执行循环语句时,程序必须提供跳出循环的出口,否则会出现死循环

    int num = 0;

    while (num < 10)
    {
        cout << "num = " << num << endl;
        num++;
    }

    system("pause");

    return 0;
}
while循环语句
#include<iostream>

using namespace std;

#include<ctime>

int main()
{
    //利用当前系统时间生成随机数
    srand((unsigned int)time(NULL));

    //系统生成随机数
    int num = rand() % 100 + 1;
    //cout << "num = " << num << endl;

    //玩家进行猜测
    int val = 0;

    while (1)
    {
        cin >> val;

        //判断玩家的猜测
        if (val > num)
        {
            cout << "猜测过大" << endl;
        }
        else if (val < num)
        {
            cout << "猜测过小" << endl;
        }
        else
        {
            cout << "猜对了哦" << endl;
            break;
        }
    }

    system("pause");

    return 0;
}
猜数字游戏
#include<iostream>

using namespace std;

int main()
{
    //与while语句区别为,do...while语句会先执行一次循环语句,再判断循环条件

    int num = 0;

    do
    {
        cout << "num = " << num << endl;
        num++;
    } while (num < 10);

    system("pause");

    return 0;
}
do-while循环语句
#include<iostream>

using namespace std;

int main()
{
    //水仙花数即,1^3 + 5^3 + 3^3 = 153

    int num = 100;

    do
    {
        int a = 0;
        int b = 0;
        int c = 0;

        a = num % 10; //个位
        b = num / 10 % 10; //十位
        c = num / 100; //百位

        if (a* a* a + b * b * b + c * c * c == num)
        {
            cout << num << endl;
        }
        num++;
    } while (num < 1000);

    system("pause");

    return 0;
}
水仙花数
#include<iostream>

using namespace std;

int main()
{            //0          //1   //3
    for (int i = 0; i < 10; i++)
    {
        //2
        cout << i << endl;          //执行顺序
    }

    system("pause");

    return 0;
}
for循环语句
#include<iostream>

using namespace std;

int main()
{
    for (int i = 1; i < 100; i++)
    {
        if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
        {
            cout << "敲桌子~" << endl;
        }
        else
        {
            cout << i << endl;
        }
    }

    system("pause");

    return 0;
}
敲桌子
#include<iostream>

using namespace std;

int main()
{
    //外层循环执行一次,内层循环执行一轮

    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            cout << "*" << " ";
        }
        cout << endl;
    }

    system("pause");

    return 0;
}
嵌套循环
#include<iostream>

using namespace std;

int main()
{
    //列数 * 行数 = 计算结果

    //列数 <= 当前行数

    for (int i = 1; i <= 9; i++)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << j << " * " << i << " = " << j * i << " ";
        }
        cout << endl;
    }

    system("pause");

    return 0;
}
乘法口诀表
#include<iostream>

using namespace std;

int main()
{
    //break语句

    //跳出选择结构或循环结构

    //出现在switch语句中时,作用是终止case并跳出switch
    //出现在循环语句中时,作用是跳出当前的循环语句
    //出现在嵌套循环语句中时,作用是跳出最近的内层循环语句

    //示例1
    cout << "请选择您挑战的副本难度:" << endl;
    cout << "1、普通" << endl;
    cout << "2、中等" << endl;
    cout << "3、困难" << endl;
    int select = 0;
    cin >> select;
    switch (select)
    {
    case 1:
        cout << "您选择的是普通难度" << endl;
        break;
    case 2:
        cout << "您选择的是中等难度" << endl;
        break;
    case 3:
        cout << "您选择的是困难难度" << endl;
        break;
    default:
        cout << "GG Baby" << endl;
        break;
    }



    //示例2
    for (int i = 0; i < 10; i++)
    {
        if (i == 5)
        {
            break;
        }
        cout << i << endl;
    }



    //示例3
    for (int i = 0; i < 10; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            if (j == 5)
            {
                break;
            }
            cout << " * ";
        }
        cout << endl;
    }

    system("pause");

    return 0;
}
break语句
#include<iostream>

using namespace std;

int main()
{
    //continue语句

    //在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环

    //continue并没有使整个循环语句终止,但break会跳出循环

    for (int i = 0; i < 100; i++)
    {
        if (i % 2 == 0)
        {
            continue;
        }
        cout << i << endl;
    }

    system("pause");

    return 0;
}
continue语句
#include<iostream>

using namespace std;

int main()
{
    //goto语句

    //可以无条件跳转语句

    cout << "1" << endl;
    goto FLAG;
    cout << "2" << endl;
    cout << "3" << endl;
    cout << "4" << endl;
FLAG:
    cout << "5" << endl;

    system("pause");

    return 0;
}
goto语句

所谓数组,就是一个集合,里面存放了相同类型的数据元素,数组是由连续的内存位置组成的,

#include<iostream>

using namespace std;

int main()
{
    //一维数组定义方式
    //数据类型 数组名[数组长度];
    //数据类型 数组名[数组长度] = {值1 , 值2 ...};
    //数据类型 数组名[ ] = {值1 , 值2 ...};
    int arr1[10];
    int arr2[10] = { 1,2,3,4,5,6 }; //若{}内不足10个数据,剩余数据用0补齐
    int arr3[] = { 10,20,30,40,50,60,70,80 };

    //数组命名规范与变量命名规范一致,不要和变量重名
    //数组下标是从0开始索引

    //一维数组名称用途:
    //可以获取数组在内存中的占用空间
    //可以获取数组在内存中的首地址
    int arr4[10] = { 1,2,3,4,5,6,7,8,9,10 };
    cout << "数组在内存中的占用空间为" << sizeof(arr4) << endl;
    cout << "数组中每个元素的占用空间为" << sizeof(arr4[0]) << endl;
    cout << "数组元素个数为" << sizeof(arr4) / sizeof(arr4[0]) << endl;
    cout << "数组首地址为" << (int)arr4 << endl;
    cout << "数组中第一个元素地址为" << (int)&arr4[0] << endl;
    cout << "数组中第二个元素地址为" << (int)&arr4[1] << endl;

    system("pause");

    return 0;
}
一维数组
#include<iostream>

using namespace std;

int main()
{
    int arr[5] = { 300,350,200,400,250 };

    int max = 0;

    for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        if (arr[i] > max)
        {
            max = arr[i];
        }
    }

    cout << "最大值为" << max << endl;

    system("pause");

    return 0;
}
五只小猪称体重
#include<iostream>

using namespace std;

int main()
{
    int arr[5] = { 1,3,2,5,4 };

    cout << "数组逆置前:" << endl;
    for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        cout << arr[i] << endl;
    }

    //起始下标
    int start = 0;
    //结束下标
    int end = sizeof(arr) / sizeof(arr[0]) - 1;

    while (start < end)
    {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        //下标更新
        start++;
        end--;
    }

    cout << "数组逆置后:" << endl;
    for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        cout << arr[i] << endl;
    }

    system("pause");

    return 0;
}
数组元素逆置
#include<iostream>

using namespace std;

int main()
{
    //比较相邻元素,如果第一个比第二个大,就交换它们两个

    int arr[9] = { 4,2,8,0,5,7,1,3,9 };

    //排序轮数 = 元素个数 - 1
    for (int i = 0; i < (sizeof(arr) / sizeof(arr[0])) - 1; i++)
    {
        //对比次数 = 元素个数 - 当前轮数 - 1
        for (int j = 0; j < (sizeof(arr) / sizeof(arr[0])) - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
    {
        cout << arr[i] << endl;
    }

    system("pause");

    return 0;
}
冒泡排序
#include<iostream>

using namespace std;

int main()
{
    //二维数组定义方式
    //数据类型 数组名[行数][列数];
    //数据类型 数组名[行数][列数] = {{数据1 , 数据2} , {数据3 , 数据4} ...};
    //数据类型 数组名[行数][列数] = {数据1 , 数据2 , 数据3 , 数据4 ...};
    //数据类型 数组名[ ][列数] = {数据1 , 数据2 , 数据3 , 数据4 ...};

    //1
    int arr1[2][3];
    arr1[0][0] = 1;
    arr1[0][1] = 2;
    arr1[0][2] = 3;
    arr1[1][0] = 4;
    arr1[1][1] = 5;
    arr1[1][2] = 6;
    //外层循环打印行数
    for (int i = 0; i < 2; i++)
    {
        //内层循环打印列数
        for (int j = 0; j < 3; j++)
        {
            cout << arr1[i][j] << endl;
        }
    }

    //2
    int arr2[2][3] =
    {
        {1,2,3},
        {4,5,6}
    };

    //3
    int arr3[2][3] = { 1,2,3,4,5,6 };

    //4
    int arr4[][3] = { 1,2,3,4,5,6 };



    //二维数组名称用途:
    //可以获取数组在内存中的占用空间
    //可以获取数组在内存中的首地址
    cout << "二维数组在内存中的占用空间为" << sizeof(arr2) << endl;
    cout << "二维数组一行元素的占用空间为" << sizeof(arr2[0]) << endl;
    cout << "二维数组单个元素的占用空间为" << sizeof(arr2[0][0]) << endl;
    cout << "二维数组行数为" << sizeof(arr2) / sizeof(arr2[0]) << endl;
    cout << "二维数组列数为" << sizeof(arr2[0]) / sizeof(arr2[0][0]) << endl;

    cout << "二维数组首地址为" << (int)arr2 << endl;
    cout << "二维数组第一行地址为" << (int)arr2[0] << endl;
    cout << "二维数组第二行地址为" << (int)arr2[1] << endl;
    cout << "二维数组第一个元素地址为" << (int)&arr2[0][0] << endl;
    cout << "二维数组第二个元素地址为" << (int)&arr2[0][1] << endl;

    system("pause");

    return 0;
}
二维数组
#include<iostream>

using namespace std;

int main()
{
    int score[3][3] =
    {
        {100,100,100},
        {90,50,100},
        {60,70,80}
    };

    string names[3] = { "张三","李四","王五" };

    for (int i = 0; i < 3; i++)
    {
        int sum = 0;
        for (int j = 0; j < 3; j++)
        {
            sum += score[i][j];
        }
        cout << names[i] << "的总分为:" << sum << endl;
    }

    system("pause");

    return 0;
}
考试成绩统计

函数就是将一段经常使用的代码封装起来,减少重复代码,

#include<iostream>

using namespace std;

int add(int num1, int num2) //形参
{
    int sum = num1 + num2;
    return sum;
}

int main()
{
    //返回值类型 函数名 (参数列表)
    //{
         //函数体语句

         //return表达式
    //}

    int a = 100;
    int b = 10;
    int c = add(a, b); //实参
    cout << "c = " << 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 = 100;
    int b = 10000;

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap(a, b);                         //值传递时,形参发生任何改变,都不会影响实参

    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    system("pause");

    return 0;
}
值传递
#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 1234;
}

int test04(int a)
{
    cout << "this is test04" << endl;
    return a;
}

int main()
{
    test01();

    test02(222222);

    int three = test03();
    cout << "three = " << three << endl;

    int four = test04(444444);
    cout << "four = " << four << endl;

    system("pause");

    return 0;
}
函数常见样式
#include<iostream>

using namespace std;

//函数声明可以多次,函数定义只能一次

int max(int a, int b);
int max(int a, int b);

int main()
{
    int a = 10;
    int b = 100;
    int c = max(a, b);
    cout << "c = " << c << endl;

    system("pause");

    return 0;
}

int max(int a, int b)
{
    return a > b ? a : b;
}
函数声明
#include<iostream>

using namespace std;

//函数分文件编写:
//创建后缀为.h的头文件(swap.h)
//创建后缀为.cpp的源文件(swap.cpp)
//在头文件中写函数声明(注意要写标准框架哦)
//在源文件中写函数定义(在swap.cpp中包含swap.h 即#include"swap.h")
//最后调用时还要在包含一次#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;
}
int main()
{
    int a = 1;
    int b = 2;
    swap(a, b);

    system("pause");

    return 0;
}
函数分文件编写

指针的作用为可以通过指针间接访问内存,

#include<iostream>

using namespace std;


int main()
{
    //指针记录内存地址
    //内存地址是从0开始的,一般为16进制

    //可以通过 & 操作符获取变量的内存地址

    //指针变量和普遍变量的区别:
    //指针变量存放的是地址,普通变量存放的是数据
    //指针变量可以通过 * 操作符,操作指针变量所指向的内存,称为解引用

    //指针定义:
    //数据类型* 变量名;
    int* p;
    int a = 10;
    p = &a;
    cout << "&a = " << &a << endl;
    cout << "p = " << p << endl;

    //指针使用:
    //*变量名
    cout << "*p = " << *p << endl;

    system("pause");

    return 0;
}
指针的定义及使用
#include<iostream>

using namespace std;


int main()
{
    int a = 10;
    int* p = &a;

    cout << "*p = " << *p << endl; //解引用

    cout << "sizeof(int*) = " << sizeof(int*) << endl;
    cout << "sizeof(float*) = " << sizeof(float*) << endl;
    cout << "sizeof(double*) = " << sizeof(double*) << endl;
    cout << "sizeof(char*) = " << sizeof(char*) << endl;

    //32位操作系统,所有数据类型指针均占用4个字节
    //64位操作系统,所有数据类型指针均占用8个字节

    system("pause");

    return 0;
}
指针所占内存空间
#include<iostream>

using namespace std;


int main()
{
    //空指针
    int* p1 = NULL;
    cout << *p1 << endl;

    //野指针
    int* p2 = (int*)0x1100;
    cout << *p2 << endl;

    system("pause");

    return 0;
}
空指针和野指针
#include<iostream>

using namespace std;

int main()
{
    //const修饰指针,常量指针(指针指向可以修改---p,指针指向的值不可以修改---*p)
    //const修饰常量,指针常量(指针指向不可以修改---p,指针指向的值可以修改---*p)
    //const既修饰指针又修饰常量(指针指向和指针指向的值均不可以修改)
    int a = 10;
    const int* p1 = &a;
    int* const p2 = &a;
    const int* const p3 = &a;

    system("pause");

    return 0;
}
const修饰指针
#include<iostream>

using namespace std;

int main()
{
    int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };

    int* p = arr;

    cout << "数组中第一个元素是" << arr[0] << endl;
    cout << "数组中第一个元素是" << *p << endl;

    //利用指针遍历数组
    for (int i = 0; i < 10; i++)
    {
        cout << *p << endl;
        p++;
    }

    system("pause");

    return 0;
}
指针和数组
#include<iostream>

using namespace std;

//值传递不会修改实参
void swap01(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
}

//地址传递会修改实参
void swap02(int* p1, int* p2)
{
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

int main()
{
    int num1 = 10;
    int num2 = 20;

    swap01(num1, num2);
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;

    cout << "============" << endl;

    swap02(&num1, &num2);
    cout << "num1 = " << num1 << endl;
    cout << "num2 = " << num2 << endl;

    system("pause");

    return 0;
}
指针和函数
#include<iostream>

using namespace std;

                //数组首地址 //数组长度 int* arr = int arr[]

                //当数组名称传入函数作为参数时,数组名称退化为数组首地址

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 printArray(int* arr, int len)
{
    for (int i = 0; i < len; i++)
    {
        cout << *arr << endl;
        arr++;
    }
}

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);

    printArray(arr, len);

    system("pause");

    return 0;
}
指针、数组、函数

结构体是用户自定义的数据类型,允许用户存储不同的数据类型,

#include<iostream>

using namespace std;

//定义结构体时,struct关键字不可以省略
struct Student
{
    string name;
    int age;
    int score;
}s3;

int main()
{
    //创建结构体变量三种方式

    //创建结构体变量时,struct关键字可以省略

    //结构体变量利用 . 操作符访问成员

    //1
    struct Student s1;
    s1.name = "张三";
    s1.age = 20;
    s1.score = 100;
    cout << "姓名 = " << s1.name << " 年龄 = " << s1.age << " 分数 = " << s1.score << endl;

    //2
    struct Student s2 = { "李四",21,99 };
    cout << "姓名 = " << s2.name << " 年龄 = " << s2.age << " 分数 = " << s2.score << endl;

    //3
    s3.name = "王五";
    s3.age = 22;
    s3.score = 98;
    cout << "姓名 = " << s3.name << " 年龄 = " << s3.age << " 分数 = " << s3.score << endl;

    system("pause");

    return 0;
}
结构体的定义及使用
#include<iostream>

using namespace std;

struct Student
{
    string name;
    int age;
    int score;
};

int main()
{
    //将自定义结构体放入数组中方便维护

    struct Student stuArray[3] =
    {
        {"张三",18,60},
        {"李四",19,70},
        {"王五",20,80}
    };

    stuArray[2].name = "赵六";

    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;

struct Student
{
    string name;
    int age;
    int score;
};

int main()
{
    //通过指针访问结构体中成员

    //利用 -> 操作符可以通过结构体指针操作结构体属性

    struct Student s1 = { "张三",18,100 };

    struct Student* p = &s1;

    p->score = 99;

    cout << "姓名 = " << p->name << " 年龄 = " << p->age << " 分数 = " << p->score << endl;

    system("pause");

    return 0;
}
结构体指针
#include<iostream>

using namespace std;

struct Student
{
    string name;
    int age;
    int score;
};

struct Teacher
{
    int id;
    string name;
    int age;
    struct Student s1;
};

int main()
{
    //结构体中成员可以是另一个结构体

    struct Teacher t1;

    t1.id = 1;
    t1.name = "黄老师";
    t1.age = 18;

    t1.s1.name = "小铭";
    t1.s1.age = 3;
    t1.s1.score = 60;

    system("pause");

    return 0;
}
结构体嵌套结构体
#include<iostream>

using namespace std;

struct Student
{
    string name;
    int age;
    int score;
};

//值传递
void printStudent01(struct Student stu)
{
    stu.name = "李四";

    cout << "值传递函数打印: 姓名 = " << stu.name << " 年龄 = " << stu.age << " 分数 = " << stu.score << endl;

}

//地址传递
void printStudent02(struct Student* p)
{
    p->name = "王五";

    cout << "地址传递函数打印: 姓名 = " << p->name << " 年龄 = " << p->age << " 分数 = " << p->score << endl;

}

int main()
{
    struct Student s1;

    s1.name = "张三";
    s1.age = 29;
    s1.score = 99;

    //printStudent01(s1);

    //若想修改主函数中数据,使用地址传递
    printStudent02(&s1);

    cout << "主函数中打印: 姓名 = " << s1.name << " 年龄 = " << s1.age << " 分数 = " << s1.score << endl;

    system("pause");

    return 0;
}
结构体做函数参数
#include<iostream>

using namespace std;

struct Student
{
    string name;
    int age;
    int score;
};

//函数形参修改为指针时,可以减少内存空间占用,即不会像值传递一样copy副本
//const修饰后,可以防止误操作,即无法修改*p的值
void printStudent(const struct Student* p)
{
    cout << "姓名 = " << p->name << " 年龄 = " << p->age << " 分数 = " << p->score << endl;
}

int main()
{
    struct Student s1;

    s1.name = "张三";
    s1.age = 29;
    s1.score = 99;

    printStudent(&s1);

    system("pause");

    return 0;
}
结构体中const使用场景 

end

posted @ 2020-01-03 14:12  _Huang95  阅读(317)  评论(0编辑  收藏  举报