# Standard A* Search Algorithm in C++

• g(n)：为到达顶点n，当前已有的花费（cost so far to reach n）
• h(n)：一个 从任意顶点 到 目标顶点 的估计花费（estimated cost to goal from n）
• f(n)：从 初始顶点 到 目标顶点 的估计花费（estimated total cost from the starting node to goal through n）

f(n) = h(n) + g(n)

A —— B —— C

A —— B权重为30，B —— C权重为40，

A —— C的直线距离为50，但是并没有A——C这条边（即不能直接从A开始，只经过一条边就到达C）

h(A) = 50

h(B) = 40

h(C) = 0

froniter中的数据如下：

current from g(current) h(current)
A A 0 50

# A simple EventEmitter in C++

EventEmitter coming with node.js is handy most of the time, and I implement this useful utility in C++ after finishing writing Functor. Yes, we need Functor to make it much more easier to write this fabulous utility. (Why don't you use lamdba? I'll talk about that later in this post)

It's extremely simple to use EventEmitter in node.js

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');

And the C++ version of EventEmitter need to be as easy as it is in node.js. As a matter of fact, this class made it.

#include <iostream>
#include <sstream>
#include <vector>
#include "EventEmitter.hpp"

using namespace std;

class emitter : public EventEmitter {
};

int main(int argc, const char * argv[]) {
emitter emitter;
emitter.on("event", [&emitter](int data) {
ostringstream osstream;
osstream << "data: " << data << '\n';
std::cout << osstream.str();
});

for (int i = 0; i < 10; i++) {
emitter.emit("event", i);
});
}

for (auto &t : threads) t.join();
}


# A simple EventEmitter in C++

node.js里面的EventEmitter非常好用，于是上次写完Functor之后，就顺理成章的写了这个EventEmitter，或者说，就是为了实现这个EventEmitter才写的Functor（后文会提到为什么要Functor，毕竟如果只看Functor本身的话，还不如直接用lamdba方便）。

const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();
myEmitter.on('event', () => {
console.log('an event occurred!');
});
myEmitter.emit('event');

#include <iostream>
#include <sstream>
#include <vector>
#include "EventEmitter.hpp"

using namespace std;

class emitter : public EventEmitter {
};

int main(int argc, const char * argv[]) {
emitter emitter;
emitter.on("event", [&emitter](int data) {
ostringstream osstream;
osstream << "data: " << data << '\n';
std::cout << osstream.str();
});

for (int i = 0; i < 10; i++) {
emitter.emit("event", i);
});
}

for (auto &t : threads) t.join();
}


# Module Check——Usage of std::enable_if

class Intermediate {
public:
template <typename T>
Intermediate(const T& object, double value) {
object.method(value);
}
};


class A {
public:
void method(double d) const {
std::cout<<d<<'\n';
}
};

class B {
public:
void method(int i) const {
std::cout<<i<<'\n';
}
};


Intermediate inter_a_1(a, 2.33); // print 2.33
Intermediate inter_b_1(b, 2.33); // print 2