前言
这里只是简单列出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;
|
二、运算符
2.1 算术运算符
2.2 关系运算符
2.3 逻辑运算符
2.4 位运算符
2.5 赋值运算符
1
| = += -= *= /= %= <<= >>= &= ^= |=
|
2.6 其他运算符
三、流程控制
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 (int i = 0; i < 10; i++) { }
while (condition) { }
do { } while (condition);
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;
|
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; decltype(auto) y = x;
|
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); };
|
十四、最佳实践
- 使用RAII管理资源
- 优先使用智能指针而非裸指针
- 使用const正确性
- 遵循三/五/零法则
- 使用命名空间组织代码
- 优先使用STL而非原生数组
- 使用nullptr替代NULL(C++11)
- 使用类型别名替代宏定义
- 使用范围for循环遍历容器
- 使用移动语义优化性能
注:本文档涵盖了C++98到C++20的主要语法特性,实际使用时需注意编译器对特性的支持情况。
版权声明: 此文章版权归曦曦所有,如有转载,请注明来自原作者