课程代码仓库初始化:Part1 (144) + Part3 (29) 全量生成与验证

把川大 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 运行通过。
This commit is contained in:
张宗平
2026-06-30 14:16:55 +08:00
commit 13b3ebbe14
287 changed files with 16532 additions and 0 deletions
+7
View File
@@ -0,0 +1,7 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch09/s02
add_executable(p1c09_02_01 stl_components_case.cpp)
set_target_properties(p1c09_02_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_02_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_02_01 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch09_s02 DEPENDS p1c09_02_01)
+119
View File
@@ -0,0 +1,119 @@
// ============================================================================
// p1c09_02_01 — 2.4 案例
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <vector>
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954487:0] 2.4 案例 ---
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
//STL 中的容器 算法 迭代器
void test01(){
vector<int> v; //STL 中的标准容器之一 :动态数组
v.push_back(1); //vector 容器提供的插入数据的方法
v.push_back(5);
v.push_back(3);
v.push_back(7);
//迭代器
vector<int>::iterator pStart = v.begin(); //vector 容器提供了 begin()方法 返回指向第一个元素的迭代器
vector<int>::iterator pEnd = v.end(); //vector 容器提供了 end()方法 返回指向最后一个元素下一个位置的迭代器
//通过迭代器遍历
while (pStart != pEnd){
cout << *pStart << " ";
pStart++;
}
cout << endl;
}
//STL 容器不单单可以存储基础数据类型,也可以存储类对象
class Teacher
{
public:
Teacher(int age) :age(age){};
~Teacher(){};
public:
int age;
};
void test02(){
vector<Teacher> v; //存储 Teacher 类型数据的容器
Teacher t1(10), t2(20), t3(30);
v.push_back(t1);
v.push_back(t2);
v.push_back(t3);
vector<Teacher>::iterator pStart = v.begin();
vector<Teacher>::iterator pEnd = v.end();
//通过迭代器遍历
while (pStart != pEnd){
cout << pStart->age << " ";
pStart++;
}
cout << endl;
}
//存储 Teacher 类型指针
void test03(){
vector<Teacher*> v; //存储 Teacher 类型指针
Teacher* t1 = new Teacher(10);
Teacher* t2 = new Teacher(20);
Teacher* t3 = new Teacher(30);
v.push_back(t1);
v.push_back(t2);
v.push_back(t3);
//拿到容器迭代器
vector<Teacher*>::iterator pStart = v.begin();
vector<Teacher*>::iterator pEnd = v.end();
//通过迭代器遍历
while (pStart != pEnd){
cout << (*pStart)->age << " ";
pStart++;
}
cout << endl;
}
//容器嵌套容器 难点(不理解,可以跳过)
void test04()
{
vector< vector<int> > v;
vector<int>v1;
vector<int>v2;
vector<int>v3;
for (int i = 0; i < 5;i++)
{
v1.push_back(i);
v2.push_back(i * 10);
v3.push_back(i * 100);
}
v.push_back(v1);
v.push_back(v2);
v.push_back(v3);
for (vector< vector<int> >::iterator it = v.begin(); it != v.end();it++)
{
for (vector<int>::iterator subIt = (*it).begin(); subIt != (*it).end(); subIt ++)
{
cout << *subIt << " ";
}
cout << endl;
}
}
int main(){
//test01();
//test02();
//test03();
test04();
pause();
return EXIT_SUCCESS;
}
// --- main ---
+31
View File
@@ -0,0 +1,31 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch09/s03
add_executable(p1c09_03_01 string_and_c_string.cpp)
set_target_properties(p1c09_03_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_03_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_03_01 PRIVATE -Wall -Wextra)
add_executable(p1c09_03_02 vector_iterator.cpp)
set_target_properties(p1c09_03_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_03_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_03_02 PRIVATE -Wall -Wextra)
add_executable(p1c09_03_03 vector_swap_shrink.cpp)
set_target_properties(p1c09_03_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_03_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_03_03 PRIVATE -Wall -Wextra)
add_executable(p1c09_03_04 vector_reserve.cpp)
set_target_properties(p1c09_03_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_03_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_03_04 PRIVATE -Wall -Wextra)
add_executable(p1c09_03_05 list_data_structure.cpp)
set_target_properties(p1c09_03_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_03_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_03_05 PRIVATE -Wall -Wextra)
add_executable(p1c09_03_06 set_find_operations.cpp)
set_target_properties(p1c09_03_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_03_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_03_06 PRIVATE -Wall -Wextra)
add_executable(p1c09_03_07 multimap_case.cpp)
set_target_properties(p1c09_03_07 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_03_07 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_03_07 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch09_s03 DEPENDS p1c09_03_01 p1c09_03_02 p1c09_03_03 p1c09_03_04 p1c09_03_05 p1c09_03_06 p1c09_03_07)
+44
View File
@@ -0,0 +1,44 @@
// ============================================================================
// p1c09_03_05 — 3.6.3 list容器的数据结构
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <list>
// --- 原文片段 [58954488:4] 3.6.3 list容器的数据结构 ---
#include <iostream>
#include <list>
using namespace std;
// 【可移植性说明】原文直接访问 MSVC STL 的 list 私有成员
// _Nodeptr / _Myhead / _Next / _Mysize / _Myval)来「演示 list 的环形
// 双向链表结构」。这些是 MSVC 实现细节,libstdc++(gcc)无等价物,无法移植。
// 下面改用标准迭代器接口遍历,并以注释图示说明 list 的环形双向链表结构:
//
// _Myhead (哨兵节点) ──┐
// ↑ ↓ │ list 内部维护一个哨兵头节点 _Myhead,
// [_Prev|_Myval|_Next] ←──→ [_Prev|_Myval|_Next] ←──→ ... ──┘
// 末节点.next = _Myhead_Myhead.prev = 末节点;_Mysize 记录元素个数。
// 遍历从 _Myhead->_Next 开始,绕一圈回到 _Myhead 即结束。
int main(){
list<int> myList;
for (int i = 0; i < 10; i++){
myList.push_back(i);
}
// 标准(可移植)方式:用迭代器遍历环形双向链表
cout << "size = " << myList.size() << endl;
for (list<int>::iterator it = myList.begin(); it != myList.end(); it++){
cout << "Node:" << *it << endl;
}
return EXIT_SUCCESS;
}
// --- main ---
+200
View File
@@ -0,0 +1,200 @@
// ============================================================================
// p1c09_03_07 — 3.8.3 multimap案例
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <map>
#include <vector>
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954488:6] 3.8.3 multimap案例 ---
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<map>
#include<string>
#include<vector>
using namespace std;
//multimap 案例
//公司今天招聘了 5 个员工,5 名员工进入公司之后,需要指派员工在那个部门工作
//人员信息有: 姓名 年龄 电话 工资等组成
//通过 Multimap 进行信息的插入 保存 显示
//分部门显示员工信息 显示全部员工信息
#define SALE_DEPATMENT 1 //销售部门
#define DEVELOP_DEPATMENT 2 //研发部门
#define FINACIAL_DEPATMENT 3 //财务部门
#define ALL_DEPATMENT 4 //所有部门
//员工类
class person{
public:
string name; //员工姓名
int age; //员工年龄
double salary; //员工工资
string tele; //员工电话
};
//创建5个员工
void CreatePerson(vector<person>& vlist){
string seed = "ABCDE";
for (int i = 0; i < 5; i++){
person p;
p.name = "员工";
p.name += seed[i];
p.age = rand() % 30 + 20;
p.salary = rand() % 20000 + 10000;
p.tele = "010-8888888";
vlist.push_back(p);
}
}
//5名员工分配到不同的部门
void PersonByGroup(vector<person>& vlist, multimap<int, person>& plist){
int operate = -1; //用户的操作
for (vector<person>::iterator it = vlist.begin(); it != vlist.end(); it++){
cout << "当前员工信息:" << endl;
cout << "姓名:" << it->name << " 年龄:" << it->age << " 工资:" << it->salary << " 电话:" << it->tele << endl;
cout << "请对该员工进行部门分配(1 销售部门, 2 研发部门, 3 财务部门):" << endl;
scanf("%d", &operate);
while (true){
if (operate == SALE_DEPATMENT){ //将该员工加入到销售部门
plist.insert(make_pair(SALE_DEPATMENT, *it));
break;
}
else if (operate == DEVELOP_DEPATMENT){
plist.insert(make_pair(DEVELOP_DEPATMENT, *it));
break;
}
else if (operate == FINACIAL_DEPATMENT){
plist.insert(make_pair(FINACIAL_DEPATMENT, *it));
break;
}
else{
cout << "您的输入有误,请重新输入(1 销售部门, 2 研发部门, 3 财务部门):" << endl;
scanf("%d", &operate);
}
}
}
cout << "员工部门分配完毕!" << endl;
cout << "***********************************************************" << endl;
}
//打印员工信息
void printList(multimap<int, person>& plist, int myoperate){
if (myoperate == ALL_DEPATMENT){
for (multimap<int, person>::iterator it = plist.begin(); it != plist.end(); it++){
cout << "姓名:" << it->second.name << " 年龄:" << it->second.age << " 工资:" << it->second.salary << " 电话:" << it->second.tele << endl;
}
return;
}
multimap<int, person>::iterator it = plist.find(myoperate);
int depatCount = plist.count(myoperate);
int num = 0;
if (it != plist.end()){
while (it != plist.end() && num < depatCount){
cout << "姓名:" << it->second.name << " 年龄:" << it->second.age << " 工资:" << it->second.salary << " 电话:" << it->second.tele << endl;
it++;
num++;
}
}
}
//根据用户操作显示不同部门的人员列表
void ShowPersonList(multimap<int, person>& plist, int myoperate){
switch (myoperate)
{
case SALE_DEPATMENT:
printList(plist, SALE_DEPATMENT);
break;
case DEVELOP_DEPATMENT:
printList(plist, DEVELOP_DEPATMENT);
break;
case FINACIAL_DEPATMENT:
printList(plist, FINACIAL_DEPATMENT);
break;
case ALL_DEPATMENT:
printList(plist, ALL_DEPATMENT);
break;
}
}
//用户操作菜单
void PersonMenue(multimap<int, person>& plist){
int flag = -1;
int isexit = 0;
while (true){
cout << "请输入您的操作((1 销售部门, 2 研发部门, 3 财务部门, 4 所有部门, 0退出):" << endl;
scanf("%d", &flag);
switch (flag)
{
case SALE_DEPATMENT:
ShowPersonList(plist, SALE_DEPATMENT);
break;
case DEVELOP_DEPATMENT:
ShowPersonList(plist, DEVELOP_DEPATMENT);
break;
case FINACIAL_DEPATMENT:
ShowPersonList(plist, FINACIAL_DEPATMENT);
break;
case ALL_DEPATMENT:
ShowPersonList(plist, ALL_DEPATMENT);
break;
case 0:
isexit = 1;
break;
default:
cout << "您的输入有误,请重新输入!" << endl;
break;
}
if (isexit == 1){
break;
}
}
}
int main(){
vector<person> vlist; //创建的5个员工 未分组
multimap<int, person> plist; //保存分组后员工信息
//创建5个员工
CreatePerson(vlist);
//5名员工分配到不同的部门
PersonByGroup(vlist, plist);
//根据用户输入显示不同部门员工信息列表 或者 显示全部员工的信息列表
PersonMenue(plist);
pause();
return EXIT_SUCCESS;
}
// --- main ---
+103
View File
@@ -0,0 +1,103 @@
// ============================================================================
// p1c09_03_06 — 3.7.2.5 set查找操作
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <set>
// --- 原文片段 [58954488:5] 3.7.2.5 set查找操作 ---
//插入操作返回值
void test01(){
set<int> s;
pair<set<int>::iterator,bool> ret = s.insert(10);
if (ret.second){
cout << "插入成功:" << *ret.first << endl;
}
else{
cout << "插入失败:" << *ret.first << endl;
}
ret = s.insert(10);
if(ret.second){
cout << "插入成功:" << *ret.first << endl;
}
else{
cout << "插入失败:" << *ret.first << endl;
}
}
struct MyCompare02{
bool operator()(int v1, int v2) const {
return v1 > v2;
}
};
//set从大到小
void test02(){
srand((unsigned int)time(NULL));
//我们发现set容器的第二个模板参数可以设置排序规则,默认规则是less<_Kty>
set<int, MyCompare02> s;
for (int i = 0; i < 10;i++){
s.insert(rand() % 100);
}
for (set<int, MyCompare02>::iterator it = s.begin(); it != s.end(); it ++){
cout << *it << " ";
}
cout << endl;
}
//set容器中存放对象
class Person{
public:
Person(string name,int age){
this->mName = name;
this->mAge = age;
}
public:
string mName;
int mAge;
};
struct MyCompare03{
bool operator()(const Person& p1, const Person& p2) const {
return p1.mAge > p2.mAge;
}
};
void test03(){
set<Person, MyCompare03> s;
Person p1("aaa", 20);
Person p2("bbb", 30);
Person p3("ccc", 40);
Person p4("ddd", 50);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person, MyCompare03>::iterator it = s.begin(); it != s.end(); it++){
cout << "Name:" << it->mName << " Age:" << it->mAge << endl;
}
}
// --- main ---
int main() {
test01();
test02();
test03();
return 0;
}
+37
View File
@@ -0,0 +1,37 @@
// ============================================================================
// p1c09_03_01 — 3.1.2.9 string和c-style字符串转换
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954488:0] 3.1.2.9 string和c-style字符串转换 ---
static void block_0() {
string s = "abcdefg";
char& a = s[2];
char& b = s[3];
a = '1';
b = '2';
cout << s << endl;
cout << (int*)s.c_str() << endl;
s = "pppppppppppppppppppppppp";
//a = '1';
//b = '2';
cout << s << endl;
cout << (int*)s.c_str() << endl;
}
// --- main ---
int main() {
block_0();
return 0;
}
+35
View File
@@ -0,0 +1,35 @@
// ============================================================================
// p1c09_03_02 — 3.2.2 vector迭代器
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <vector>
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954488:1] 3.2.2 vector迭代器 ---
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
for (int i = 0; i < 10;i ++){
v.push_back(i);
cout << v.capacity() << endl; // v.capacity()容器的容量
}
pause();
return EXIT_SUCCESS;
}
// --- main ---
+46
View File
@@ -0,0 +1,46 @@
// ============================================================================
// p1c09_03_04 — 3.2.5.2 reserve预留空间
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954488:3] 3.2.5.2 reserve预留空间 ---
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
//预先开辟空间
v.reserve(100000);
int* pStart = NULL;
int count = 0;
for (int i = 0; i < 100000;i ++){
v.push_back(i);
if (pStart != &v[0]){
pStart = &v[0];
count++;
}
}
cout << "count:" << count << endl;
pause();
return EXIT_SUCCESS;
}
// --- main ---
+50
View File
@@ -0,0 +1,50 @@
// ============================================================================
// p1c09_03_03 — 3.2.5.1巧用swap,收缩内存空间
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
#include <cstdio>
static inline void pause() { printf("按回车键继续..."); (void)getchar(); }
// --- 原文片段 [58954488:2] 3.2.5.1巧用swap,收缩内存空间 ---
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<vector>
using namespace std;
int main(){
vector<int> v;
for (int i = 0; i < 100000;i ++){
v.push_back(i);
}
cout << "capacity:" << v.capacity() << endl;
cout << "size:" << v.size() << endl;
//此时 通过resize改变容器大小
v.resize(10);
cout << "capacity:" << v.capacity() << endl;
cout << "size:" << v.size() << endl;
//容量没有改变
vector<int>(v).swap(v);
cout << "capacity:" << v.capacity() << endl;
cout << "size:" << v.size() << endl;
pause();
return EXIT_SUCCESS;
}
// --- main ---
+27
View File
@@ -0,0 +1,27 @@
# Auto-generated by tools/gen_part1.py — do not edit by hand.
# 章节:p01/ch09/s04
add_executable(p1c09_04_01 function_object.cpp)
set_target_properties(p1c09_04_01 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_04_01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_04_01 PRIVATE -Wall -Wextra)
add_executable(p1c09_04_02 predicate.cpp)
set_target_properties(p1c09_04_02 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_04_02 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_04_02 PRIVATE -Wall -Wextra)
add_executable(p1c09_04_03 builtin_function_object.cpp)
set_target_properties(p1c09_04_03 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_04_03 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_04_03 PRIVATE -Wall -Wextra)
add_executable(p1c09_04_04 builtin_function_object_2.cpp)
set_target_properties(p1c09_04_04 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_04_04 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_04_04 PRIVATE -Wall -Wextra)
add_executable(p1c09_04_05 traversal_algorithms.cpp)
set_target_properties(p1c09_04_05 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_04_05 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_04_05 PRIVATE -Wall -Wextra)
add_executable(p1c09_04_06 traversal_algorithms_2.cpp)
set_target_properties(p1c09_04_06 PROPERTIES C_STANDARD 17 C_STANDARD_REQUIRED ON C_EXTENSIONS OFF)
set_target_properties(p1c09_04_06 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
target_compile_options(p1c09_04_06 PRIVATE -Wall -Wextra)
add_custom_target(all_p01_ch09_s04 DEPENDS p1c09_04_01 p1c09_04_02 p1c09_04_03 p1c09_04_04 p1c09_04_05 p1c09_04_06)
+58
View File
@@ -0,0 +1,58 @@
// ============================================================================
// p1c09_04_03 — 4.3 内建函数对象
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <functional>
#include <vector>
// --- 原文片段 [58954498:2] 4.3 内建函数对象 ---
//取反仿函数
void test01()
{
negate<int> n;
cout << n(50) << endl;
}
//加法仿函数
void test02()
{
plus<int> p;
cout << p(10, 20) << endl;
}
//大于仿函数
void test03()
{
vector<int> v;
srand((unsigned int)time(NULL));
for (int i = 0; i < 10; i++){
v.push_back(rand() % 100);
}
for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
cout << *it << " ";
}
cout << endl;
sort(v.begin(), v.end(), greater<int>());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++){
cout << *it << " ";
}
cout << endl;
}
// --- main ---
int main() {
test01();
test02();
test03();
return 0;
}
+184
View File
@@ -0,0 +1,184 @@
// ============================================================================
// p1c09_04_04 — 4.3 内建函数对象
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <functional>
#include <vector>
// --- 原文片段 [58954498:3] 4.3 内建函数对象 ---
//函数适配器bind1st bind2nd
//现在我有这个需求 在遍历容器的时候,我希望将容器中的值全部加上100之后显示出来,怎么做?
//我们直接给函数对象绑定参数 编译阶段就会报错
//for_each(v.begin(), v.end(), bind2nd(myprint(),100));
//如果我们想使用绑定适配器,需要我们自己的函数对象继承binary_function 或者 unary_function
//根据我们函数对象是一元函数对象 还是二元函数对象
class MyPrint :public binary_function<int,int,void>
{
public:
void operator()(int v1,int v2) const
{
cout << "v1 = : " << v1 << " v2 = :" <<v2 << " v1+v2 = :" << (v1 + v2) << endl;
}
};
//1、函数适配器
void test01()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
cout << "请输入起始值:" << endl;
int x;
cin >> x;
for_each(v.begin(), v.end(), bind1st(MyPrint(), x));
//for_each(v.begin(), v.end(), bind2nd( MyPrint(),x ));
}
//总结: bind1st和bind2nd区别?
//bind1st : 将参数绑定为函数对象的第一个参数
//bind2nd : 将参数绑定为函数对象的第二个参数
//bind1st bind2nd将二元函数对象转为一元函数对象
class GreaterThenFive:public unary_function<int,bool>
{
public:
bool operator ()(int v) const
{
return v > 5;
}
};
//2、取反适配器
void test02()
{
vector <int> v;
for (int i = 0; i < 10;i++)
{
v.push_back(i);
}
// vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterThenFive()); //返回第一个大于5的迭代器
// vector<int>::iterator it = find_if(v.begin(), v.end(), not1(GreaterThenFive())); //返回第一个小于5迭代器
//自定义输入
vector<int>::iterator it = find_if(v.begin(), v.end(), not1 ( bind2nd(greater<int>(),5)));
if (it == v.end())
{
cout << "没找到" << endl;
}
else
{
cout << "找到" << *it << endl;
}
//排序 二元函数对象
sort(v.begin(), v.end(), not2(less<int>()));
for_each(v.begin(), v.end(), [](int val){cout << val << " "; });
}
//not1 对一元函数对象取反
//not2 对二元函数对象取反
void MyPrint03(int v,int v2)
{
cout << v + v2<< " ";
}
//3、函数指针适配器 ptr_fun
void test03()
{
vector <int> v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
// ptr_fun( )把一个普通的函数指针适配成函数对象
for_each(v.begin(), v.end(), bind2nd( ptr_fun( MyPrint03 ), 100));
}
//4、成员函数适配器
class Person
{
public:
Person(string name, int age)
{
m_Name = name;
m_Age = age;
}
//打印函数
void ShowPerson(){
cout << "成员函数:" << "Name:" << m_Name << " Age:" << m_Age << endl;
}
void Plus100()
{
m_Age += 100;
}
public:
string m_Name;
int m_Age;
};
void MyPrint04(Person &p)
{
cout << "姓名:" << p.m_Name << " 年龄:" << p.m_Age << endl;
};
void test04()
{
vector <Person>v;
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//for_each(v.begin(), v.end(), MyPrint04);
//利用 mem_fun_ref 将Person内部成员函数适配
for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));
// for_each(v.begin(), v.end(), mem_fun_ref(&Person::Plus100));
// for_each(v.begin(), v.end(), mem_fun_ref(&Person::ShowPerson));
}
void test05(){
vector<Person*> v1;
//创建数据
Person p1("aaa", 10);
Person p2("bbb", 20);
Person p3("ccc", 30);
Person p4("ddd", 40);
v1.push_back(&p1);
v1.push_back(&p2);
v1.push_back(&p3);
v1.push_back(&p4);
for_each(v1.begin(), v1.end(), mem_fun(&Person::ShowPerson));
}
//如果容器存放的是对象指针, 那么用mem_fun
//如果容器中存放的是对象实体,那么用mem_fun_ref
// --- main ---
int main() {
test01();
test02();
test03();
test04();
test05();
return 0;
}
+67
View File
@@ -0,0 +1,67 @@
// ============================================================================
// p1c09_04_01 — 4.1 函数对象
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
// --- 原文片段 [58954498:0] 4.1 函数对象 ---
//函数对象是重载了函数调用符号的类
class MyPrint
{
public:
MyPrint()
{
m_Num = 0;
}
int m_Num;
public:
void operator() (int num)
{
cout << num << endl;
m_Num++;
}
};
//函数对象
//重载了()操作符的类实例化的对象,可以像普通函数那样调用,可以有参数 ,可以有返回值
void test01()
{
MyPrint myPrint;
myPrint(20);
}
// 函数对象超出了普通函数的概念,可以保存函数的调用状态
void test02()
{
MyPrint myPrint;
myPrint(20);
myPrint(20);
myPrint(20);
cout << myPrint.m_Num << endl;
}
void doBusiness(MyPrint print,int num)
{
print(num);
}
//函数对象作为参数
void test03()
{
//参数1:匿名函数对象
doBusiness(MyPrint(),30);
}
// --- main ---
int main() {
test01();
test02();
test03();
return 0;
}
+85
View File
@@ -0,0 +1,85 @@
// ============================================================================
// p1c09_04_02 — 4.2 谓词
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
// --- 原文片段 [58954498:1] 4.2 谓词 ---
class GreaterThenFive
{
public:
bool operator()(int num)
{
return num > 5;
}
};
//一元谓词
void test01()
{
vector<int> v;
for (int i = 0; i < 10;i ++)
{
v.push_back(i);
}
vector<int>::iterator it = find_if(v.begin(), v.end(), GreaterThenFive());
if (it == v.end())
{
cout << "没有找到" << endl;
}
else
{
cout << "找到了: " << *it << endl;
}
}
//二元谓词
class MyCompare
{
public:
bool operator()(int num1, int num2)
{
return num1 > num2;
}
};
void test02()
{
vector<int> v;
v.push_back(10);
v.push_back(40);
v.push_back(20);
v.push_back(90);
v.push_back(60);
//默认从小到大
sort(v.begin(), v.end());
for (vector<int>::iterator it = v.begin(); it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
cout << "----------------------------" << endl;
//使用函数对象改变算法策略,排序从大到小
sort(v.begin(), v.end(),MyCompare());
for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
// --- main ---
int main() {
test01();
test02();
return 0;
}
+101
View File
@@ -0,0 +1,101 @@
// ============================================================================
// p1c09_04_05 — 4.3 常用遍历算法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
// --- 原文片段 [58954498:4] 4.3 常用遍历算法 ---
/*
template<class _InIt,class _Fn1> inline
void for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{
for (; _First != _Last; ++_First)
_Func(*_First);
}
*/
//普通函数
void print01(int val){
cout << val << " ";
}
//函数对象
struct print001{
void operator()(int val){
cout << val << " ";
}
};
//for_each算法基本用法
void test01(){
vector<int> v;
for (int i = 0; i < 10;i++){
v.push_back(i);
}
//遍历算法
for_each(v.begin(), v.end(), print01);
cout << endl;
for_each(v.begin(), v.end(), print001());
cout << endl;
}
struct print02{
print02(){
mCount = 0;
}
void operator()(int val){
cout << val << " ";
mCount++;
}
int mCount;
};
//for_each返回值
void test02(){
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
print02 p = for_each(v.begin(), v.end(), print02());
cout << endl;
cout << p.mCount << endl;
}
struct print03 : public binary_function<int, int, void>{
void operator()(int val,int bindParam) const{
cout << val + bindParam << " ";
}
};
//for_each绑定参数输出
void test03(){
vector<int> v;
for (int i = 0; i < 10; i++){
v.push_back(i);
}
for_each(v.begin(), v.end(), bind2nd(print03(),100));
}
// --- main ---
int main() {
test01();
test02();
test03();
return 0;
}
+91
View File
@@ -0,0 +1,91 @@
// ============================================================================
// p1c09_04_06 — 4.3 常用遍历算法
// 来源:川大 C++/LinuxC wiki「02.Qt方向」
// 说明:本文件由 gen_part1.py 合并页面中的教学片段而成。每个片段用
// // --- 原文片段 [pageId:blockIdx] --- 标注出处。
// 已做最小修正(笔误/平台移植),详见 docs/ERRATA.md。
// ============================================================================
#include <iostream>
using namespace std;
#include <algorithm>
#include <vector>
// --- 原文片段 [58954498:5] 4.3 常用遍历算法 ---
//transform 将一个容器中的值搬运到另一个容器中
/*
template<class _InIt, class _OutIt, class _Fn1> inline
_OutIt _Transform(_InIt _First, _InIt _Last,_OutIt _Dest, _Fn1 _Func)
{
for (; _First != _Last; ++_First, ++_Dest)
*_Dest = _Func(*_First);
return (_Dest);
}
template<class _InIt1,class _InIt2,class _OutIt,class _Fn2> inline
_OutIt _Transform(_InIt1 _First1, _InIt1 _Last1,_InIt2 _First2, _OutIt _Dest, _Fn2 _Func)
{
for (; _First1 != _Last1; ++_First1, ++_First2, ++_Dest)
*_Dest = _Func(*_First1, *_First2);
return (_Dest);
}
*/
struct transformTest01{
int operator()(int val){
return val + 100;
}
};
struct print01{
void operator()(int val){
cout << val << " ";
}
};
void test01(){
vector<int> vSource;
for (int i = 0; i < 10;i ++){
vSource.push_back(i + 1);
}
//目标容器
vector<int> vTarget;
//给vTarget开辟空间
vTarget.resize(vSource.size());
//将vSource中的元素搬运到vTarget
vector<int>::iterator it = transform(vSource.begin(), vSource.end(), vTarget.begin(), transformTest01());
//打印
for_each(vTarget.begin(), vTarget.end(), print01()); cout << endl;
}
//将容器1和容器2中的元素相加放入到第三个容器中
struct transformTest02{
int operator()(int v1,int v2){
return v1 + v2;
}
};
void test02(){
vector<int> vSource1;
vector<int> vSource2;
for (int i = 0; i < 10; i++){
vSource1.push_back(i + 1);
}
//目标容器
vector<int> vTarget;
//给vTarget开辟空间
vTarget.resize(vSource1.size());
transform(vSource1.begin(), vSource1.end(), vSource2.begin(),vTarget.begin(), transformTest02());
//打印
for_each(vTarget.begin(), vTarget.end(), print01()); cout << endl;
}
// --- main ---
int main() {
test01();
test02();
return 0;
}