2020 重学C++ 重构你的C++知识体系笔记

Coding Alan 4年前 (2020-05-18) 3874次浏览 0个评论 扫描二维码

2020 重学C++ 重构你的C++知识体系笔记

VS Code 配置

在插件中搜索 C/C++安装

command+shift+p打开命令窗口,输入 edit选择C/Cpp: Edit Configurations生成c_cpp_properties.json

在命令行中输入gcc -v -E -x c++ -,将输出结果中的路径加到c_cpp_properties.json的includePath配置中,这样就不会出现再出现如下的报错

C++语言数据类型

类型范围
char1 个字节-128 到 127 或者 0 到 255
unsigned char1 个字节0 到 255
signed char1 个字节-128 到 127
int4 个字节-2147483648 到 2147483647
unsigned int4 个字节0 到 4294967295
signed int4 个字节-2147483648 到 2147483647
short int2 个字节-32768 到 32767
unsigned short int2 个字节0 到 65,535
signed short int2 个字节-32768 到 32767
long int4 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int8 个字节0 到 18,446,744,073,709,551,615
float4 个字节精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double8 个字节双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long double16 个字节长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t2 或 4 个字节1 个宽字符

常见数据类型的定义

C++关键字、保留字

asmelsenewthis
autoenumoperatorthrow
boolexplicitprivatetrue
breakexportprotectedtry
caseexternpublictypedef
catchfalseregistertypeid
charfloatreinterpret_casttypename
classforreturnunion
constfriendshortunsigned
const_castgotosignedusing
continueifsizeofvirtual
defaultinlinestaticvoid
deleteintstatic_castvolatile
dolongstructwchar_t
doublemutableswitchwhile
dynamic_castnamespacetemplate

C++转义序列的编码

字符名称ASCII 符号C++代码十进制 ASCII 码十六进制 ASCII 码
换行符 NL(LF)\n100xA
水平制表符HT\t90x9
垂直制表符VT\v110xB
退格BS\b80x8
回车CR\r130xD
振铃BEL\a70x7
反斜杠\\\920x5C
问号?\?630x3F
单引号'\'390x27
双引号"\"340x22

欧拉公式

$$e^{\pi i} + 1 = 0$$

无符号数的编码
用一个函数 B2Uw(Binary to Unsigned的缩写,长度为 w)来表示:

$$B2U_w(\vec{x})\doteq \sum_{i=0}^{w-1}{x_i2^i}$$

例:

$$B2U_4([0001]) = 0\cdot2^3 + 0\cdot2^2 + 0\cdot2^1 + 1\cdot2^0$$

有符号数的补码
用函数 B2Tw(Binary to Two’s-complement 的缩写,长度为 w)来表示:

$$B2T_w(\vec{x})\doteq -x_{w-1}2^{w-1} + \sum_{i=0}^{w-2}{x_i2^i}$$

例:

$$B2T_4([0001]) = -0\cdot2^3 + 0\cdot2^2 + 0\cdot2^1 + 1\cdot2^0$$

字节序

  • Big Endian
  • Little Endian

实际使用中应作用 strnlen_s, strcp_s, strncpy_s, strcat_s 这类安全 API,防止缓存区溢出等问题

2020 重学C++ 重构你的C++知识体系笔记

C++的智能指针

  • auto_ptr(已弃用)
  • unique_ptr
  • shared_ptr
  • weak_ptr

结构体修改默认空间占用编译选项

C++11字符串字面值

设计模式 GoF(Gang of Four)

内存分区模型

  • 代码区:存放函数体的二进制代码,由操作系统管理
  • 全局区:存放全局变量、静态变量以及常量
  • 栈区:由编译器自动分配释放,存放函数参数值、局部变量等
  • 堆区:由程序员分配、翻译,若程序员不翻译,程序结束时由操作系统回收

推荐书籍:

入门:

《C++ Primer》 Stanley B.Lippman

最佳实践:

《C++高质量编程》 林锐

《Effective C++》侯捷(译)

《More Effective C++》侯捷(译)

《Effective STL》潘爱民(译)

《The C++ Programming Language》 Bjarne Stroustrup

深入:

《STL 源码剖析》侯捷

《COM 本质论》Don Box

《Exeptional C++》Addsion. Wesley

《Inside the C++ Object Model》Stanley B.Lippman

《The Design and Evolution of C++》Bjarne Stroustrup

 

Source Insight

http://download.qt.io/official_releases/qt/

一段说明运算符重载、友元函数、类、构造函数、析构函数、指针、引用、浅拷贝、深拷贝等内容的示例代码:

 

常见问题

1、 error: non-aggregate type ‘vector<int>’ cannot be initialized with an initializer list

安装C/C++ Clang Command Adapter,点击Code->Preferences->Settings, 搜索code-runner.executorMap,加入如下配置:

其它知识

2020 重学C++ 重构你的C++知识体系笔记

编译:Tokenization -> Syntax analysis -> Semantic analysis -> Intermediate code generation -> Optimization -> Machine code generation

Google测试框架:https://github.com/google/googletest

 

喜欢 (2)
[]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址