Cpp备忘
Cpp备忘
编译
g++
make
cmake
关键字备忘
C++11
typedef, struct, class, namespace, using, friend, inline, const, static, virtual, override, final, template, typename, auto, decltype, nullptr, this, new, delete, operator, sizeof, alignas, alignof, volatile, mutable, explicit, default, delete, noexcept, throw, try, catch, throw, static_assert, assert, FILE, LINE, func, cplusplus, __STDC, STDC_VERSION, STDC_HOSTED, STDC_IEC_559, STDC_IEC_559_COMPLEX, STDC_ISO_10646, STDC_MB_MIGHT_NEQ_WC, STDCPP_STRICT_POINTER_SAFETY, STDCPP_THREADS, __STDCPP_USE_NOEXCEPT, __STDCPP_USE_STATIC_ASSERT
typedef: 类型alias
1
2
3
4
5
6
7
8
9
10
11
12
// samplpe1
typedef int integer;
integer a = 1;
// sample2
typedef int (*func)(int, int); // 定义了一个名为func的类型,这个类型是一个指向函数的指针,这个函数接受两个int类型的参数,并返回一个int类型的值。
// sample3
typedef struct Foo{
int a;
int b;
} Point; // 定义了一个名为Point的类型,这个类型是一个结构体,包含两个int类型的成员变量a和b。
关于sample2: 函数指针
typeof 获取变量的类型, 返回变量的类型
1
2
3
// sample1
int a = 1;
typeof(a) b = 2;
decltype: 获取表达式的类型
1
2
3
// sample1
int a = 1;
decltype(a) b = 2;
nullptr: 空指针
sizeof: 获取变量的大小
初始化的四种方式
请查看这个链接
- 拷贝初始化:
int a = 1;
- 值初始化:
int a[] = {1,2};
- 列表初始化:
int a{1};
推荐使用 - 直接初始化:
int a(1);
多态
接口类: 纯虚函数
纯虚函数是一个在基类中声明的虚函数,它在基类中没有定义,但是在派生类中必须定义。纯虚函数通过在声明语句的结尾使用 “= 0” 来指定。实现后绑定。
1
2
3
4
class Shape{
public:
virtual void draw() = 0; // 纯虚函数
};
char相关操作
- isalpha: 判断是否是字母
- isdigit: 判断是否是数字
- isalnum: 判断是否是字母或数字
- islower: 判断是否是小写字母
- isupper: 判断是否是大写字母
- tolower: 转换为小写字母
toupper: 转换为大写字母
- isspace: 判断是否是空白字符
- iscntrl: 判断是否是控制字符
- ispunct: 判断是否是标点符号
- isprint: 判断是否是可打印字符
- isgraph: 判断是否是图形字符
- isxdigit: 判断是否是十六进制字符
- isascii: 判断是否是ASCII字符
关于临时对象
1
2
3
4
5
6
7
8
9
10
11
class A{
public:
A(int a):m_a(a){}
int m_a;
};
int main(){
A* a = &A(1); // 临时对象, 会被释放, 会报错
A* b = new A(1); // 不是临时对象, 需要手动释放
return 0;
}
本文由作者按照 CC BY 4.0 进行授权