1llusion


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

Const限定符

发表于 2018-07-05 | 分类于 C++ |

Intro

有时候我们希望定义这样一种变量,它的值不能被改变。为了满足这一要求,可以用关键字const对变量的类型加以限定。

因为const对象一旦创建后其值就不能再改变,所以const对象必须初始化。

默认状态下,const对象仅在文件内有效

某些时候有这样一种const变量,它的初始值不是一个常量表达式,但又确实有必要在文件间共享。解决的办法是,对于const变量不管是声明还是定义都添加extern关键字,这样只需定义一次就可以了:

1
2
extern const int bufSize = fcn();
extern const int bufSize;

如果想在多个文件之间共享const对象,必须在变量的定义之前添加extern关键字。

const的引用

称之为对常量的引用(reference to const)。

引用的类型必须与其引用对象的类型一致,但是有两个例外。

第一种例外情况就是在初始化常量引用的时允许用任意表达式作为初始值,只要该表达式的结果能转换成应用的类型即可。尤其,允许为一个常量引用绑定非常量的对象

对const的引用可能引用一个并非const的对象

阅读全文 »

GDB

发表于 2018-06-12 |

What is GDB?

GDB, the GNU Project debugger, allows you to see what is going on `inside’ another program while it executes – or what another program was doing at the moment it crashed.

GDB can do four main kinds of things (plus other things in support of these) to help you catch bugs in the act:

  • Start your program, specifying anything that might affect its behavior.
  • Make your program stop on specified conditions.
  • Examine what has happened, when your program has stopped.
  • Change things in your program, so you can experiment with correcting the effects of one bug and go on to learn about another.

Those programs might be executing on the same machine as GDB (native), on another machine (remote), or on a simulator. GDB can run on most popular UNIX and Microsoft Windows variants, as well as on Mac OS X.

现代操作系统中,用core dump 表示当程序异常终止或崩溃时,将进程此时的内存中的内容拷贝到磁盘文件中存储,以方便编程人员调试。core dump 中包含了程序运行时的内存,寄存器状态,堆栈指针,内存管理信息等。

Core is like a black box which is use to get the last moment information about the crashed plane.

core dump文件的生成方法

通过ulimit -c查看是否打开生成core dump

1
ulimit -c unlimited
阅读全文 »

Elaticsearch 6.0 同义词配置说明

发表于 2018-05-20 | 分类于 检索 |

准备工作

最近,有一个小需求如下:

给2段文字,找出两段文字中高度重合词的同义词。

由于需求比较简单(意思也就是这个需求不是那么重要),所以就想省点事,而且最近又在折腾elasticsearch。所以就Google查了下相关的文章。当然这一篇就借鉴了很多前辈的经验。

Elasticsearch

这个参考文档安装就可以了,比较简单。elasticsearch

分词

因为基于elasticsearch,所以可以直接使用对应的中文分词插件elasticsearch-analysis-ik

同义词词典

同样基于elasticsearch,所以首先我们要先看下es的相关文档:Synonym Token Filter

Two synonym formats are supported: Solr, WordNet.

也就是es的同义词词典格式目前支持两种格式,我们这里基本上仅使用Solr这种模式的同义词。

阅读全文 »

Lazy initialization and double-checked locking

发表于 2018-03-01 | 分类于 设计模式 |

Lazy initialization

在程序设计中,惰性初始是一种拖延战术。在第一次需求出现以前,先延迟创建物件、计算值或其他昂贵程序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.*;

public class Fruit
{
private static final Map<String,Fruit> types = new HashMap<String,Fruit>();
private final String type;

// using a private constructor to force use of the factory method.
private Fruit(String type) {
this.type = type;
}

/**
* Lazy Factory method, gets the Fruit instance associated with a
* certain type. Instantiates new ones as needed.
* @param type Any string that describes a fruit type, e.g. "apple"
* @return The Fruit instance associated with that type.
*/
public static synchronized Fruit getFruit(String type) {
if(!types.containsKey(type))
types.put(type, new Fruit(type)); // Lazy initialization
return types.get(type);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Fruit:
def __init__(self, type):
self.type = type

class Fruits:
def __init__(self):
self.types = {}

def get_fruit(self, type):
if type not in self.types:
self.types[type] = Fruit(type)

return self.types[type]

if __name__ == '__main__':
fruits = Fruits()
print fruits.get_fruit('Apple')
print fruits.get_fruit('Lime’)

Double-checked locking

In software engineering, double-checked locking (also known as “double-checked locking optimization”) is a software design pattern used to reduce the overhead of acquiring a lock by first testing the locking criterion (the “lock hint”) without actually acquiring the lock. Only if the locking criterion check indicates that locking is required does the actual locking logic proceed.

阅读全文 »

C++ Primer阅读笔记二

发表于 2017-07-03 | 分类于 C++ |

字符串、向量和数组

表达式

语句

Reference

C++ Primer第五版

代理模式

发表于 2017-06-24 | 分类于 设计模式 |

代理模式

我们买房子或者租房子,我们可以自己去找房源,也可以通过房屋中介来完成。

意图:为其他对象提供一种代理以控制对这个对象的访问。

动机:对一个对象进行访问控制的一个原因是为了只有在我们确实需要这个对象时才对它进行创建和初始化。

参与者

  • Proxy

    保存一个引用使得代理可以访问实体。如RealSubject和Subject的接口相同,Proxy会引用Subject。

    提供一个与Subject的接口相同的接口,这样代理就可以来替代实体。

    控制对实体的存取,并可能负责创建和删除它。

    其他功能依赖于代理的类型:

    • Remote Proxy负责对请求及其参数进行编码,并向不同地址空间中的实体发送已编码的请求。
    • Virtual Proxy可以缓存实体的附加信息,以便延迟对它的访问。
    • Protection Proxy检查调用者是否具有实现一个请求所必须的访问权限
  • Subject

    定义RealSubject和Proxy的共用接口,这样就在任何使用RealSubject的地方都可以使用Proxy。

  • Real Subject

    定义Proxy 所代表的实体。

阅读全文 »

Reactor vs. Proactor

发表于 2017-05-30 | 分类于 设计模式 |

Reactor

反应堆

The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers.

All reactor systems are single threaded by definition, but can exist in a multithreaded environment.

Event Demultiplexes 事件多路分离器。

In general, I/O multiplexing mechanisms rely on an event demultiplexor [1, 3], an object that dispatches I/O events from a limited number of sources to the appropriate read/write event handlers. The developer registers interest in specific events and provides event handlers, or callbacks. The event demultiplexor delivers the requested events to the event handlers.

两个与事件分离器有关的模式是Reactor和Proactor。Reactor模式采用同步IO,而Proactor采用异步IO。

阅读全文 »

I/O和文件

发表于 2017-05-28 | 分类于 操作系统 |

CPU Scheduling

发表于 2017-05-24 | 分类于 操作系统 |

Intro

The success of CPU scheduling depends on an observed property of processes:

process execution consists of a cycle of CPU execution and I/O wait.

Processes alternate between these two states. Process execution begins with a CPU burst.

That is followed by an I/O burst, which is followed by another CPU burst, then another I/O burst, and so on.

CPU调度的成功取决于一个观察到的属性的过程:

进程的执行由CPU执行周期和I/O等待组成,进程将在两种状态中交替执行。

An I/O-bound program typically has many short CPU bursts. A CPU-bound program might have a few long CPU bursts. This distribution can be important in the selection of an appropriate CPU-scheduling algorithm.

一个I/O-bound程序通常会有很短的CPU 突发,一个 CPU-bound程序可能会有稍微有点长的CPU突发。

Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be executed. The selection process is carried out by the short-term scheduler, or CPU scheduler. The scheduler selects a process from the processes in memory that are ready to execute and allocates the CPU to that process.

阅读全文 »

关于正则

发表于 2017-05-18 |

Intro

最近工作上遇到一些字符串匹配问题,自然就用到了正则。很早以前自己翻过一些正则相关的书以及资料,但是最终如果没有应用到实际场景中,渐渐就会忘了。

Regular Expression 正则表达式,是计算机科学的一个概念。正则表达式使用单个字符来描述、匹配一系列匹配某个句法规则的字符串。

Repeaters

A repeater indicates how many times the previous character has to be repeated. When put just after a group, it indicates how many times the group has to be repeated.

* Zero or more times.
+ One or more times.
? Zero or one times.
{m,n} At least m and at most n times.

Repeaters are greedy.

阅读全文 »
12…4
一

一

One more light goes out.

33 日志
12 分类
18 标签
© 2018 一
由 Hexo 强力驱动
|
主题 — NexT.Mist