前言

这里只是简单列出C++语言的知识点


一、基础语法

1.1 基本结构

1
2
3
4
5
6
7
#include <iostream>  // 头文件包含
using namespace std; // 命名空间

int main() { // 主函数
cout << "Hello World!"; // 输出
return 0; // 返回值
}

1.2 数据类型

类型 说明 示例
int 整型 int a = 10;
float 单精度浮点 float b = 3.14f;
double 双精度浮点 double c = 3.14159;
char 字符 char d = 'A';
bool 布尔 bool e = true;
void 无类型 void func();
auto 自动推导(C++11) auto f = 5.0;

1.3 变量与常量

1
2
3
int var = 10;           // 变量
const int MAX = 100; // 常量
constexpr int SIZE = 50;// 编译期常量(C++11)

二、运算符

2.1 算术运算符

1
+ - * / % ++ --

2.2 关系运算符

1
== != > < >= <=

2.3 逻辑运算符

1
&& || !

2.4 位运算符

1
& | ^ ~ << >>

2.5 赋值运算符

1
= += -= *= /= %= <<= >>= &= ^= |=

2.6 其他运算符

1
sizeof() ?: :: . -> & * 

三、流程控制

3.1 条件语句

1
2
3
4
5
6
7
8
9
10
11
12
if (condition) {
// ...
} else if (condition2) {
// ...
} else {
// ...
}

switch (var) {
case 1: /*...*/ break;
default: /*...*/
}

3.2 循环结构

1
2
3
4
5
6
7
8
9
10
11
// for循环
for (int i = 0; i < 10; i++) { /*...*/ }

// while循环
while (condition) { /*...*/ }

// do-while循环
do { /*...*/ } while (condition);

// 范围for循环(C++11)
for (auto& item : container) { /*...*/ }

四、函数

4.1 函数定义

1
2
3
4
returnType funcName(parameters) {
// 函数体
return value;
}

4.2 函数重载

1
2
void print(int i) { /*...*/ }
void print(double d) { /*...*/ }

4.3 默认参数

1
void func(int a, int b = 10) { /*...*/ }

4.4 Lambda表达式(C++11)

1
auto lambda = [](int x) -> int { return x * x; };

五、面向对象编程

5.1 类与对象

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MyClass {
public: // 访问修饰符
// 构造函数
MyClass() { /*...*/ }
// 析构函数
~MyClass() { /*...*/ }

// 成员函数
void method() { /*...*/ }

private:
// 成员变量
int data;
};

5.2 继承

1
2
class Base { /*...*/ };
class Derived : public Base { /*...*/ };

5.3 多态

1
2
3
4
5
class Base {
public:
virtual void func() { /*...*/ } // 虚函数
virtual void pure() = 0; // 纯虚函数
};

六、模板与泛型编程

6.1 函数模板

1
2
template <typename T>
T max(T a, T b) { return a > b ? a : b; }

6.2 类模板

1
2
3
4
template <class T>
class Stack {
// ...
};

6.3 可变参数模板(C++11)

1
2
template<typename... Args>
void func(Args... args) { /*...*/ }

七、标准模板库(STL)

7.1 容器

1
2
3
4
vector<int> v = {1,2,3};       // 动态数组
list<string> lst; // 双向链表
map<string, int> m; // 关联数组
unordered_set<int> uset; // 哈希集合(C++11)

7.2 算法

1
2
sort(v.begin(), v.end());       // 排序
auto it = find(v.begin(), v.end(), 3); // 查找

7.3 迭代器

1
2
3
for (auto it = v.begin(); it != v.end(); ++it) {
cout << *it << endl;
}

八、内存管理

8.1 动态内存

1
2
int* p = new int(10);  // 分配
delete p; // 释放

8.2 智能指针(C++11)

1
2
3
unique_ptr<int> uptr(new int(5));
shared_ptr<int> sptr = make_shared<int>(10);
weak_ptr<int> wptr = sptr;

九、异常处理

1
2
3
4
5
try {
throw runtime_error("Error");
} catch (const exception& e) {
cerr << e.what() << endl;
}

十、文件操作

1
2
3
4
#include <fstream>
ofstream out("file.txt");
out << "Text";
out.close();

十一、多线程(C++11)

1
2
3
#include <thread>
thread t([](){ /* 任务 */ });
t.join();

十二、现代C++特性

12.1 移动语义(C++11)

1
2
string str1 = "Hello";
string str2 = std::move(str1); // 移动构造

12.2 类型推导(C++11/14)

1
2
auto x = 5;          // int
decltype(auto) y = x; // int

12.3 结构化绑定(C++17)

1
auto [x, y] = make_pair(1, 2.0);

十三、设计模式实现

13.1 单例模式

1
2
3
4
5
6
7
8
9
class Singleton {
public:
static Singleton& getInstance() {
static Singleton instance;
return instance;
}
private:
Singleton() = default;
};

13.2 工厂模式

1
2
3
4
5
6
7
8
9
10
class Product {
public:
virtual ~Product() {}
virtual void op() = 0;
};

class Factory {
public:
static unique_ptr<Product> create(int type);
};

十四、最佳实践

  1. 使用RAII管理资源
  2. 优先使用智能指针而非裸指针
  3. 使用const正确性
  4. 遵循三/五/零法则
  5. 使用命名空间组织代码
  6. 优先使用STL而非原生数组
  7. 使用nullptr替代NULL(C++11)
  8. 使用类型别名替代宏定义
  9. 使用范围for循环遍历容器
  10. 使用移动语义优化性能
    注:本文档涵盖了C++98到C++20的主要语法特性,实际使用时需注意编译器对特性的支持情况。