在文章[1]中展示了c++11的最新特性,经过了13年的时间,c++11比c++98更改了一些新的特性,相当于一门新的语言。
1吸收了当前其他流行语言的一些特性。
1.1 比如可以使用auto来不指定变量类型,和python、matlab相似。
auto x=0; //x has type int because 0 is int
auto c='a'; //char
auto d=0.5; //double
auto national_debt=14400000000000LL;//long long
1.2 foreach的使用
和scala或者python等类似,被称为lambda表达式
Lambda
int main()
{
char s[]="Hello World!";
int Uppercase = 0; //modified by the lambda
for_each(s, s+sizeof(s), [&Uppercase] (char c) {
if (isupper(c))
Uppercase++;
});
cout<< Uppercase<<" uppercase letters in: "<< s<<endl;
}
2. 还简化了一些操作,
2.1比如vector数组的初始化,不用直接很多push_back,直接赋值即可
// C++11 container initializer
vector<string> vs={ "first", "second", "third"};
map singers =
{ {"Lady Gaga", "+1 (212) 555-7890"},
{"Beyonce Knowles", "+1 (212) 555-0987"}}
2.2 类中成员初始化
class C
{
int a=7; //C++11 only
public:
C();
};
3 经典外部库成员加入
3.1 shared_ptr
原来STL库里面的东西,增加了shared_ptr智能指针