2.c++基础语法

文章目录

  • 1.c++ 程序结构
    • 关键字
    • 标识符、操作符、标点
    • 预处理指令
    • 注释
    • main 主函数
    • 命名空间
  • 2.c++ 变量和常量
    • 变量
  • 3.c++ 数组和容器
  • 4.c++ 程序流程
  • 5.c++字符和字符串

1.c++ 程序结构

在这里插入图片描述

关键字

关键字事程序保留的,程序员不能使用,c++的常见关键字如下图:
在这里插入图片描述

标识符、操作符、标点

在这里插入图片描述
:: 这个也是操作符,不是标点。

预处理指令

在这里插入图片描述

注释

在这里插入图片描述

main 主函数

一个程序只能有一个入口。
在这里插入图片描述
代码练手:

#include<iostream>

using namespace std;

int main(){
    cout << "hello" << endl;
    return 0;
}

代码练手

#include<iostream>
using namespace std;

//argc 参数数量
//argv 参数列表
int main(int argc,char** argv){
    cout << "参数数量:" << argc << endl;
    cout << "==== 参数列表 =====" << endl;
    for (int i = 0;i < argc; i ++){
        cout << "参数:" <<argv[i] << endl;
    } 
    return 0;
}

命名空间

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

想不发生冲突,还是直接std::cout,比较好一点。

2.c++ 变量和常量

在这里插入图片描述

变量

在这里插入图片描述
变量名实际上就是你的内存地址。只不过对于不同的对象就是在堆上还是在栈上。因为是一块地址,所以是可以变化的,想放什么就放什么。

在这里插入图片描述
在这里插入图片描述
变量的初始化。
在这里插入图片描述
代码演示:

#include<iostream>
using namespace std;

int main(){
    int age;
    cout << "age is : " << age << endl;
    return 0;
}

如果变量没有初始化,会触发警告。如下图。警告不影响运行,但是最好都要做初始化。

在这里插入图片描述
代码练手,计算房子面积:

#include<iostream>
using namespace std;

int main(){
    int room_width {0};
    cout << "请输入房间宽度:" ;
    cin >> room_width;

    int room_height {0};
    cout << "请输入房间高度" ;
    cin >> room_height;
    

    cout << "=====================" << endl;
    cout << "房间的面积是:" << room_height * room_width << endl;
    return 0;
}

数据基本类型都有哪些:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
附上ASCII编码表:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

变量声明的时候,因为变量是有大小限制的,如果声明的超过了范围,使用花括号,就会报错。如下图,这也是花括号声明的好处之一。

在这里插入图片描述
代码练手:

#include<iostream>
using namespace std;

int main(){
    cout << "=====字符型" << endl;
    char my_char {'f'};
    cout << "字符:" << my_char << endl;


    cout << "=====短整型" << endl;
    short my_short {59};
    cout << "短整型:" << my_short << endl;

    // cout << "======浮点数======" << endl;
    // 不会报错
    short overflow_num_1 = 32769;
    cout << "短整型溢出" << overflow_num_1 << endl;

    // short overflow_num_2 {32768}; // 会报错,无法编译
    // cout <<  "短整型溢出" << overflow_num_2 << endl;

    cout << "########int#######" << endl;
    int my_height {182};
    cout << "int类型:" << my_height << endl;


    long peolple {10360000};
    cout << "杭州人数:" << peolple << endl;


    long long people_in_world {80'0000'0000}; // 方便阅读 c++14标准
    cout << "全世界的人数:" << people_in_world << endl;

    //浮点型
    cout << "=======浮点型=======" << endl;
    float book_price {24.21f};
    cout << "书的价格" << book_price << endl;

    double pi {3.14159};
    cout << "圆周率:" << pi << endl;

    cout << "#######bool#########" << endl;

    bool add_to_cart {true};
    cout << boolalpha; // 以bool值的形式输出
    cout << "是否加入购物车:" << add_to_cart << endl;

    return 0;
}
  • sizeof 和climits
  • 在这里插入图片描述
    代码示例:
#include<iostream>
using namespace std;
// 想要使用看大小的函数,需要引入climits
#include<climits>

int main(){


    cout << "char:" << sizeof(char) << endl;
    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 << " min and max" << endl;
    cout << "char min:" << CHAR_MIN << ",max:" <<CHAR_MAX << endl;
    cout << "short min:" << SHRT_MIN << ",max:" <<SHRT_MAX << endl;
    cout << "long long min:" << LLONG_MIN << ",max:" << LLONG_MAX << endl;

    cout << "使用变量名称看大小" << endl;
    int age {11};
    cout << "age is : " << sizeof age << endl;
    cout << "age is : " << sizeof(age) << endl;

    double salary {123123.44};

    cout << "salary is : " << sizeof(salary) << endl;


    return 0;
}
  • 常量

在这里插入图片描述

代码练手:


#include<iostream>
using namespace std;

int main(){
    const double pi {3.1415926};
    cout << "请输入半径:" ;
    double radius {};
    cin >> radius;
    cout << "圆的面积:" << pi * radius * radius << endl;
}

3.c++ 数组和容器

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注意:访问的超出范围会报错。
在这里插入图片描述

代码演示:

#include<iostream>
using namespace std;


int maopao(int array[]){
    // 写一段冒泡排序的代码


    return 0;
} 

int main(){

    char vowels[] {'a','e'};

    cout << "第1个元素:" << vowels[0] << endl;
    cout << "第2个元素:" << vowels[1] << endl;

    // cin >> vowels[2];

    // cout << "第3个元素:" << vowels[2] << endl;

    double hi_tmps [] {100,101,102,103};

    hi_tmps[0]= 200;

    cout << "第五天的温度:" << hi_tmps[4] << endl; // 放到到一个未知的地址空间,数据每次都不同

    int student_score[5];

    cout << "第一个学生的成绩是:" << student_score[0] << endl;
    cout << "第二个学生的成绩是:" << student_score[1] << endl;
    cout << "第三个学生的成绩是:" << student_score[2] << endl;
    
    cout << endl;

    cin >> student_score[0];
    cin >> student_score[1];
    cin >> student_score[2];
    cin >> student_score[3];
    cin >> student_score[4];

    cout << "第一个学生的成绩是:" << student_score[0] << endl;
    cout << "第二个学生的成绩是:" << student_score[1] << endl;
    cout << "第三个学生的成绩是:" << student_score[2] << endl;
    cout << "第四个学生的成绩是:" << student_score[3] << endl;
    cout << "第五个学生的成绩是:" << student_score[4] << endl;

    cout << "数组的名称是:" << student_score << endl; // 数组的名称是数组的首地址
    cout << "数组的名称是:" << *student_score << endl; // 直接用指针指一下,输出的就是第一个数值


    cout << "定义一个二维数组" << endl;

    int array_2d[3][4] {
        {1,2,3,4},
        {5,6,7,8},
        {9,10,11,12}
    };

    cout << "第一行第一列的值是:" << array_2d[0][0] << endl;
    cout << "第3行第2列的值是:" << array_2d[2][1] << endl;


    cout << endl;

    return 0;
}

  • 容器
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

代码练手:

#include<iostream>
#include<vector>

using namespace std;

int main(){

    // vector<char> vowels;
    // vector<char> vowels2 (5);
    // cout << "第一个元素是:" << vowels2[0] << "\n";


    // vector<char> vowels {'a', 'e', 'i', 'o', 'u'};
    // cout << "第一个元素是:" << vowels[0] << "\n";
    // cout << "第二个元素是:" << vowels[1] << "\n";

    // vector<int> test_scores (3);
    // cout << "第一个元素是:" << test_scores[0] << "\n";

    // vector<int> students_socre(3,100);
    // cout << "第一个元素是:" << students_socre[0] << "\n";

    // vector<int> students_socre {100, 98, 89};
    // cout << "array方式访问:" << endl;
    // cout << "三个元素分别是:" << students_socre[0] << " " << students_socre[1] << " " << students_socre[2] << endl;

    // cout<< "======================" << endl;
    // cout << "vector方式访问:" << endl;
    // cout << "三个元素是:" << students_socre.at(0) << " " << students_socre.at(1) << " " << students_socre.at(2) << endl;

    // // 添加元素
    // cout << "======================" << endl;
    // int add_new_value {0};
    // cout << "请输入一个新的值:";
    // cin >> add_new_value;

    // students_socre.push_back(add_new_value); // 添加到最后一个元素

    // cout << "在添加一个新的值:" ;
    // cin >> add_new_value;
    // students_socre.push_back(add_new_value); // 添加到最后一个元素

    // cout << "添加后一共有多少个元素 :" << students_socre.size() << endl;
    // cout << "第一个元素是:" << students_socre.at(0) << endl;
    // cout << "第二个元素是:" << students_socre.at(1) << endl;
    // cout << "第三个元素是:" << students_socre.at(2) << endl;
    // cout << "第四个元素是:" << students_socre.at(3) << endl;
    // cout << "最后一个元素是:" << students_socre.at(students_socre.size() - 1) << endl;

    // cout << "获取不存在的元素:" << students_socre.at(10) << endl; // 报错
    // cout << "获取不存在的元素:" << students_socre[10] << endl; // 不报错,显示0


    cout << "======================" << endl;
    vector<vector<int>> movie_ratings {
        {1, 2, 3, 4},
        {1, 2, 4, 4},
        {1, 3, 4, 5}
    };
    cout << "数组风格的第一个电影第一个评分是:" << movie_ratings[0][0] << endl;
    cout << "vector风格的第一个电影第一个评分是:" << movie_ratings.at(0).at(0) << endl;
    cout << "第三个电影的第四个评分是:" << movie_ratings.at(2).at(3) << endl;

    cout <<endl;

    return 0;
}

4.c++ 程序流程

在这里插入图片描述
在这里插入图片描述

if代码练手:

#include<iostream>
using namespace std;

int main(){
    int input_num {0};
    const int lower_limit {10};
    const int upper_limit {100};

    cout << "Enter a number: ";
    cin >> input_num;

    if(input_num > lower_limit){
        cout << "\nNumber is greater than or equal to " << lower_limit <<  ",大" << (input_num - lower_limit) <<endl;
    }

    if (input_num < upper_limit){
        cout << "\nNumber is less than or equal to " << upper_limit << ",小" << (upper_limit - input_num) << endl;
    }

    if (input_num > lower_limit && input_num < upper_limit){
        cout << "\nNumber is in range " << endl;
    }

    if (input_num == lower_limit || input_num == upper_limit){
        cout << "\nNumber is on the boundary" << endl;
    }
}   

if-esle代码练手

#include<iostream>
using namespace std;

int main(){
    int num {0};

    const int target_num {10};

    cout << "请输入一个数字:";
    cin >> num;

    if (num <= target_num){
        cout << "你输入的数字小于等于目标数字" << endl;
    }else{
        cout << "你输入的数字大于目标数字" << endl;
    }

    return 0;
}   

switch代码练手:


5.c++字符和字符串

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/156455.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

jupyter修改默认打开目录

当我们打开jupyter notebook&#xff08;不管用什么样的方式打开&#xff0c;使用菜单打开或者是命令行打开是一样的&#xff09;会在默认的浏览器中看到这样的界面、 但是每一台不同的电脑打开之后的界面是不同的&#xff0c;仔细观察就会发现&#xff0c;这里面现实的一些文件…

中心极限定理

中心极限定理是统计学中的一个基本定理&#xff0c;它描述了在满足一定条件的情况下&#xff0c;独立随机变量的均值的分布会在样本容量足够大时趋近于正态分布。中心极限定理为许多统计推断方法的合理性提供了理论基础。 中心极限定理有两种常见的表述&#xff1a;独立同分布…

【Python基础篇】字符串的拼接

博主&#xff1a;&#x1f44d;不许代码码上红 欢迎&#xff1a;&#x1f40b;点赞、收藏、关注、评论。 格言&#xff1a; 大鹏一日同风起&#xff0c;扶摇直上九万里。 文章目录 一 Python中的字符串拼接二 join函数拼接三 os.path.join函数拼接四 号拼接五 &#xff0c;号…

Git面经

Git八股文 第一章 git基础 1.1 什么是git git是一款免费的开源的分布式版本控制系统 1.2 为什么要使用git 为了保留之前的所有版本&#xff0c;方便回滚或修改 1.3 集中化版本控制系统和分布式版本控制系统的区别 集中化版本控制系统如svn&#xff0c;客户端连接到中央服…

金蝶云星空签出元数据提示“数据中心业务对象版本高于应用版本”

文章目录 数据中心业务对象版本高于应用版本签出元数据提示建议 数据中心业务对象版本高于应用版本 签出元数据提示 建议 每次签出元数据前&#xff0c;先获取最新的代码后再签出&#xff0c;如果还是提示&#xff0c;那就根据你的情况选择版本。

001.前置知识

1、硬件 我们知道&#xff0c;组成计算机的硬件主要有“主机”和“输入/输出设备”。 主机包括机箱、电源、主板、CPU&#xff08;Central Processing Unit&#xff0c;中央处理器&#xff09;、内存、显卡、声卡、网卡、 硬盘、光驱等。输入/输出设备包括显示器、键盘、鼠标…

什么是BT种子!磁力链接又是如何工作的?

目录 一.什么是BT&#xff1f;1.BT简介&#xff1a;1.1.BT是目前最热门的下载方式之一1.2.BT服务器是通过一种传销的方式来实现文件共享的 2.小知识&#xff1a;2.1.你知道吗BT下载和常规下载到底有哪些不同2.2.BT下载的灵魂&#xff1a;种子2.3.当下载结束后&#xff0c;如果未…

Python调用企微机器人: 发送常用格式汇总

企微接口文档 发送应用消息 - 接口文档 - 企业微信开发者中心 发送格式 应用支持推送文本、图片、视频、文件、图文等类型。 ~~~以下列举常用格式 示例~~~ 1.发送文本 代码如下&#xff1a; def sendtxt_robotmsg(self):# 正式keywx_key "xx"wx_webhookurl htt…

K8s 命令行

前言&#xff1a;关于k8s 与 docker Docker和Kubernetes&#xff08;通常简称为K8s&#xff09;是两个在容器化应用程序方面非常流行的开源工具。 Docker: Docker 是一种轻量级的容器化平台&#xff0c;允许开发者将应用程序及其所有依赖项打包到一个称为容器的可移植容器中…

【C++】AVL树(动图详解)

文章目录 一、前言二、AVL树的概念&#xff08;引入bf&#xff09;三、AVL节点树的定义四、AVL树的基本框架五、AVL树的旋转5.1左单旋&#xff08;新节点插入较高右子树的右侧---右右&#xff1a;左单旋&#xff09;例一&#xff08;h0&#xff09;例二&#xff08;h1&#xff…

C/C++算法-----------------------双指针详解技巧及例题

双指针 基本介绍降低时间复杂度降低时间复杂度例题 验证回文串判断是否为环反转链表总结 基本介绍 双指针&#xff08;two poinnters&#xff09;实际上是一种算法编程里的一种思想&#xff0c;它更像是一种编程思想&#xff0c;提供看非常高的算法效率&#xff0c;一般来说双…

FL Studio水果软件2025中文破解版注册机

你是否体验过Tomorrowland现场万人蹦迪的的激情&#xff1f;又是否加入过“死墙&#xff08;Mosh pit&#xff1a;一种Bass音乐节常有的娱乐方式&#xff09;”的狂欢盛宴&#xff1f;随着时代发展&#xff0c;以电子音乐为代表的数字音乐已然象征着时尚与潮流。在这股风靡全球…

fiddler展示接口的响应时间

最近项目组迁移了一个新项目&#xff0c;想对比迁移前后访问菜单的响应时间是否有变化&#xff0c;因为没需求文档&#xff0c;所以只有靠fiddler一个个的抓接口来看&#xff0c;开发经理想要看具体每个接口耗时&#xff0c;虽然点击接口&#xff0c;在页面上也能看到接口响应时…

HackTheBox-Starting Point--Tier 2---Markup

文章目录 一 Markup测试过程1.1 打点1.2 权限获取1.3 权限升级 二 题目 一 Markup测试过程 1.1 打点 1.端口扫描 nmap -A -Pn -sC 10.129.95.1922.访问web网站&#xff0c;登录口爆破发现存在弱口令admin&#xff1a;password 3.抓包&#xff0c;发现请求体是XML格式 4.尝试使…

购买阿里云服务器需要多少钱?活动价3000元-5000元的阿里云服务器汇总

购买阿里云服务器需要多少钱&#xff1f;如果我们只有3000元-5000元的预算可以购买什么实例规格和配置的阿里云服务器呢&#xff1f;因为阿里云服务器价格是由实例规格、配置、带宽等众多配置决定的&#xff0c;所以&#xff0c;目前阿里云活动中的价格在3000元-5000元的云服务…

购买阿里云服务器需要多少钱?活动价2000元-3000元的阿里云服务器汇总

购买阿里云服务器需要多少钱&#xff1f;如果我们只有2000元-3000元的预算可以购买什么实例规格和配置的阿里云服务器呢&#xff1f;因为阿里云服务器价格是由实例规格、配置、带宽等众多配置决定的&#xff0c;所以&#xff0c;目前阿里云活动中的价格在2000元-3000元的云服务…

一文揭秘共享wifi二维码项目推广技巧!

随着无线网络的普及和移动互联网的快速发展&#xff0c;共享WiFi已成为人们生活中不可或缺的一部分。共享WiFi二维码项目作为一个独具创意的共享项目&#xff0c;将二维码推广与共享WiFi相结合&#xff0c;不仅可以提升品牌曝光度&#xff0c;还能为用户提供便捷的上网体验。那…

什么是圆锥的准线?

定曲线C叫做锥面的准线&#xff0c;构成曲面的每一条直线叫做母线。

RedHat公司及红帽认证介绍丨红帽认证等级介绍

RedHat公司及红帽认证介绍 红帽公司成立工1993年&#xff0c;是全球首家收入超10亿美元的开源公司&#xff0c;总部位于美国&#xff0c;分支机构遍布全球。红帽公司作头全球领先的开源和Linux系统提供商&#xff0c;其产品已被业界广泛认可并使用&#xff0c;尤其是RHEL系统在…

CentOs 7 PHP安装和配置

目录 1 安装epel源 2 安装REMI源 3 安装yum源管理工具 4 安装PHP7.3 5 启动php服务 6 设置PHP 6.1 查找安装包 6.2 查找PHP安装位置 6.3 查找php配置文件位置 6.4 配置PHP 6.5 设置快捷命令 6.6 查看php版本 6.7 更新php 1 安装epel源 yum -y install epel-release 2 安…
最新文章