Utilites

简介

• Class pair<> and class tuple<>

• Smart pointer classes (class shared_ptr<> and class unique_ptr)

• Numeric limits

• Type traits and type utilities

• Auxiliary functions (for example, min(), max(), and swap())

• Class ratio<>

• Clocks and timers

• Some important C functions

pairs

Utilites_第1张图片
pair操作

1,具有成员模板构造函数(当pair类型不同但可以隐式类型转换时调用)

2,pair的成员的类型的拷贝构造函数必须是常量引用

tuples

扩展pair,使得tuple支持多个元素

Utilites_第2张图片
tuple操作

1,使用get<0>(t)等获取每个元素

2,赋值操作时右操作数必须显式的为tuple类型,不支持隐式转换

tuple_size::value返回tuple的元素个数

tuple_element::type返回tuple的第index个元素的类型

tuple_cat()将多个tuple组合为一个整体

shared_ptr

多个指针共享同一资源,当最后一个指针销毁时会将资源释放。

Utilites_第3张图片
shared_ptr使用1
Utilites_第4张图片
shared_ptr使用2
Utilites_第5张图片
weak_ptr使用

1,weak_ptr的的拷贝和赋值不会增加或减少对应的shared_ptr的引用计数

2,使用lock()函数获取weak_ptr绑定的shared_ptr

Utilites_第6张图片
原子操作
Utilites_第7张图片
unique_ptr操作
Utilites_第8张图片
内建类型的最小大小

数值极限

Utilites_第9张图片
Utilites_第10张图片
Utilites_第11张图片

类型萃取

A type trait provides a way to deal with the properties of a type. It is a template, which at compile time yields a specific type or value based on one or more passed template arguments, which are usually types.

Utilites_第12张图片
Utilites_第13张图片
Utilites_第14张图片
Utilites_第15张图片
Utilites_第16张图片

引用转换

std::reference_wrapper<>定义在,将参数转化为引用类型来适用函数模板std::vector> coll; // OK

比较辅助函数

定义在

Utilites_第17张图片

std::swap函数定义在

编译时分数计算类

ratio<>定义在

例如ratio<3, 5>表示五分之三,类成员num,den分别表示分子和分母

Utilites_第18张图片

例如std::ratio_add, std::ratio<2,6>>::type为std::ratio<13, 21>

Utilites_第19张图片

例如std::nano,等价于std::ratio<1, 1000000000LL>

你可能感兴趣的:(Utilites)