13b3ebbe14
把川大 wiki「02.Qt方向」课程代码整理为 CMake 管理的可编译运行项目。
Part1 C/C++ 强化 (p01/, 144 目标, host gcc/g++ 构建, 全绿):
- 修复全部编译失败:补 <cstring>/<fstream>/<iomanip>/<vector> 等缺失头
(common_fix + infer_stl_includes 自动检测注入)、修正 pause() 壳注入
(改检测已转换后的 pause 调用)、namespace 提升出函数体、const 成员函数
正确性、dedupe 不再破坏函数重载集、K&R 隐式 int / 动态异常规范等
C++17 移除特性的可编译等价改写。
- 目录采用深缩写 p01/ch04/s03/ (原 part1_cpp/ch04_class_object/03_ctor_dtor)。
Part3 Qt 桌面 (p03/, 29 目标, 隔离 Qt 5.14.2 构建, 全绿):
- gen_part3.py: Qt 外壳模板 (main+QApplication+show, QTimer 自动退出便于
offscreen 验证)、AUTOMOC + .moc include、rewrite_* 处理散文混入/重载/
资源依赖、生成占位 png/gif + .qrc + rundata 样例数据。
- 修正 QLable 笔误、全角分号、.→->、Windows 路径、QApplication 阻塞事件
循环改为 QCoreApplication 等。
任务1 概念/版本验证:
- tools/std_probe.py: 跨 -std= 编译探针 (--pedantic-errors 让已废除特性硬失败)。
- tools/demo_versions/: K&R 隐式 int、三目左值、void* 转换、enum 赋整、
const 经指针、动态异常规范 共 6 个跨版本演示。
- docs/VERSION_NOTES.md: 每条「原文说法→C17/C++17 是否成立→何版本成立→已验证」。
文档: 顶层 README、docs/SOURCE_PAGES.md (页面→目录映射)、
docs/ERRATA.md (Part1 修正)、docs/ERRATA_part3.md (Part3 修正)。
另修复 tools/run.sh / run_qt.sh 的 ${1:?...} 引号语法 bug。
隔离 Qt 验证: ldd 0 系统 Qt 引用; 173/173 目标全绿; p03 offscreen 运行通过。
1 line
9.0 KiB
JSON
1 line
9.0 KiB
JSON
{"id":"58954487","type":"page","status":"current","title":"02.STL三大组件","version":{"by":{"type":"known","username":"ethanliu","userKey":"2c90931b6cfb654f016d5c69becc0001","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"刘浩","_links":{"self":"https://wiki.suncaper.net/rest/api/user?key=2c90931b6cfb654f016d5c69becc0001"},"_expandable":{"status":""}},"when":"2022-07-09T01:51:24.000Z","message":"","number":1,"minorEdit":false,"hidden":false,"_links":{"self":"https://wiki.suncaper.net/rest/experimental/content/58954487/version/1"},"_expandable":{"content":"/rest/api/content/58954487"}},"body":{"storage":{"value":"<ac:layout><ac:layout-section ac:type=\"two_right_sidebar\"><ac:layout-cell><h2>2.1 容器</h2><p>容器,置物之所也。</p><p>研究数据的特定排列方式,以利于搜索或排序或其他特殊目的,这一门学科我们称为数据结构。大学信息类相关专业里面,与编程最有直接关系的学科,首推数据结构与算法。几乎可以说,任何特定的数据结构都是为了实现某种特定的算法。<span>STL</span>容器就是将运用最广泛的一些数据结构实现出来。</p><p>常用的数据结构:数组<span>(array),</span>链表<span>(list),tree(</span>树<span>)</span>,栈<span>(stack),</span>队列<span>(queue),</span>集合<span>(set),</span>映射表<span>(map),</span>根据数据在容器中的排列特性,这些数据分为<strong>序列式容器</strong>和<strong>关联式容器</strong>两种。</p><ul><li>序列式容器强调值的排序,序列式容器中的每个元素均有固定的位置,除非用删除或插入的操作改变这个位置。<span>Vector</span>容器、<span>Deque</span>容器、<span>List</span>容器等。</li><li>关联式容器是非线性的树结构,更准确的说是二叉树结构。各元素之间没有严格的物理上的顺序关系,也就是说元素在容器中并没有保存元素置入容器时的逻辑顺序。关联式容器另一个显著特点是:在值中选择一个值作为关键字<span>key</span>,这个关键字对值起到索引的作用,方便查找。<span>Set/multiset</span>容器<span> Map/multimap</span>容器</li></ul><h2>2.2 算法</h2><p>算法,问题之解法也。</p><p>以有限的步骤,解决逻辑或数学上的问题,这一门学科我们叫做算法<span>(Algorithms).</span></p><p>广义而言,我们所编写的每个程序都是一个算法,其中的每个函数也都是一个算法,毕竟它们都是用来解决或大或小的逻辑问题或数学问题。<span>STL</span>收录的算法经过了数学上的效能分析与证明,是极具复用价值的,包括常用的排序,查找等等。特定的算法往往搭配特定的数据结构,算法与数据结构相辅相成。</p><p>算法分为<span>:</span><strong>质变算法</strong>和<strong>非质变算法</strong>。</p><p>质变算法:是指运算过程中会更改区间内的元素的内容。例如拷贝,替换,删除等等</p><p>非质变算法:是指运算过程中不会更改区间内的元素内容,例如查找、计数、遍历、寻找极值等等</p><p><br /></p><table><tbody><tr><td><p><strong>再好的编程技巧,也无法让一个笨拙的算法起死回生。</strong></p></td></tr></tbody></table><p><br /></p><h2>2.3 迭代器</h2><p>迭代器<span>(iterator)</span>是一种抽象的设计概念,现实程序语言中并没有直接对应于这个概念的实物。在<span><<Design Patterns>></span>一书中提供了<span>23</span>中设计模式的完整描述,其中<span>iterator</span>模式定义如下:提供一种方法,使之能够依序寻访某个容器所含的各个元素,而又无需暴露该容器的内部表示方式。</p><p>迭代器的设计思维<span>-STL</span>的关键所在,<span>STL</span>的中心思想在于将容器<span>(container)</span>和算法<span>(algorithms)</span>分开,彼此独立设计,最后再一贴胶着剂将他们撮合在一起。从技术角度来看,容器和算法的泛型化并不困难,<span>c++</span>的<span>class template</span>和<span>function template</span>可分别达到目标,如果设计出两这个之间的良好的胶着剂,才是大难题。</p><p><br /></p><p>迭代器的种类<span>:</span></p><table><colgroup><col /><col /><col /></colgroup><tbody><tr><td><p>输入迭代器</p></td><td><p>提供对数据的只读访问</p></td><td><p>只读,支持<span>++</span>、<span>==</span>、!<span>=</span></p></td></tr><tr><td><p>输出迭代器</p></td><td><p>提供对数据的只写访问</p></td><td><p>只写,支持<span>++</span></p></td></tr><tr><td><p>前向迭代器</p></td><td><p>提供读写操作,并能向前推进迭代器</p></td><td><p>读写,支持<span>++</span>、<span>==</span>、!<span>=</span></p></td></tr><tr><td><p>双向迭代器</p></td><td><p>提供读写操作,并能向前和向后操作</p></td><td><p>读写,支持<span>++</span>、<span>--</span>,</p></td></tr><tr><td><p>随机访问迭代器</p></td><td><p>提供读写操作,并能以跳跃的方式访问容器的任意数据,是功能最强的迭代器</p></td><td><p>读写,支持<span>++</span>、<span>--</span>、<span>[n]</span>、<span>-n</span>、<span><</span>、<span><=</span>、<span>></span>、<span>>=</span></p></td></tr></tbody></table><h2><span>2.4 </span>案例</h2><ac:structured-macro ac:name=\"code\" ac:schema-version=\"1\" ac:macro-id=\"2f1cc36d-f5ac-4a0c-a014-5b1342943541\"><ac:parameter ac:name=\"language\">cpp</ac:parameter><ac:plain-text-body><![CDATA[#define _CRT_SECURE_NO_WARNINGS\n#include<iostream>\n#include<vector>\n#include<algorithm>\nusing namespace std;\n\n//STL 中的容器 算法 迭代器\nvoid test01(){\n\tvector<int> v; //STL 中的标准容器之一 :动态数组\n\tv.push_back(1); //vector 容器提供的插入数据的方法\n\tv.push_back(5);\n\tv.push_back(3);\n\tv.push_back(7);\n\t//迭代器\n\tvector<int>::iterator pStart = v.begin(); //vector 容器提供了 begin()方法 返回指向第一个元素的迭代器\n\tvector<int>::iterator pEnd = v.end(); //vector 容器提供了 end()方法 返回指向最后一个元素下一个位置的迭代器\n\t//通过迭代器遍历\n\twhile (pStart != pEnd){\n\t\tcout << *pStart << \" \";\n\t\tpStart++;\n\t}\n\tcout << endl;\n}\n//STL 容器不单单可以存储基础数据类型,也可以存储类对象\nclass Teacher\n{\npublic:\n\tTeacher(int age) :age(age){};\n\t~Teacher(){};\npublic:\n\tint age;\n};\nvoid test02(){\n\tvector<Teacher> v; //存储 Teacher 类型数据的容器\n\tTeacher t1(10), t2(20), t3(30);\n\tv.push_back(t1);\n\tv.push_back(t2);\n\tv.push_back(t3);\n\tvector<Teacher>::iterator pStart = v.begin();\n\tvector<Teacher>::iterator pEnd = v.end();\n\t//通过迭代器遍历\n\twhile (pStart != pEnd){\n\t\tcout << pStart->age << \" \";\n\t\tpStart++;\n\t}\n\tcout << endl;\n}\n//存储 Teacher 类型指针\nvoid test03(){\n\tvector<Teacher*> v; //存储 Teacher 类型指针\n\tTeacher* t1 = new Teacher(10);\n\tTeacher* t2 = new Teacher(20);\n\tTeacher* t3 = new Teacher(30);\n\tv.push_back(t1);\n\tv.push_back(t2);\n\tv.push_back(t3);\n\t//拿到容器迭代器\n\tvector<Teacher*>::iterator pStart = v.begin();\n\tvector<Teacher*>::iterator pEnd = v.end();\n\t//通过迭代器遍历\n\twhile (pStart != pEnd){\n\t\tcout << (*pStart)->age << \" \";\n\t\tpStart++;\n\t}\n\tcout << endl;\n}\n//容器嵌套容器 难点(不理解,可以跳过)\nvoid test04()\n{\n\tvector< vector<int> > v;\n\tvector<int>v1;\n\tvector<int>v2;\n\tvector<int>v3;\n\n\tfor (int i = 0; i < 5;i++)\n\t{\n\t\tv1.push_back(i);\n\t\tv2.push_back(i * 10);\n\t\tv3.push_back(i * 100);\n\t}\n\tv.push_back(v1);\n\tv.push_back(v2);\n\tv.push_back(v3);\n\n\tfor (vector< vector<int> >::iterator it = v.begin(); it != v.end();it++)\n\t{\n\t\tfor (vector<int>::iterator subIt = (*it).begin(); subIt != (*it).end(); subIt ++)\n\t\t{\n\t\t\tcout << *subIt << \" \";\n\t\t}\n\t\tcout << endl;\n\t}\n} \nint main(){\n\t//test01();\n\t//test02();\n\t//test03();\n\ttest04();\n\tsystem(\"pause\");\n\treturn EXIT_SUCCESS;\n}]]></ac:plain-text-body></ac:structured-macro></ac:layout-cell><ac:layout-cell><p><br /></p></ac:layout-cell></ac:layout-section></ac:layout>","representation":"storage","_expandable":{"content":"/rest/api/content/58954487"}},"_expandable":{"editor":"","view":"","export_view":"","styled_view":"","anonymous_export_view":""}},"extensions":{"position":"none"},"_links":{"webui":"/pages/viewpage.action?pageId=58954487","edit":"/pages/resumedraft.action?draftId=58954487","tinyui":"/x/95KDAw","collection":"/rest/api/content","base":"https://wiki.suncaper.net","context":"","self":"https://wiki.suncaper.net/rest/api/content/58954487"},"_expandable":{"container":"/rest/api/space/2CT","metadata":"","operations":"","children":"/rest/api/content/58954487/child","restrictions":"/rest/api/content/58954487/restriction/byOperation","history":"/rest/api/content/58954487/history","ancestors":"","descendants":"/rest/api/content/58954487/descendant","space":"/rest/api/space/2CT"}} |