• C++学习笔记--黑马程序员


    学习目标:

    • 掌握 C++入门知识
    • 掌握 STL
    • 洛谷算法训练题

    学习内容:

    C++入门知识

    一、基本介绍:C++不同于C语言,这是一门面向对象的高级程序设计语言。
    二、面向对象与面向过程:什么是面向对象?对象又是什么? 对象是对客观事物的抽象,也就是说,任何事物都可以看做对象。说到面向对象,就不得不提面向过程了。我们可以举个例子来理解他们:
    例如:我们要去自动售货机买饮料,如果面向过程:我们需要走到售货机前,投掷硬币,拿到饮料,它所针对的是我买饮料的这个过程。而面向对象则是:自动售货机有哪些功能,收钱、找钱、送出饮料,它面向的是整个过程。
    三、C++基本结构
    众所周知,每一门语言都有自己的基本结构,下面让我来介绍一下C++的基本结构吧!

    #include <iostream>
    using namespace std;
     
    // main() 是程序开始执行的地方
     
    int main()
    {
       cout << "Hello World"; // 输出 Hello World
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • C++ 语言定义了一些头文件,这些头文件包含了程序中必需的或有用的信息。上面这段程序中,包含了头文件 。
    • 下一行 using namespace std; 告诉编译器使用 std 命名空间。命名空间是 C++ 中一个相对新的概念
    • 下一行 // main() 是程序开始执行的地方 是一个单行注释。单行注释以 // 开头,在行末结束。
    • 下一行 int main() 是主函数,程序从这里开始执行。
    • 下一行 cout << “Hello World”; 会在屏幕上显示消息 “Hello World”。
    • 下一行 return 0; 终止 main( )函数,并向调用进程返回值 0。

    四、变量
    1、什么是变量:变量名称本身就是一个指针指向你一个空间用来存储你的数据,其作用就是给一段指定的内存起名,方便我们操作这段内存。
    2、变量创建语法:数据类型 变量名 = 变量初始值
    3、示例

    #include <iostream>
    using namespace std;
    int main() {
        int a=10;
        cout<< a<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    规定所有用到的变量要先定义后使用,编译器处理起来比较方便,不会有歧义。因为 C++ 里面,相同名字的变量在不同的作用域里面,是可以重复声明的,而一个{}就是一个作用域。
    五、常量
    1、作用:用于记录程序中不可更改的数据
    2、定义方式:
    <1> #define 宏常量 #define 常量名 常量值 通常在文件上方定义
    <2> const修饰的变量 const 数据类型 常量名 = 常量值
    3、示例

    #include <iostream>
    #define day 7
    using namespace  std;
    int main() {
       const int time=24;
    cout<<"一周有"<<day<<"天"<<endl;
    cout<<"一天有"<<time<<"小时"<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    五、关键字
    作用:关键字是C++中预先保留的单词,也称标识符。

    • 在定义变量或者常量时,不得使用关键字,否则会产生歧义
      在这里插入图片描述
      六、标识符命名规则
      作用:C++规定标识符(变量、常量)命名时,有一套自己的规则
    • 标识符不能是关键字
    • 标识符只能由字母、数字以及下划线组成
    • 第一个字符必须是字母或下划线
    • 标识符中的字母区分大小写
      给标识符命名时,一定要做到见明知意
      七、数据类型
      存在意义:给变量分配合适的内存空间。
      在这里插入图片描述

    1、整型
    在这里插入图片描述
    2、sizeof关键字
    <1>作用:利用sizeof关键字可以统计数据类型所占内存大小
    <2>语法:sizeof( 数据类型 / 变量 )
    <3>

    #include <iostream>
    using namespace std;
    int main(){
    
        cout <<"short类型所占空间"<<sizeof(short)<<endl;
        cout <<"int类型所占空间"<<sizeof(int)<<endl;
        cout <<"long类型所占空间"<<sizeof(long)<<endl;
        cout <<"long long类型所占空间"<<sizeof(long long)<<endl;
        cout <<"float类型所占空间"<<sizeof(float)<<endl;
        cout <<"double类型所占空间"<<sizeof(double)<<endl;
        cout <<"long double类型所占空间"<<sizeof(long double)<<endl;
        cout <<"char类型所占空间"<<sizeof(char)<<endl;
        cout <<"unsigned char类型所占空间"<<sizeof(unsigned char)<<endl;
        cout <<"unsigned short类型所占空间"<<sizeof(unsigned short)<<endl;
        cout <<"unsigned int类型所占空间"<<sizeof(unsigned int)<<endl;
        cout <<"unsigned long类型所占空间"<<sizeof(unsigned long)<<endl;
        cout <<"unsigned long long类型所占空间"<<sizeof(unsigned long long)<<endl;
        cout <<"bool类型所占空间"<<sizeof(bool)<<endl;
    
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3、浮点型
    <1>作用:用于表示小数
    <2>分类:浮点型变量分为两种:单精度(float)和双精度(double)

    数据类型占用空间有效数字范围
    float4字节7位有效数字
    double8字节15~16位有效数字

    <3>示例

    #include<iostream>
    using namespace std;
    int main(){
        float f1=3.14f;//不加f系统会认为是double类型的
        cout<<"f1="<<f1<<endl;
        double dl=3.14;
        cout<<"dl="<<dl<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、字符型
    <1>作用:字符型变量用于显示单个字符
    <2>语法:char ch='a'

    注意:在显示字符型变量时,用单引号将字符括起来

    • c/c++中字符型变量只占用一个字节
    • 字符型变量并不是把字符本身到内存中存储,而是将对应的ASCII编码放入存储单元

    <3>示例

    #include<iostream>
    using namespace std;
    int main(){
        char ch='a';
        cout <<ch<<endl;
        cout<<sizeof(char)<<endl;
        cout<<(int)ch<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    5、字符串型
    <1>作用:用于表示一串字符
    <2>语法:string 变量名 =“字符串值”
    <3>示例:

    #include <iostream>
    #include <string>
    using namespace std;
    int main(){
        string str="adfg";
        cout<<str<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    6、布尔类型
    <1>作用:布尔数据类型代表真或假的值
    bool类型只有两个值:

    • true --真(本质是1)
    • false–假(本质是0)

    bool类型只占一个字节
    <2>示例:

    #include<iostream>
    using namespace std;
    int main(){
        bool flag=true;
        cout<<flag<<endl;
        cout<<sizeof(flag)<<endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    八、运算符
    1、算术运算符
    <1>作用:用于处理四则运算
    <2>分类
    在这里插入图片描述

    #include<iostream>
    using namespace std;
    int main(){
        int a1=10;
        int b1=3;
        double d1=3.14;
        double d2=3.1;
        cout<<a1+b1<<endl;
        cout<<a1-b1<<endl;
        cout<<a1*b1<<endl;
        cout<<a1/b1<<endl;
        cout<<a1%b1<<endl;
        //cout<<d1%d2<<endl;  //必须有整数
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2、逻辑运算符
    <1>作用:进行逻辑运算
    在这里插入图片描述
    <2>示例:

    #include <iostream>
    using namespace std;
     
    int main()
    {
       int a = 5;
       int b = 20;
       int c ;
     
       if ( a && b )
       {
          cout << "Line 1 - 条件为真"<< endl ;
       }
       if ( a || b )
       {
          cout << "Line 2 - 条件为真"<< endl ;
       }
       /* 改变 a 和 b 的值 */
       a = 0;
       b = 10;
       if ( a && b )
       {
          cout << "Line 3 - 条件为真"<< endl ;
       }
       else
       {
          cout << "Line 4 - 条件不为真"<< endl ;
       }
       if ( !(a && b) )
       {
          cout << "Line 5 - 条件为真"<< endl ;
       }
       return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    九、程序流程结构
    C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构

    • 顺序结构:程序按顺序执行,不发生跳转
    • 选择结构:依赖条件是否满足,有选择的执行相应功能
    • 循环结构:依据条件是否满足,循环多次执行某段代码

    顺序结构比较简单,在此就不多做赘述
    1、选择结构
    <1>作用:执行满足条件的语句
    <2>格式:

    • 单行格式if语句
    • 多行格式if语句
    • 多条件if语句
      <4>练习题:
      这里推荐大家去练习洛谷的算法题,题目如下:
      输入一个年份(大于 1582 的整数 ),判断这一年是否是闰年,如果是输出 1,否则输出 0。
      解答:
    #include <iostream>
    using namespace std;
    int main() {
    int year;
    cin>>year;
        if ((year%4==0&&year%100!=0)||year%400==0){
            cout<<"1";
        }
        else cout<<"0";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2、循环结构
    <1>作用:满足循环条件,执行循环语句
    <2>分类:
    ①while语句:while(循环条件){循环语句}

    #include <iostream>
    using namespace std;
    int main() {
        int n;
        cin>>n;
    while(n--){
        cout<<n<<endl;
    }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    ②for语句:for(起始表达式;条件表达式;末尾循环体){循环语句;}

    #include <iostream>
    using namespace std;
    int main() {
        int n;
        cin>>n;
        for (int i = 0; i < n; i++) {
            cout<<i<<endl;
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    <3>解释:只要循环条件位置,就执行循环语句。
    <4>两大语句break、continue
    break
    ①作用:终止循环
    ②使用时机:

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

    continue
    ①作用:在循环语句中,跳过本次循环中余下的语句,继续执行下一次循环
    ②示例:

    #include <iostream>
    using namespace std;
    int main() {
        int n;
        cin>>n;
        for (int i = 0; i < n; ++i){
            if(i%2==0){
                continue;
            }
            cout<<i<<endl;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    十、数组
    1、概述:所谓数组,就是一个集合,里面存放了相同类型的数据元素
    2、特点:<1>数组中每个数据元素都是相同的数据类型;<2>数组是由连续内存位置组成的
    3、语法:数据类型 数组名[]={值1,值2};
    4、数组名
    <1>用途:①可以统计整个数组在内存中的长度②可以获取数组在内存中的首地址
    <2>示例

    #include <iostream>
    using namespace std;
    int main(){
        int arr[10]={1,2,3,4,5,6,7,8,9,10};
        cout<<sizeof(arr)<<endl;//整个数组所占内存空间
        cout<< sizeof(arr[0])<<endl;//每个元素所占内存空间
        cout<<sizeof(arr)/sizeof(arr[0])<<endl;//数组长度
        cout<<(long long)arr<<endl;//数组的首地址
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    vite项目启动use `--host` to expose
    TikTok搬运视频怎么做,搬运怎样的视频最好
    计算机毕业设计Java社交物联网的服务搜索系统(源码+系统+mysql数据库+lw文档)
    C++ day6
    『ChatGPT is bullshit』
    typeScript简单封装axios
    【独家专访】“数网”同防筑牢屏障——新型电力系统网络安全保障体系需加快调整
    leveldb-FilterBlock实现
    用《斗破苍穹》的视角打开C#委托2 委托链 / 泛型委托 / GetInvocationList
    Vue复习7:组件化编程,关于VueComponent,非单文件组件,单文件组件
  • 原文地址:https://blog.csdn.net/weixin_53549425/article/details/125365567