1.引入nullptr
改进NULL,隐式推导返回类型(专门用来区分空指针和0)
2.自动类型推导auto和decltype
auto x = 1;
auto y = 2;
decltype(x + y) z;
3.基于范围的for循环
vector<int>ar({1,2,3,4,5,6});
for(auto i : ar) {
cout<<i;
}
或
for(auto &i : ar) {
i = 10;
cout<<i;
}
4.智能指针
shared_ptr<int>p1 = make_shared<int>();
防止内存泄漏
5.类初始化列表
struct A {
int a;
float b;
};
struct B {
B(int _a, float _b): a(_a), b(_b) {}
private:
int a;
float b;
};
A a {1, 1.1}; // 统一的初始化语法
B b {2, 2.2};
6.委托构造函数
class Base {
public:
int value1;
int value2;
Base() {
value1 = 1;
}
Base(int value) : Base() { // 委托 Base() 构造函数
value2 = 2;
}
};
7.lambda表达式
[ caputrue ] ( params ) opt -> ret { body; };
8.右值引用和move语义
简单记:能取地址的叫做左值,不能则叫做右值。
9.语言级线程支持
std::thread
std::mutex/std::unique_lock
std::future/std::packaged_task
std::condition_variable
代码编译需要使用 -pthread 选项
10.正则表达式
11.新增容器array
12.新增容器forward_list
参考:
C++特性快速一览
本文介绍了C++11的一些重要更新,包括 nullptr 的引入以改进 NULL,自动类型推导 auto 和 decltype,基于范围的 for 循环简化迭代,智能指针 shared_ptr 防止内存泄漏,类初始化列表,委托构造函数,lambda 表达式的使用,右值引用和 move 语义,语言级线程支持,以及正则表达式等。这些特性极大地提高了C++的效率和安全性。
2万+

被折叠的 条评论
为什么被折叠?



