0%

Item 21: Always have comparison functions return false for equal values.

严格弱序( strict weak ordering )

先补充下严格弱序的概念: 对两个变量 xy

  • x > y 等同于 y < x
  • x == y 等同于 !(x < y) && !(x > y)

要想严格弱序,就需要遵循如下规则:

  • 每个变量值必须等于其本身(irreflexivity):x < x 永远不能为 true
  • 不对称性(asymmetry):如果 x < y,那么 y < x 就不能为 true
  • 有序性必须可传递性:如果 x < y 并且 y < z,那么 x < z
  • 值相同必须具有可传递性:如果 x == y 并且 y == z,那么 x == z
阅读全文 »

介绍

结构

什么是Reactor模式

Reactor模式结构

  • Handles:
  • Synchronous Event Demultiplexer
  • Initiation Dispatcher
  • Event Handler
  • Concrete Event Handler

Reactor模式模块之间的交互

Reactor模式实现

Reactor模式优点

Reactor模式的缺点

  • Efficiency
  • Programming simplicity
  • Portability

Item9. Choose carefully among easing options.

一、删除特定值

  1. 对于 vectorstringdeque

    最好使用 erase-remove习惯用法:

    1
    c.erase(remove(c.begin(), c.end(), 1963, c.end()));
  2. 对于 list 容器

    直接使用 remove 方法:

    1
    c.remove(1963);
  3. 对于标准关联容器

    直接使用 erase 方法:

    1
    c.erase(1963)
阅读全文 »

介绍

Disk Arbitration framework 是一个基于 Core Foundation 的低级框架。会在磁盘出现和消失时通知您的应用程序,并让您的应用程序影响该过程。借助 Disk Arbitration,我们可以:

  • 检测何时出现新磁盘
  • 阻止挂载
  • 使用不同的标志或在不同的安装点上安装卷
  • 卸载卷
  • 观察卷的变化
阅读全文 »

焦点事件

当焦点从一个 widget 移动到另一个 widget 时,会触发 QFocusEvent 事件,这个事件会被发送给原焦点窗口和当前焦点窗口,原焦点窗口执行 focusOutEvent() ,新焦点窗口执行 focusInEvent()。 相关函数如下:

1
2
void focusInEvent(QFocusEvent *event) override;
void focusOutEvent(QFocusEvent *event) override;

焦点策略

只有可获取焦点的窗口,才有机会成为焦点窗口。比如QWidget 默认策略是 Qt::NoFocus 所以 QWidget 默认不获取焦点。Qt提供了如下接口:

1
void QWidget::setFocusPolicy(Qt::FocusPolicy policy);
阅读全文 »

内存分配

std::make_shared 执行一次堆分配,而调用std::shared_ptr 构造函数执行两次

在一个典型的实现中 std::shared_ptr 管理两个实体:

  • 控制块(存储元数据,如引用计数、类型擦除删除器等)
  • 被管理的对象

控制块是一个动态分配的对象,它包含:

  • 指向托管对象的指针或托管对象本身;
  • 删除器 (类型擦除)
  • 分配器 (类型擦除)
  • 拥有被管理对象的 shared_ptr的数量
  • 引用托管对象的 weak_ptr 的数量

std::make_shared执行一次堆分配,计算控制块和数据所需的总空间。在另一种情况 std::shared_ptr<Obj>(new Obj("foo"))下执行两次, new Obj("foo")为托管数据调用堆分配,std::shared_ptr构造函数为控制块执行另一个堆分配。

阅读全文 »

.dSYM

.dSYM (debugging SYMbols) 又称为调试符号表,是苹果为了方便调试和定位问题而使用的一种调试方案,本质上使用的是起源于贝尔实验室的 DWARFDebugging With Attributed Record Formats),其在.xcarchive目录中的层次结构为:

1
2
3
4
5
6
.xcarchive
--dSYMs
|--Your.app.dSYM
|--Contents
|--Resources
|--DWARF
阅读全文 »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
➜ sudo apt update 
命中:1 https://pro-driver-packages.uniontech.com eagle InRelease
获取:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease [269 kB]
命中:3 http://packages.microsoft.com/repos/code stable InRelease
命中:4 https://home-packages.chinauos.com/home plum InRelease
命中:5 https://home-packages.chinauos.com/home plum/beta InRelease
命中:6 https://home-packages.chinauos.com/printer eagle InRelease
错误:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease
由于没有公钥,无法验证下列签名: NO_PUBKEY 871920D1991BC93C
命中:7 https://home-store-img.uniontech.com/appstore eagle InRelease
正在读取软件包列表... 完成
W: GPG 错误:http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY 871920D1991BC93C
E: 仓库 “http://mirrors.tuna.tsinghua.edu.cn/ubuntu hirsute InRelease” 没有数字签名。
N: 无法安全地用该源进行更新,所以默认禁用该源。
N: 参见 apt-secure(8) 手册以了解仓库创建和用户配置方面的细节。

执行:

1
➜ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 871920D1991BC93C