C++11新特性(四):库特性

C++11新特性

库特性

std::move

用于实现移动语义的函数,完成左值到右值的转换,参见C++11新特性(一)

std::forward

用于实现完美转发的函数,直接将参数的类型传递到参数中,右值不会退化为左值

std::thread

thread是引入的线程库,用于创建线程,并发编程。

std::to_string()

to_string能够完成的作用是将数字转换为字符串。stoi()函数将字符转为数字,一定要注意提前检查字符一定是数字,而不是数字意外的其它字符,否则会程序崩溃,不安全,最好提前入口检查。

type_traits类型特性库

类型特性库包含了一组编译时检查类型特性的工具,可以配合static_assert使用。

// 基本类型判断包括整型、浮点型等
std::is_integral<T>::value // 检查T是否是整数类型,value是一个静态常量,其值为true或false
// 类型修饰
std::remove_const<T> // 移除类型T的const修饰
// 类型转换
std::add_const<T> // 为类型T添加const修饰
// 类型特性检查
std::is_same<T, U> // 检查两个类型是否相同
// 条件类型
std::conditional<Condition, T, F> // Condition为true, 则类型为T,否则为F

智能指针

智能指针位于#include <memory> 一般有三种,share_ptr共享指针,用于多个读;weak_ptr弱指针,解决共享指针循环引用计数无法下降问题; unique_ptr独占指针,用于独享资源。

std::chrono

chrono库提供一系列定时器函数

tuples元组

tuple<int, int, int> t{1, 2, 3};
auto tu = make_tuple(1, 2 ,3);
get<0>(tu); // 获取元组第1个元素
get<1>(tu); // 获取元组第2个元素
get<2>(tu); // 获取元组第3个元素

std::tie绑定

tie提供对象到tuple或者pair的绑定

tuple<int, int, int> t{1, 2, 3};
int id;
tie(std::ignore, id, std::ignore) = t; // 此时就会将id绑定到第二个元素上 id:2
pair<int, int> p{1, 2};
int x, y;
tie(x, y) =  p;
// c++17之后可以直接使用结构绑定
auto [id1, id2] = p;

std::array数组容器

数组容器,有强制类型检查,连续存储可以告诉访问,可以用于存储特定个数元素。vector是可以扩容的

std::array<int, 5> intArray{1, 2, 3, 4, 5};

unordered containers未排序容器

// 底层都是红黑树实现
unordered_map<key, value>;
unordered_multiset<key, value>;
unordered_set<key, value>;
unordered_multiset<key, value>;

std::make_shared

这个库函数用于生成共享指针

std::ref

ref创建容器存储对象的引用

int val = 0;
auto re = std::ref(val);

std::async

异步函数,用于异步任务创建

std::begin()和std::end()

这两个库函数用于获取首尾迭代器

内存模型

内存模型提供对原子操作和线程的支持。

posted @ 2024-08-24 11:12  LemHou  阅读(72)  评论(0)    收藏  举报