{"id":20649194,"url":"https://github.com/liulinboyi/learn-cplusplus","last_synced_at":"2025-07-21T03:33:13.817Z","repository":{"id":109596320,"uuid":"343260881","full_name":"liulinboyi/Learn-cplusplus","owner":"liulinboyi","description":"学习C++，Learn C++","archived":false,"fork":false,"pushed_at":"2021-03-08T15:01:46.000Z","size":48,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-07-10T07:46:07.010Z","etag":null,"topics":["cpp","learning"],"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/liulinboyi.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-01T01:53:21.000Z","updated_at":"2021-03-16T13:28:54.000Z","dependencies_parsed_at":"2023-05-11T08:30:56.068Z","dependency_job_id":null,"html_url":"https://github.com/liulinboyi/Learn-cplusplus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/liulinboyi/Learn-cplusplus","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FLearn-cplusplus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FLearn-cplusplus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FLearn-cplusplus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FLearn-cplusplus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/liulinboyi","download_url":"https://codeload.github.com/liulinboyi/Learn-cplusplus/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/liulinboyi%2FLearn-cplusplus/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266235438,"owners_count":23897180,"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","learning"],"created_at":"2024-11-16T17:13:06.200Z","updated_at":"2025-07-21T03:33:13.772Z","avatar_url":"https://github.com/liulinboyi.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Learn C++\n\n## Hello world!\n\n## Tips\n\n```c++\n// 这里使用endl而非\\n，原因是：\n// 系统输出，并非实时输出，是赞一定量的字符，然后批量的输出，\n// 加endl就表示，遇到endl进行一次刷输出的操作，此时立即输出，不赞了，提高输出效率\nstd::cout \u003c\u003c \"hello!\" \u003c\u003c std::endl;\n\n```\n\n```c++\n// 流程： 预处理 =\u003e 编译器编译 =\u003e 链接器\n// 引入头文件 iostream 实现了输入输出功能\n#include\n\u003cstdio.h\u003e\n\n// main函数\nint main() { // 入口函数，可以有参数，把外部文件传进来\u003c返回值类型是int如果不写返回值，默认也返回0\nprintf(\"hello world!\\n\");\n\nreturn 0;\n}\n\n```\n\n## void类型指针可以表示任何类型，void类型是一个非常有用的类型\n\n## 整形指针只能表示整形；浮点数的指针只能表示浮点型；\n\n## 变量定义、变量赋值、变量取值\n\n```c++\n// 定义\nint year = 2021;\nunsigned long date = 20210225;\nlong double pi = 3.14159;\nchar dot = '.';\n// 赋值\nint x = 1;\nx = 2;\nx = 3 + 4;\nx = x + 5;\n// 取值\nint x = 1;\nstd::cout \u003c\u003c x \u003c\u003c std::endl;\nint y = x + 1;\n// const修饰符\nconst int a = 3;\n```\n\n## 选择语句\n\n```c++\n// 关系运算法:\n// \u003c | \u003c= | \u003e | \u003e= | == | !=\n\n// if条件语句\nint a = 3;\nif (a \u003e 2) {\nstd::cout \u003c\u003c \"a \u003e 2\" \u003c\u003c std::endl;\n}\n\nif (a != 5) {\nstd::cout\u003c\u003c \"a != 5\" \u003c\u003c std::endl;\n}\n\n// else\n\nif (a \u003c 2) {\nstd::cout \u003c\u003c \"a \u003c 2\" \u003c\u003c std::endl;\n} else {\nstd::cout \u003c\u003c \"a \u003e= 2\" \u003c\u003c std::endl;\n}\n\n// else if\n\nif (a \u003c 2) {\nstd::cout \u003c\u003c \"a \u003c 2\" \u003c\u003c std::endl;\n} else if (a \u003e 2) {\nstd::cout \u003c\u003c \"a \u003e 2\" \u003c\u003c std::endl;\n} else {\nstd::cout \u003c\u003c \"a == 2\" \u003c\u003c std::endl;\n}\n\n// 相当于:\n// 从 a \u003c 2 分成了两个大分支，后面是一个小分支\nif (a \u003c 2) {\nstd::cout \u003c\u003c \"a \u003c 2\" \u003c\u003c std::endl;\n} else {\nif (a \u003e 2) {\nstd::cout \u003c\u003c \"a \u003e 2\" \u003c\u003c std::endl;\n} else {\nstd::cout \u003c\u003c \"a == 2\" \u003c\u003c std::endl;\n}\n}\n\n\n// switch\nint a = 3;\n\nswitch (a) {\ncase 1:\nstd::cout \u003c\u003c \"a is 1\" \u003c\u003c std::endl;\nbreak;\ncase 2:\nstd::cout \u003c\u003c \"a is 1\" \u003c\u003c std::endl;\nbreak;\ndefault:\nstd::cout \u003c\u003c \"a is not 1 ant 2\" \u003c\u003c std::endl;\nbreak;\n}\n\n```\n\n## 表达式\n\n```c++\n// 字面量表达式\n// 常数字面量表达式\n3;\n5;\n\n// 变量表达式\nint x = 3;\nx = x + 3;\n\n//算术表达式\n20 + 7;\n20 - 7;\n20 * 7;\n20 / 7;\n20 % 7;\n\n// 关系表达式\n20 \u003c 7;\n20 \u003c= 7;\n20 == 7;\n20 \u003e 7;\n20 \u003e= 7;\n\n// 逻辑表达式\ntrue and false;\ntrue \u0026\u0026 false;\ntrue or false;\ntrue || false;\nnot true;\n!true;\n\n// 位运算表达式\n5 \u0026 3; // 每个二进制位进行运算（\u0026操作）101 \u0026 011\n5 | 3; // 每个二进制位进行运算（|操作）101 | 011\n~3; // 每个二进制位进行取反\n5 ^ 3; // 每个二进制位进行异或操作，如果相对应的二进制位相同则该位为0，否则为1\n5 \u003c\u003c 1; // 二进制左移一位 相当于十进制数乘以2\n5 \u003e\u003e 1; // 二进制右移一位 相当于十进制数除以2\n\n// 条件表达式\ntrue ? 1 : 0;\n\n// 逗号表达式\n1, 2, 3; // 通过逗号把任意数量表达式进行连接，保证从左到右依次计算，保证运算顺序，将最后一个表达式的结果，作为整个表达式的结果\n\n// 函数调用表达式\nfoo()\n\n// 赋值表达式\nx = 3;\nx += 3;\nx -= 3;\nx *= 3;\nx /= 3;\nx %= 3;\n\n```\n\n## 数组\n\n```c++\n// 数组定义\nint array[5];\nchar words[6][6];\n\n// 数组初始化\nint array[5] = { 1, 2, 3, 4, 5 };\nchar words[6][6] = {\n{\n'o', 'n', 'e'\n},\n{\n't', 'w', 'o'\n},\n{\n't', 'h', 'r', 'e', 'e'\n}\n};\n\n// 数组访问\narray[0] = 6;\nint x = array[4];\n\nwords[3][0] = 'f';\n\n// C语言风格字符串\nchar s1[] = \"Hello\";\nchar s2[] = { 'H', 'e', 'l', 'l', 'o', 0 }; // 需要加一个0字符，0表示字符串结束\n\ns1[1] = 'a';\n\nstd::cout \u003c\u003c s1 \u003c\u003c std::endl;\n\n\n```\n\n## 循环语句\n\n```c++\n// while\nint n = 1;\nwhile (n \u003c 100) {\nstd::cout \u003c\u003c n \u003c\u003c std::endl;\nn *= 2;\n}\n\n// do while\nint n = 1;\ndo {\nstd::cout \u003c\u003c n \u003c\u003c std::endl;\nn *= 2;\n} while (n \u003c 100)\n\n// for\nfor (int n = 1;n \u003c 100;n *= 2) {\nstd::cout \u003c\u003c n \u003c\u003c std::endl;\n}\n\n// continue\nfor (int n = 1;n \u003c 100;n++) {\nif (n == 50) {\ncontinue; // n等于50时，不执行下面的语句，快进到下一步，进入下一次循环\n}\nstd::cout \u003c\u003c n \u003c\u003c std::endl;\n}\n\n// break\nfor (int n = 1;n \u003c 100;n++) {\nif (n == 50) {\nbreak; // n等于50时，结束当前循环，退出\n}\nstd::cout \u003c\u003c n \u003c\u003c std::endl;\n}\n```\n\n## 声明语句\n\n[变量声明代码](example/array.cpp#L11)\n\n```c++\n// 变量声明\nextern std::string name; // 声明，使用外部变量，告诉编译器，有个变量，不是我定义的，但是我想用，先定义后使用\n\n// 函数声明\ndouble cos(double x);\n\n// 名字空间\n// 不用每次都告诉编译器，这个对象是哪个名字空间下面的\nusing std::cout; // 省去单个，去名字空间下找对象，在后面直接使用cout就行\nusing namespace std; // 省去std名字空间下所有，去名字空间找对象，直接cout、cin就可以\n\n// 类型别名\n// 遇到模板时候，会再次使用带类型别名，\n// 比如让一个模板处理很多种类型，首先给类型起一个别名，\n// 不管类型真正的类型是什么，都叫做一个名字，通过这个类型的名字取处理他，\n// 最终达到一份代码，来处理不同类型的目的\ntypedef int int_t;\nusing uint_t = unsigned int; // c++ 11 之后新的写法\n\n\n```\n\n## 组合类型\n\n```c++\n// 数组\n// 前面有\n\n// 结构体struct 可以包含多个类型的组合\n// 定义\nstruct StudentInfo {\nchar name[20]; // 字符数组表示字符串\nint age;\ndouble score;\n};\n// 使用\nStudentInfo sz = { \"张三\", 24, 90 };\nStudentInfo ls = { \"李四\" }; // 后面未赋值，则自动用0填充\nls.age = 26;\nls.score = 80;\n\nstd::cout \u003c\u003c \"name: \" \u003c\u003c ls.name \u003c\u003c \"\\t\" \u003c\u003c \"age: \" \u003c\u003c ls.age \u003c\u003c \"\\t\" \u003c\u003c \"score: \" \u003c\u003c ls.score \u003c\u003c std::endl;\n\n\n// 联合体union 可以容纳第一个类型，单任意时刻只包含其中一种\n\n// 枚举 表示类型的取值有，有限多种可能性\n\nenum Gender {\nMALE,\nFEMALE,\n};\n```\n\n## 函数重载\n\n```c++\ndouble power(double a, int n);\ndouble power(double a, double n);\n\n// 根据参数列表不同去找不同的函数\n\n```\n\n## [指针](example/pointer.cpp)\n\n```c++\n\n\n```\n\n## 表达式树\n\n```c++\n\n// 节点基类\nstruct Node {\nvirtual double eval() const = 0;\n\n};\n\n```\n\n## 内存管理\n\n```c++\n// std::share_ptr\n#include\n\u003cmemory\u003e\n\nusing namespace std;\nshared_ptr\u003cint\u003e p (new int(3));\np = make_shared\u003cint\u003e(5);\ncout \u003c\u003c \"*p: \" \u003c\u003c *p \u003c\u003c endl;\n\n{\nshared_ptr\u003cint\u003e q(p);\ncout \u003c\u003c \"*q:\" \u003c\u003c *q \u003c\u003c endl;\ncout \u003c\u003c \"p.use_count():\" \u003c\u003c p.use_count() \u003c\u003c endl;\n}\n\ncout \u003c\u003c \"p.use_count():\" \u003c\u003c p.use_count() \u003c\u003c endl;\n\n```\n\n## 模板库的使用\n\n```c++\n// 顺序容器\n\n// 字符串 C语言中字符串是一个字符数组\nstring()\nstring(n, c)\nstring(str)\nstring(c_str, n)\nstring(begin, end)\n\n```\n\n## 列表\n\n```c++\n// 是由链表实现的\n// 没有at方法\n// 自身有sort方法\n\n```\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliulinboyi%2Flearn-cplusplus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliulinboyi%2Flearn-cplusplus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliulinboyi%2Flearn-cplusplus/lists"}