。
nullptr\text{nullptr}nullptr 的出现是为了取代 NULL\text{NULL}NULL,避免 NULL\text{NULL}NULL 的二义性。
编译器根据初始值来推算变量的类型,常用于搭配迭代器使用。
auto xgg = 3;
auto it = vec.begin();
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 搭配使用可以重命名匿名类型,在泛型编程中结合 autoautoauto 也有妙用,暂不赘述。 ex:\text{ex:}ex:
struct
{
int x;
} tmp;
decltype(tmp) tmp_;
using t = decltype(tmp);
类似于 const\text{const}const,既可以修饰变量,也可以修饰函数,区别在于,constexpr\text{constexpr}constexpr 修饰的函数生效于编译阶段,而不是运行时,重点在于修饰函数使其在编译期大幅度被解释,必须保证编译期可以得到结果,即字面常量,不可是运行时才能获取的结果。
用处有三: 一、声明命名空间; 二、取代 typedef\text{typedef}typedef; 三、父类同名函数在子类中得以重载方式使用。
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),但是建立 hashhashhash 表花费时间较多。
在内存上,unordered_map\text{unordered\_map}unordered_map 的消耗比较高,尤其是数据重复率较低的情况下,内存消耗尤为明显。
unordered_map\text{unordered\_map}unordered_map 的使用上和 map\text{map}map 是完全一样的,只不过头文件需要导入 #include <unordered_map>\text{\#include <unordered\_map>}#include <unordered_map>。
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());
这是一个时间库,源于 boostboostboost,需要头文件 #include <chrono>\text{\#include <chrono>}#include <chrono>。
表示一段时间,需要两个模板参数,一个是时间数值类型,一个是时间单位,数值类型可以是 int/double/floatint/double/floatint/double/float 等,时间单位通过 std:ratio<x,y>std:ratio<x, y>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 转换为小时单位
表示一个具体时间,两个模板参数,一个是 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 类下都有 time_point/duration\text{time\_point/duration}time_point/duration。
表示当前系统时钟,与系统进程中 now()now()now() 一致。
now(); // 当前时间
to_time_t(); // time_point => time_t
from_time_t(); // time_t => time_point
表示稳定的时间间隔,后一次调用 now()\text{now()}now() 总是比前一次大,哪怕中途修改了系统时间,也不会影响 now()\text{now()}now() 结果,tick\text{tick}tick 保证稳定的时间间隔。
表示高精度时钟,实际上它只是一个 typedef\text{typedef}typedef。