学习 C++ \text{C++} C++ 三年多了,虽然写了很多代码,但是从来没有系统的去学习过 C++ 11 \text{C++ 11} C++ 11 的新特性,以至于有的特性使用了却不知道是新特性,有的特性见到了却不知道是什么玩意儿,近期打算系统的去整理一下工作中会用到的 C++ 11 \text{C++ 11} C++ 11 的新特性。

关键字

1、 nullptr \text{nullptr} nullptr

nullptr \text{nullptr} nullptr 的出现是为了取代 NULL \text{NULL} NULL,避免 NULL \text{NULL} NULL 的二义性。

2、 auto \text{auto} auto

编译器根据初始值来推算变量的类型,常用于搭配迭代器使用。

auto xgg = 3;
auto it = vec.begin();

3、 decltype \text{decltype} decltype

decltype(表达式) \text{decltype(表达式)} decltype(表达式) 可以推导出表达式类型,与 using/typedef \text{using/typedef} using/typedef 搭配使用可以定义类型。

decltype(xgg) ergou = 4;
using size_t = decltype(sizeof(0));

也可以用来对匿名类型重用,结合 using/typedef \text{using/typedef} using/typedef 搭配使用可以重命名匿名类型,在泛型编程中结合 a u t o auto auto 也有妙用,暂不赘述。
ex: \text{ex:} ex:

struct
{
	int x;
} tmp;

decltype(tmp) tmp_;
using t = decltype(tmp);

4、 constexpr \text{constexpr} constexpr

类似于 const \text{const} const,既可以修饰变量,也可以修饰函数,区别在于, constexpr \text{constexpr} constexpr 修饰的函数生效于编译阶段,而不是运行时,重点在于修饰函数使其在编译期大幅度被解释,必须保证编译期可以得到结果,即字面常量,不可是运行时才能获取的结果。

5、 using \text{using} using

用处有三:
一、声明命名空间;
二、取代 typedef \text{typedef} typedef
三、父类同名函数在子类中得以重载方式使用。

容器

1、 unordered_map \text{unordered\_map} unordered_map

unordered_map \text{unordered\_map} unordered_map map \text{map} map 功能类似,均是存储 key-value \text{key-value} key-value 键值对,帮助快速检索 key \text{key} key 对应的值。

map \text{map} map 的底层实现是红黑树,红黑树具有有序性,所以 map \text{map} map 内部元素都是有序的,插入与查找都十分快,达到 O(lgn) \text{O(lgn)} O(lgn) unordered_map \text{unordered\_map} unordered_map 的底层实现则是 hash \text{hash} hash,不具有有序性,所以内部元素都是无序的,查找速度可以达到 O(1) \text{O(1)} O(1),但是建立 h a s h hash hash 表花费时间较多。

在内存上, unordered_map \text{unordered\_map} unordered_map 的消耗比较高,尤其是数据重复率较低的情况下,内存消耗尤为明显。

unordered_map \text{unordered\_map} unordered_map 的使用上和 map \text{map} map 是完全一样的,只不过头文件需要导入 #include &lt;unordered_map&gt; \text{\#include &lt;unordered\_map&gt;} #include <unordered_map>

语法

1、 f o r for for

2、 &amp;&amp; \text{\&amp;\&amp;} &&(右值引用)

3、 L a m d a Lamda Lamda 表达式

4、智能指针

std::unique_ptr \text{std::unique\_ptr} std::unique_ptr

unique_ptr \text{unique\_ptr} unique_ptr 独占所指向的对象,同一时刻只能有一个 unique_ptr \text{unique\_ptr} unique_ptr 指向给定对象(通过禁止拷贝语义,只有移动语义来实现)。

标准库早期版本中定义了 auto_ptr \text{auto\_ptr} auto_ptr,它具有 unique_ptr \text{unique\_ptr} unique_ptr 的部分特征,但不是全部,例如,不能在容器中保存 auto_ptr \text{auto\_ptr} auto_ptr,也不能从函数中返回 auto_ptr \text{auto\_ptr} auto_ptr

std::unique_ptr<T> upt;
upt.reset(new T());
std::unique_ptr<X> upx(new X());

5、变长参数列表

函数

1、 std::move() \text{std::move()} std::move()

2、 forword() \text{forword()} forword()

1、 std::chrono \text{std::chrono} std::chrono

这是一个时间库,源于 b o o s t boost boost,需要头文件 #include &lt;chrono&gt; \text{\#include &lt;chrono&gt;} #include <chrono>

duration \text{duration} duration

表示一段时间,需要两个模板参数,一个是时间数值类型,一个是时间单位,数值类型可以是 i n t / d o u b l e / f l o a t int/double/float int/double/float 等,时间单位通过 s t d : r a t i o &lt; x , y &gt; std:ratio&lt;x, y&gt; std:ratio<x,y> 表示。

std:ratio<60, 1>;	//  m
std:ratio<3600, 1>;	//	h
std:ratio<1, 1000000>;	//	微妙(1/1000000 s)

std::chrono::duration<int> seconds = 1;					// 1s
std::chrono::duration<int, std::ratio<3600>> hours = 2;	//	2h
std::chrono::duration<int, std::ratio<1, 1000>> microseconds = 30;	//	30ms

std::chrono::duration \text{std::chrono::duration} std::chrono::duration 还提供了不同 duration \text{duration} duration 之间的转换函数—— duration_cast \text{duration\_cast} duration_cast

std::chrono::duration_cast<hours_type>(seconds);	//	将 1s 转换为小时单位

time_point \text{time\_point} time_point

表示一个具体时间,两个模板参数,一个是 clock \text{clock} clock,一个是 duration \text{duration} duration,默认是系统时钟,单位是秒。

std::chrono::time_point<system_clock, duration<int>> tp_s(duration<int>(1));

同样也提供了 time_point_cast \text{time\_point\_cast} time_point_cast,用来转换不同时间。

time_point_cast<days_type>(system_clock::now());

clock \text{clock} clock

每个 clock \text{clock} clock 类下都有 time_point/duration \text{time\_point/duration} time_point/duration

system_clock \text{system\_clock} system_clock

表示当前系统时钟,与系统进程中 n o w ( ) now() now() 一致。

now();			//	当前时间
to_time_t();	//	time_point => time_t
from_time_t();	//	time_t => time_point	
steady_clock \text{steady\_clock} steady_clock

表示稳定的时间间隔,后一次调用 now() \text{now()} now() 总是比前一次大,哪怕中途修改了系统时间,也不会影响 now() \text{now()} now() 结果, tick \text{tick} tick 保证稳定的时间间隔。

high_resolution_clock \text{high\_resolution\_clock} high_resolution_clock

表示高精度时钟,实际上它只是一个 typedef \text{typedef} typedef

参考整理

C++11 std::chrono库详解