第一个C++程序

#include <iostream> // 使用C++提供的流库
using namespace std; // 使用命名空间,为什么必须是std? int main() {
// 根据 iostream 提供的函数 打印
std::cout << "hello c++" << std::endl; // 使用命名空间引用函数?
// 也可以不使用命名空间 cout << "hello c++" << endl;
return 0;
}

变量、常量

给指定的一段内存空间起了一个别名,方便操作这个内存空间

内存空间的指定由操作系统完成

#include <iostream>

int main() {
int varA = 100;
cout << varA << endl;
return 0;
}

常量可以使用宏,或者是const

#include <iostream>
using namespace std;
#define DAY 1 int main() {
const double PI = 3.14;
cout << DAY << endl;
cout << PI << endl;
// printf("PI need format %.2f\n", PI); // 原始的C写法
return 0;
}

似乎printf函数已经包含在iostream里面了

C++整形

#include <iostream>
#include <cmath> using namespace std; int main() {
short n1 = pow(2, 15) - 1; // 2^15-1
int n2 = pow(2, 31) - 1; // 2^31-1
long n3 = pow(2, 31) - 1; // 2^31-1
long long n4 = pow(2, 61) - 1; // 2^61-1 cout << "short maxValue -> " << n1 << endl;
cout << "int maxValue -> " << n2 << endl;
cout << "long maxValue -> " << n3 << endl;
cout << "long long maxValue -> " << n4 << endl;
return 0;
}

SIZEOF 运算符

获取该变量或者是数据类型的字节占用大小

#include <iostream>
#include <cmath> using namespace std; int main() {
int temp = 100;
cout << sizeof(temp) << endl;
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(long long) << endl;
return 0;
}

实型【浮点型】

#include <iostream>
#include <cmath> using namespace std; int main() {
// 使用float 需要后缀f
float f = 10.002f;
double d = 3.1415926;
cout << sizeof(float) << sizeof(f) << endl;
cout << sizeof(double) << sizeof(d) << endl;
return 0;
}  

字符串型

字节占用1,存储的是对应的ASCII编码

C++支持原生的C字符数组形式创建

也可以使用自己的string

#include <iostream>
using namespace std; int main() { char str1[] = "hello c style string";
string str2 = "hello c++ string"; cout << str1 << endl;
cout << str2 << endl; return 0;
}

布尔类型

最常用的TRUE & FALSE 类型在C++终于有正式的数据类型了

C中的定义是 0 false 非0的正数即 true,字节占用1,可以使用字面量整数写入流程控制中

#include <iostream>
using namespace std; int main() {
bool flag = true;
if (flag) {
cout << sizeof(bool) << endl;
}
return 0;
}

输入函数

#include <iostream>
using namespace std; int main() {
string aaa; cin >> aaa; cout << aaa << endl;
return 0;
}

GOTO语句

#include <iostream>
using namespace std; int main() { cout << "executing program in line1 ..." << endl;
cout << "executing program in line2 ..." << endl;
cout << "executing program in line3 ..." << endl;
cout << "executing program in line4 ..." << endl;
goto FLAG; FLAG2:
cout << "executing program in line5 ..." << endl;
cout << "executing program in line6 ..." << endl; FLAG:
cout << "executing program in line7 ..." << endl;
cout << "executing program in line8 ..." << endl; return 0;
}

直接跳转到指定的标记行执行,不建议使用,可读性差,逻辑混乱

C++ 指针

32位操作系统的任意数据类型的指针类型都是占用4字节大小

64位操作系统则占用8个字节

#include <iostream>
using namespace std; int main() { int a = 100;
double b = 3.14; int * integerPointer = &a;
double * doublePointer = &b; cout << sizeof(integerPointer) << endl;
cout << sizeof(doublePointer) << endl; return 0;
}

C++ NEW操作符

之前的C中我们需要使用malloc函数在堆区中申请内存空间进行使用,之后还需要释放该空间

在C++ 中我们可以使用new操作符实现

#include <iostream>
using namespace std; // 基本语法
void newUsage() {
// new 返回的是一个该数据类型的指针
int * pointer = new int(10); // 获取数据就需要取引 *p // 堆区的数据由开发者自己管理,创建和释放
cout << *pointer << endl; // 我们需要释放该内存空间,则使用 delete关键字 (C语言中使用free函数,另外还需要自己做一些安全处理)
delete pointer; cout << *pointer << endl;
}
// 创建堆区数组
void newArray() {
int * arrPointer = new int[10]; for (int i = 0; i < 10; ++i) {
arrPointer[i] = i + 100;
cout << *(arrPointer + i)/* arrPointer[i] */ << endl;
}
// 释放数组还需要 这样声明标识 ,否则只释放首个元素的内存空间
delete [] arrPointer;
} int main() {
newUsage();
return 0;
}

C++ 引用

引用和指针的作用相似,对指针操作习惯反而对引用难理解

作用是对变量起别名进行使用

#include <iostream>
using namespace std; void referenceUsage() {
int a = 100;
cout << a << endl; // int &refA; 不可以只声明不初始化
int &refA = a;
refA = 200; // *&refA = 200;
cout << refA << endl; refA = 300; // 引用允许重新赋值 // 但是不允许改变引用的变量
} int main() {
referenceUsage();
return 0;
}

显而易见使用引用的好出就在于函数形参的问题上

我们希望传递普通变量,进行交换,在C语言中保证改变的变量是实参而非形参

就必须使用指针作为形参传递,使用引用就可以很好的解决这个问题,函数调用时可以很好的表达出我们希望做出的逻辑

#include <iostream>
using namespace std; void referenceSwap(int &n1, int &n2) {
int temp = n1;
n1 = n2;
n2 = temp;
}
void show() {
int a = 10;
int b = 20;
cout << a << " - " << b << endl;
referenceSwap(a, b);
cout << a << " - " << b << endl;
} int main() {
show();
return 0;
}

用来做函数的返回值和被赋值?

#include <iostream>
using namespace std; int& returnValue() { // 不要返回局部变量,该变量在函数调用结束后出栈销毁
int a = 100;
return a;
}
int& returnValue2() { // 静态变量在程序结束后释放
static int a = 100;
return a;
} void essenceOfReference() {
// 引用的本质实际上就是 【指针常量】
int a = 100;
int * const reference = &a;
*reference = a; int b = 200;
// reference = &b; 不可更改指向 } int main() {
// int &a = returnValue();
// cout << a << endl; // 不要这样使用 returnValue2() = 300; // 作为左值进行赋值的意义?
return 0;
}

引用的本质

void essenceOfReference() {
// 引用的本质实际上就是 【指针常量】
int a = 100;
int * const reference = &a;
*reference = a; int b = 200;
// reference = &b; 不可更改指向
}

常量引用,应用的场景,固定形参

void constantFormalParameters(const int &n1, const int &n2) {
// 上述的形参不可以在函数体中发生写入,只允许读取
}

【C++】01的更多相关文章

  1. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  2. 【hdu3080】01背包(容量10^7)

    [题意]n个物品,有wi和vi,组成若干个联通块,只能选取一个联通块,问得到m的价值时最小要多少空间(v).n<=50,v<=10^7 [题解] 先用并查集找出各个联通块. 这题主要就是v ...

  3. 【u115】&&【t031】 01迷宫

    01迷宫(maze01) Time Limit: 1 second Memory Limit: 128 MB [问题描述] 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相 ...

  4. 【C】 01 - 再学C语言

    “C语言还用再学吗?嵌入式工程师可是每天都在用它,大家早就烂熟于心,脱离语言这个层面了”.这样说不无道理,这门古老的语言以其简单的语法.自由的形式的而著称.使用C完成工作并不会造成太大困扰,所以很少有 ...

  5. 【算法】01分数规划 --- HNOI2009最小圈 & APIO2017商旅 & SDOI2017新生舞会

    01分数规划:通常的问法是:在一张有 \(n\) 个点,\(m\) 条边的有向图中,每一条边均有其价值 \(v\) 与其代价 \(w\):求在图中的一个环使得这个环上所有的路径的权值和与代价和的比率最 ...

  6. 【整理】01. Fiddler 杂记

    抓手机包步骤: Tools -- Fiddler Options -- Connections (默认)Fiddler listens on port:8888 (勾选)Allow remote co ...

  7. 【OracleDB】 01 概述和基本操作

    实例概念: Oracle有一个特殊的概念 Oracle数据库 = 数据库 + Oracle文件系统 + Oracle实例 实例处理Oracle的请求,调用文件系统 然后返回结果响应给客户端 单实例和多 ...

  8. 【learning】01分数规划

    问题描述 首先分数规划是一类决策性问题 一般形式是: \[ \lambda=\frac{f(x)}{g(x)} \] 其中\(f(x)\)和\(g(x)\)都是连续的实值函数,然后要求\(\lambd ...

  9. 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}

    P.S.又是一个抽时间学了2个小时的新东西......讲解在上半部分,题解在下半部分. 先说一下转的原文:http://www.cnblogs.com/perseawe/archive/2012/05 ...

  10. 【分享】01. Eclipse for PHP + phpStudy 搭建php开发环境

    配置php编译器 配置phpStudy服务器项目发布目录 修改hosts文件127.0.0.1      www.350zx.cn 新建项目 启动的你的phpStudy  

随机推荐

  1. ubuntu server 22.04 安装docker

    ubuntu server 22.04 安装docker 官方安装文档: https://docs.docker.com/engine/install/ubuntu/ 1.更新软件列表: sudo a ...

  2. C# yyyyMMddHHmmss 格式的日期转换

    C# yyyyMMddHHmmss 格式的日期转换 DateTime dtTimeEnd = DateTime.Now; if (!string.IsNullOrWhiteSpace(rspA.fin ...

  3. C#.NET HTTP Request 跳过自签名证书校验。

    public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain ...

  4. 一种复习flex布局的方法

    方法论 flex布局有多个属性,时常会忘记.我们复习的话,单纯看一些博客文章,不能直观的理解,也比较枯燥. 因此如果有一种用写代码闯关的方式来复习(学习)flex布局,那也许会更有意思. FLEXBO ...

  5. 关于编译告警 C4819 的完整解决方案 - The file contains a character that cannot be represented in the current code page (number). Save the file in Unicode format to prevent data loss.

    引言 今天迁移开发环境的时候遇到一个问题,同样的操作系统和 Visual Studio 版本,原始开发环境一切正常,但是迁移后 VS 出现了 C4819 告警,上网查了中文的一些博客,大部分涵盖几种解 ...

  6. 14-LNMP搭建

    介绍 LNMP: Linux + Nginx + Mysql/Mariadb + PHP 借助LNMP,我们就能搭建一个动态的网页. 安装Nginx 详细nginx教程:https://blog.cs ...

  7. LangGraph实战

    1.概述 前段时间LangChain发布了LangGraph,它引起了很多关注.LangGraph 的主要优势在于它能够实现循环工作流,这对于在 LLM 应用程序中模拟类似代理的行为至关重要.本篇博客 ...

  8. 【Mysql】 MysqlDump导表结构或数据

    mysqldump只导出表结构或只导出数据的实现方法 语法: 默认不带参数的导出,导出文本内容大概如下:创建数据库判断语句-删除表-创建表-锁表-禁用索引-插入数据-启用索引-解锁表. Usage: ...

  9. OpenWrt中的LuCi和Lua一些总结

    Lua.LuCi Lua是一种小巧的脚本语言,和Python一样,Lua脚本的运行需要Lua解释器: UCI(Unified Configuration Interface)是OpenWrt实现所有系 ...

  10. Python 潮流周刊#59:Polars 1.0 发布了,PyCon US 2024 演讲视频也发布了(摘要)

    本周刊由 Python猫 出品,精心筛选国内外的 250+ 信息源,为你挑选最值得分享的文章.教程.开源项目.软件工具.播客和视频.热门话题等内容.愿景:帮助所有读者精进 Python 技术,并增长职 ...