{"id":17239953,"url":"https://github.com/yearing1017/cpp_may5","last_synced_at":"2025-03-26T03:12:38.829Z","repository":{"id":53207401,"uuid":"520175777","full_name":"yearing1017/CPP_May5","owner":"yearing1017","description":"cpp","archived":false,"fork":false,"pushed_at":"2022-10-17T15:20:53.000Z","size":59,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-31T00:31:47.412Z","etag":null,"topics":["cpp"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yearing1017.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-01T16:02:22.000Z","updated_at":"2023-02-08T14:02:05.000Z","dependencies_parsed_at":"2023-01-20T02:31:21.207Z","dependency_job_id":null,"html_url":"https://github.com/yearing1017/CPP_May5","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yearing1017%2FCPP_May5","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yearing1017%2FCPP_May5/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yearing1017%2FCPP_May5/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yearing1017%2FCPP_May5/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yearing1017","download_url":"https://codeload.github.com/yearing1017/CPP_May5/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245579677,"owners_count":20638679,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp"],"created_at":"2024-10-15T05:50:16.853Z","updated_at":"2025-03-26T03:12:38.798Z","avatar_url":"https://github.com/yearing1017.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\n\n\n\n\n# C_Primer_Plus\n\n## 关于\n\n📚 本仓库是面向 C/C++ 的基础知识总结，包括语言、数据结构。\n\n🙏 仓库内容如有错误或改进欢迎 issue 或 pr. 由于本人水平有限，仓库中的知识点有来自本人原创、读书笔记、书籍、博文等，非原创均已标明出处，如有遗漏，请 issue 提出。本仓库遵循 [CC BY-NC-SA 4.0（署名 - 非商业性使用 - 相同方式共享）](https://github.com/huihut/interview/blob/main/LICENSE) 协议，转载请注明出处，不得用于商业目的。\n\n## 目录\n* [STL](#stl)\n* [CPP基础](#cpp)\n\n\n\u003ca id=\"stl\"\u003e\u003c/a\u003e\n\n## STL\n\n### STL 索引\n\n[STL 方法含义索引](https://github.com/yearing1017/CPP_May5/tree/main/STL)\n\n### STL 容器\n\n容器 | 底层数据结构 | 时间复杂度 | 有无序 | 可不可重复 | 其他\n---|---|---|---|---|---\n[array](https://github.com/yearing1017/CPP_May5/tree/main/STL#array)|数组|随机读改 O(1)|无序|可重复|支持随机访问\n[vector](https://github.com/yearing1017/CPP_May5/tree/main/STL#vector)|数组|随机读改、尾部插入、尾部删除 O(1)\u003cbr/\u003e头部插入、头部删除 O(n)|无序|可重复|支持随机访问\n[deque](https://github.com/yearing1017/CPP_May5/tree/main/STL#deque)|双端队列|头尾插入、头尾删除 O(1)|无序|可重复|一个中央控制器 + 多个缓冲区，支持首尾快速增删，支持随机访问\n[forward_list](https://github.com/yearing1017/CPP_May5/tree/main/STL#forward_list)|单向链表|插入、删除 O(1)|无序|可重复|不支持随机访问\n[list](https://github.com/yearing1017/CPP_May5/tree/main/STL#list)|双向链表|插入、删除 O(1)|无序|可重复|不支持随机访问\n[stack](https://github.com/yearing1017/CPP_May5/tree/main/STL#stack)|deque / list|顶部插入、顶部删除 O(1)|无序|可重复|deque 或 list 封闭头端开口，不用 vector 的原因应该是容量大小有限制，扩容耗时\n[queue](https://github.com/yearing1017/CPP_May5/tree/main/STL#queue)|deque / list|尾部插入、头部删除 O(1)|无序|可重复|deque 或 list 封闭头端开口，不用 vector 的原因应该是容量大小有限制，扩容耗时\n[priority_queue](https://github.com/yearing1017/CPP_May5/tree/main/STL#priority_queue)|vector + max-heap|插入、删除 O(log\u003csub\u003e2\u003c/sub\u003en)|有序|可重复|vector容器+heap处理规则\n[set](https://github.com/yearing1017/CPP_May5/tree/main/STL#set)|红黑树|插入、删除、查找 O(log\u003csub\u003e2\u003c/sub\u003en)|有序|不可重复|\n[multiset](https://github.com/yearing1017/CPP_May5/tree/main/STL#multiset)|红黑树|插入、删除、查找 O(log\u003csub\u003e2\u003c/sub\u003en)|有序|可重复|\n[map](https://github.com/yearing1017/CPP_May5/tree/main/STL#map)|红黑树|插入、删除、查找 O(log\u003csub\u003e2\u003c/sub\u003en)|有序|不可重复|\n[multimap](https://github.com/yearing1017/CPP_May5/tree/main/STL#multimap)|红黑树|插入、删除、查找 O(log\u003csub\u003e2\u003c/sub\u003en)|有序|可重复|\n[unordered_set](https://github.com/yearing1017/CPP_May5/tree/main/STL#unordered_set)|哈希表|插入、删除、查找 O(1) 最差 O(n)|无序|不可重复|\n[unordered_multiset](https://github.com/yearing1017/CPP_May5/tree/main/STL#unordered_multiset)|哈希表|插入、删除、查找 O(1) 最差 O(n)|无序|可重复|\n[unordered_map](https://github.com/yearing1017/CPP_May5/tree/main/STL#unordered_map)|哈希表|插入、删除、查找 O(1) 最差 O(n)|无序|不可重复|\n[unordered_multimap](https://github.com/yearing1017/CPP_May5/tree/main/STL#unordered_multimap)|哈希表|插入、删除、查找 O(1) 最差 O(n)|无序|可重复|\n\n\n\u003ca id=\"cpp\"\u003e\u003c/a\u003e\n\n## CPP基础\n\n### 指针相关\n- 指针基础\n    - 指针存储的是地址，写法：`int * p1 = NULL`\n    - 取变量num的地址：`p2 = \u0026num`\n    - 指针指向内容赋值：`*p1 = 20`\n- 指针与结构体\n    - 对象：`Student stu = {\"Yu\", 2000, true};`\n    - 指针：`Student * pStu = \u0026stu;`\n    - 指针使用-\u003e来指向member：`pStu-\u003emember`\n    - 对象变量使用 `.` 来指向member：`(*pStu).member || stu.member` \n    - 指针自身也有地址：`int num = 10; int * p = \u0026num; int ** pp = \u0026p;`\n- const 指针\n    - `int foo(const char * p){ char * p2 = p; return 0; }` const char * 的指针不能用来初始化char *类型指针\n    - `const int * p1 = \u0026num;` 不能通过p1来改变p1指向的值，但可以改变p1指向的地址\n    - `int * const p2 = \u0026num;` 不能改变p2指向的地址，但可以通过p2来改变p2指向的值\n    - `const int* const p3 = \u0026num;` 都不可以改变\n- 指针与数组\n    - 数组名的指针 指向 数组首元素的地址\n- 指针的偏移与越界\n    - p + num || num + p 指向数组p的第num个元素\n    - p - num 指向数组p的第-num个元素\n    - 越界：`int num = 0; int * p = \u0026num; p[-1] = 2; //out of bound p[0] = 3; //okay *(p+1) = 4; //out of bound`\n- 指针和数组的区别\n    - 数组是一个constant指针\n    - sizeof 可以获得数组所有元素的size`int numbers[4] = {0, 1, 2, 3}; cout \u003c\u003c sizeof(numbers) \u003c\u003c endl; //4*sizeof(int)`\n    - 指针的size `int * p = numbers; cout \u003c\u003c sizeof(p) \u003c\u003c endl; // 4 or 8`\n- c和c++的动态分配内存与释放\n    - c语言的malloc \u0026 free\n    - c++的new和delete\n        - new() \u0026 new[]\n        - delete \u0026 delete[]\n\n### 函数\n- 函数基础\n    - .h头文件用来声明函数；func.cpp实现函数；main.cpp调用函数\n- 值参数\n    - 传递进来的基本类型参数是一份copy；修改该copy，原始数据不会受影响\n- 指针参数\n    - 传递进来的指针类型参数是一份copy；修改该copy，由于地址的值一样，所以原始数据也会被修改\n- 引用 \u0026 引用参数\n    - 引用写法`int \u0026 num_ref = num`，取地址写法`\u0026num`\n    - `int \u0026 num_ref = num` \u0026放在类型后面表示引用，不是取地址；相当于取别名\n    - 引用在声明的时候一定要初始化，只声明会报错 `int \u0026 num_ref;`\n    - 对象特别大时，传参会导致拷贝，所以可以使用指针或引用，不需拷贝\n        - 指针vs引用：指针需要判空，引用不需要；\n        - 传进来的引用被修改，会导致原始数据也被修改；所以可在引用参数前+const防止被修改\n- return 函数返回值\n    - void函数可返回可不返回，可使用`return;`\n    - 函数（A+B-\u003eC）1.使用引用防止数据copy；2.使用const防止输入数据被修改；3.使用non-const数据来接收输出\n- inline函数与宏\n- 函数默认参数\n    - c++的特性，c没有；在函数声明时，给参数赋予一个默认值，如果调用的时候没有赋值，则使用默认参数\n    - 默认参数只能放在参数列表的尾部，不能放前中位置\n- 函数重载\n    - \n- 函数模板\n\n### 类\n- 类的定义\n- 构造函数 + 析构函数","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyearing1017%2Fcpp_may5","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyearing1017%2Fcpp_may5","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyearing1017%2Fcpp_may5/lists"}