前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 11 新特性

C++ 11 新特性

作者头像
f_zyj
发布2019-05-27 09:02:10
7320
发布2019-05-27 09:02:10
举报

关键字

1、nullptr\text{nullptr}nullptr

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

2、auto\text{auto}auto

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

代码语言:javascript
复制
auto xgg = 3;
auto it = vec.begin();

3、decltype\text{decltype}decltype

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

代码语言:javascript
复制
decltype(xgg) ergou = 4;
using size_t = decltype(sizeof(0));

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

代码语言:javascript
复制
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),但是建立 hashhashhash 表花费时间较多。

在内存上,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、forforfor

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

3、LamdaLamdaLamda 表达式

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。

代码语言:javascript
复制
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

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

duration\text{duration}duration

表示一段时间,需要两个模板参数,一个是时间数值类型,一个是时间单位,数值类型可以是 int/double/floatint/double/floatint/double/float 等,时间单位通过 std:ratio&lt;x,y&gt;std:ratio&lt;x, y&gt;std:ratio<x,y> 表示。

代码语言:javascript
复制
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。

代码语言:javascript
复制
std::chrono::duration_cast<hours_type>(seconds);	//	将 1s 转换为小时单位
time_point\text{time\_point}time_point

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

代码语言:javascript
复制
std::chrono::time_point<system_clock, duration<int>> tp_s(duration<int>(1));

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

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年01月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 关键字
    • 1、nullptr\text{nullptr}nullptr
      • 2、auto\text{auto}auto
        • 3、decltype\text{decltype}decltype
          • 4、constexpr\text{constexpr}constexpr
            • 5、using\text{using}using
            • 容器
              • 1、unordered_map\text{unordered\_map}unordered_map
              • 语法
                • 1、forforfor
                  • 2、&amp;&amp;\text{\&amp;\&amp;}&&(右值引用)
                    • 3、LamdaLamdaLamda 表达式
                      • 4、智能指针
                        • std::unique_ptr\text{std::unique\_ptr}std::unique_ptr
                      • 5、变长参数列表
                      • 函数
                        • 1、std::move()\text{std::move()}std::move()
                          • 2、forword()\text{forword()}forword()
                            • 1、std::chrono\text{std::chrono}std::chrono
                              • duration\text{duration}duration
                              • time_point\text{time\_point}time_point
                              • clock\text{clock}clock
                          相关产品与服务
                          容器服务
                          腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档