2.1 基本语法与类型
- 基本数据类型与常量
- 标识符、命名与关键字
const、引用与指针
- 作用域与命名空间
auto、枚举与类型别名
- 控制流与范围
for
2.2 函数
- 值传递、指针传递与引用传递
- 函数重载与默认参数
- 内联函数
- Lambda 表达式
- 函数对象
2.3 类与对象
struct与class
- 访问控制
- 构造函数与析构函数
- 成员初始化列表
- 静态成员与
this指针
- 运算符重载
- 拷贝与移动
2.4 字符串与输入输出
std::string常用操作
- 标准输入输出流
- 文本与二进制文件
- 流状态与缓冲区
Variables and Types
Built-in
- Boolean -
boolean- eithertrueorfalse
- Characters - alphabets and all the symbols. Defined using
char.
- Integers - whole numbers which can be both positive and negative. Defined using
int(4 bytes) orshort int(2 bytes) orlong int(8 bytes) based on the size of the numbers used.
- Floating point numbers - real numbers (numbers with fractions). Defined using
floatanddouble.
- Valueless using the
voidkeyword
- Wide character using the
wchar_tkeyword
Type Modifiers
The above types can be modified using the following type modifiers:
signed and unsigned short and longUser defined
- Structures -
structwill be explained later, in the Structures section.
- Classes -
classwill be covered later, in the Classes section
C++ allows an array of characters to define strings. It also provides an extensive
string library for manipulating strings and will be explained in the Strings section.Typdefs
Typedefs allow for creating new names (think of them as aliases) for existing types. Following is the simple syntax to define a new type using typedef:
Enumerated types
To create an enumeration requires the use of the keyword enum. The general form of an enumeration type is:
Above, the enum_name is the enumeration's type name. The list of names is comma separated.
For example, the following code defines an enumeration of colors called colors and the variable
a_colour of type color. Finally, a_colour is assigned the value "green".For loops
- legacy Regular
- "foreach-loop" C++ (since C++11)
Linked lists
define a linked list
Adding an item (to the end of the linked list)
- passing the address of a new code to
the next pointer of tail
- make tail pointer equal to new node

Iterating over a list
- use a
currentpointer that will keep track of the node we are currently printing.
- After printing the value of the node, we set the
currentpointer to the next node, and print again, until we've reached the end of the list
Insertion an item at the start of the list (pushing to the list)
- Create a new item and set its value
- Link the new item to point to the
headof the list
- Set the
headof the list to be our new item
Insertion at the End
The insertion of a node at the end of a linked list is the same as we have done in node creation function.
Insertion at Particular Position
The new node can be inserted between the
previous and current node by just performing two steps:- Pass the address of the new node in the next field of the previous node.
- Pass the address of the current node in the next field of the new node.
Deletion of the first item (popping from the linked list)
To pop a variable, we will need to perform three steps:
- Take the next item that the
headpoints to and save it
- Free the head item
- Set the head to be the next item that we've stored on the side
Deletion of the last item of the list
Removing the last item from a list is very similar to adding it to the end of the list, but with one big exception - since we have to change one item before the last item, we actually have to look two items ahead and see if the next item is the last one in the list:
Removing a specific item
Here is the algorithm:
- Iterate to the node before the node we wish to delete
- Save the node we wish to delete in a temporary pointer
- Set the previous node's next pointer to point to the node after the node we wish to delete
- Delete the node using the temporary pointer
removeByValue
方法2:
方法3:
Template Metaprogramming
function TemplateInitializer list
成员初始化列表,是从C++98开始就有的功能,是C++语言的基础特性之一。它允许在创建对象时为其成员变量进行初始化,可以在构造函数中显式调用。从C++11开始,成员初始化列表功能得到了一些扩展,比如可以使用花括号语法进行初始化,也可以使用默认的成员初始化方式等。
优点:
- 成员初始化列表在对象创建时直接初始化成员变量,而不是先创建成员变量,然后再为其赋值。这意味着使用成员初始化列表通常比在构造函数体内为成员变量赋值更高效。
- 对于const成员变量和引用类型的成员变量,必须使用成员初始化列表进行初始化,因为它们不能在构造函数体内被赋值。
Dynamic allocation new/delete
- new返回指定类型的内存指针,并自动计算所申请内存的大小。
- new在创建对象分配内存时会自动调用构造函数
Lambda Function
- particularly useful for short, one-time use functions, such as predicates in algorithms, callbacks, or event handlers.
- cleaner and more intuitive code
Example 1: Sorting with Lambda
Example 2: Capturing Local Variables
Example 3: Modifying Captured Variables
带参数构造和不带参数构造注意事项
默认存在不带参数构造
若添加带参数构造,需要重写不带参数构造,在cpp中
解释拷贝构造
tmp 属于栈中,跳出+运算符后进行赋值时,需要tmp所指向的对象。C++内部缺省会拷贝构造。tmp并未传递给等号运算符,而是构造了一个副本传过来。
但利用C++缺省的构造会造成问题,浅拷贝,深拷贝问题,比如定义了堆内存。
- 注意:=重载,报错:没有合适的复制构造函数
解释临时对象优化问题
解释等号运算符 =
C++会有默认操作,但需要增加一个自定义的运算符
解释前置和后置操作符 ++ —
解释标准输入输出IO重载
C++ 指针
pointers
new features compared with c pointer
- the operators
newanddeletetoallocateandde-allocatememory to a pointer
- A so called "void-pointer" is a pointer, which has
no data type assigned. It can hold an address of any type and can betypecastedto any other type.
点击查看更多
- In C, we can assign a void pointer to a non-void pointer without
using a cast tonon-void pointer. In C++, however, you have toexplicitlytype cast it.
点击查看更多
一个变量三个重要的信息::地址、存储信息、类型
指针变量是专门用来记录变量地址的变量,通过指针变量可以间接的访问(*)另一个变量的值C++原生指针
数组变量和指针变量区别
- 数组变量不可变,指向值可变
- 指针变量可变,指向的值可变与否取决于存储区域是否可变
左值和右值特点
- 左值单独分配了地址空间,可以去地址
- 右值指数据本身,不能取地址,也成为“
临时对象”,数组对象名也是一个右值
几种C++中的原始指针
一般类型 T*
指针数组(array of pointers)和数组指针(a pointer to an array)
int* a[4]; int (*b)[4]; 指针数组和数组指针例子
常量指针和指针常量
指针的指针:: 操作符*具有从右向左结合性
野指针出现的三种情况
- 指针变量没有初始化;
- 已经释放不用的指针没有置NULL,如delete和free之后的指针;
- 指针操作超越了变量的作用范围;
使用指针注意事项
1. 初始化指针值实际地址值或者NULL);
2. 间接引用时,请先判断是否为NULL。
原始指针的基本运算
运算符
- & 与 *
- ++ 和 —
著名的命名方法有
匈牙利命名法、驼峰式命名法、Pascal命名法。匈牙利命名法:
int iMyAge;驼峰式命名法:
int myAge;Pascal 命名法:
int MyAge;标识符长度应当符合
min-length&&max-information原则C++ 关键字
A – C | D – P | R – Z |
alignas (C++11)
alignof (C++11)
and
and_eq
asm
atomic_cancel (TM TS)
atomic_commit (TM TS)
atomic_noexcept (TM TS)
auto (1) (2) (3) (4)
bitand
bitor
bool
break
case
catch
char
char8_t (C++20)
char16_t (C++11)
char32_t (C++11)
class (1)
compl
concept (C++20)
const
consteval (C++20)
constexpr (C++11)
constinit (C++20)
const_cast
continue
co_await (C++20)
co_return (C++20)
co_yield (C++20) | reflexpr (reflection TS)
register (2)
reinterpret_cast
requires (C++20)
return
short
signed
sizeof (1)
static
static_assert (C++11)
static_cast
struct (1)
switch
synchronized (TM TS)
template
this (4)
thread_local (C++11)
throw
true
try
typedef
typeid
typename
union
unsigned
using (1)
virtual
void
volatile
wchar_t
while
xor
xor_eq |
整数常量前缀:
0x/0X(十六进制) 0(八进制)整数常量后缀:
U(unsigned)、L(long),顺序无关,大小写无关常量定义两种方法:: const 和define 编译时可查错
布尔常量:true/false 是标准C++关键词
字符常量如何表示一个宽字符:
添加大写L,如L'x',存储在wchat_t类型中 字符常量有三种:: 普通字符’x’,转义序列’\t’,通用字符’\u02c0’
换行符、回车符、水平制表符、垂直制表符:: \n \r \t \v
ANSI 转义序列
- ANSI转义序列是什么::用于控制文本终端显示的标准方法。它们通常用于设置文本颜色、背景颜色、文本样式(如粗体、下划线)等
- ANSI 转义序列的基本格式是
\033[m ,其中 \033 (或 \e )是转义字符, [ 是开头的方括号, 是具体的控制代码, m 表示设置文本属性。
ANSI 转义序列常见的文本属性哪些?
前景色(文本颜色)
- 30: 黑色
- 31: 红色
- 32: 绿色
- 33: 黄色
- 34: 蓝色
- 35: 洋红色(紫色)
- 36: 青色(蓝绿色)
- 37: 白色
背景色
- 40: 黑色
- 41: 红色
- 42: 绿色
- 43: 黄色
- 44: 蓝色
- 45: 洋红色(紫色)
- 46: 青色(蓝绿色)
- 47: 白色
高亮颜色(明亮颜色)
一些终端支持高亮(明亮)颜色,通过在基本颜色代码的基础上加 60 来实现,例如:
- 90: 明亮黑色(灰色)
- 91: 明亮红色
- 92: 明亮绿色
- 93: 明亮黄色
- 94: 明亮蓝色
- 95: 明亮洋红色(紫色)
- 96: 明亮青色(蓝绿色)
- 97: 明亮白色
文本样式
- 0: 重置/正常
- 1: 粗体
- 4: 下划线
- 5: 闪烁
- 7: 反显(背景和前景色互换)
- 8: 隐藏
- 设置字符为绿色并加粗
\033[1;32mThis is bold green text\033[0m





