# Effective Modern C++:unique_ptr

You should include when using smart pointers

### unique_ptr

Just as its name means, unique_ptr guarantees that there is only one pointer point to a specific memory block. So it cannot be copied by only moved.

Init a unique_ptr

unique_ptr<int> p{ new int };
unique_ptr<int> p{ new int{4} };


As you can see, smart pointers are wraps of normal pointers, we call them “raw” pointers.

# Effective Modern C++:constexpr, thread safe

constexpr is more strict than const

int a;
const b = a;//correct
constexpr c = a;//wrong, because a has no value yet


When declaring a constexpr, it should be assigned with a already existed value.

you can get compile-time result when using constexpr function

# Effective Modern C++:alias,enum,override

### Alias is preferred to typedef

It is clearer:

typedef void (*FP)(int, const std::string&); // old way: typedef
using FP = void (*)(int, const std::string&); // new way: alias


It support template:

template<typename T>
using MyAllocList = std::list<T, MyAlloc<T>>;


### enum is preferred to be scoped

Comparison between the old and new way:

# Data binding of ListBox and string list in C#

### DataBinding and MVVM Pattern

When dealing with GUI, normally we use the so-called MVC pattern, which means we manager a set of data as Model and we demonstrate them as View, the Contoller controls the logic of showing the data:

The main problem of this pattern is that most of the controller’s works are simple “get” and “set” functions between the data and the gui Like:

# Effective Modern C++:decltyle,use {}

### decltype用法:

In C++11, perhaps the primary use for decltypeis declaring function templates where the function’s return typedepends on its parameter types.

decltype 用于获得某个未知变量的类型，在什么情况下我们不知道变量的类型呢？ 当然是使用template或auto的时候：

template<typename T>
void funcValue(T param)
{
auto subParam = param;
decltype(auto) subSubParam = subParam;
}


# Effective Modern C++:Template and auto

The type deduction of Template and auto

The type deduction of template will ignore one level of reference and pointer

Example 1:

Input argument as reference

template<typename T>
void f(T& param);

int x = 27; // x is an int
const int cx = x; // cx is a const int
const int& rx = x; // rx is a reference to x as a const int
f(x);// T is int
f(cx);// T is const int, param's typeis const int&
f(rx);// T is const int, param's type is const int&


# PCA and Face Recognition - Eigen Face

PCA (Principal component analysis), just as its name shows, it computes the data set’s internal structure, its “principal components”.

Considering a set of 2 dimensional data, for one data point, it has 2 dimensions $x_1$ and $x_2$ . Now we get n such data points . What is the relationship between the first dimension $x_1$ and the second dimension $x_2$ ? We compute the so called covariance:

the covariance shows how strong is the relationship between $x_1$ and $x_2$. Its logic is the same as Chebyshev’s sum inequality:

# Switch to another window using C#

[DllImport("user32.dll")]
public static extern void SwitchToThisWindow(IntPtr hWnd,bool turnon);
String ProcWindow = "wechat";
private void switchToWechart()
{
Process[] procs = Process.GetProcessesByName(ProcWindow);
foreach (Process proc in procs)
{
//switch to process by name
SwitchToThisWindow(proc.MainWindowHandle, true);
}
}