C++11 new standard

1. New Types

long long和unsigned long long;

char16_t and char32_t;

Add raw strings;

 

2. Unified initialization

C++11 expands the scope of brace-enclosed lists (initializer lists) to work with all built-in types and user-defined types (i.e., class objects).

When using an initializer list, you can add "=" or not.

int x = { 5 } ;

double y = { 2.75 } ;

short quar [5] {4, 5, 2, 76, 1};

Alternatively, list initialization syntax can also be used in new expressions:

int * ar = new int [4] {2, 4, 6, 7}; // C ++ 11

Constructors can also be called using a list enclosed in curly braces (instead of parentheses) when creating an object:

class Stump {

  private:

    int roots ;

    double weight ;

  pubilc:

    Stump ( int r , double w ) : roots ( r ) , weight ( w ) { }

} ;

Stump s1 { 3 , 15.6 } ; // oid style

Stump s2 { 5 , 43.4 } ; // C++11

Stump s3 { 4 , 32.1 } ; // C++11

However, if the class has a constructor that takes the template std::initializer_list as a parameter, only that constructor can use the list-initialization form.

1. Narrow down

2. std::initializer_list

C++11 provides the template class initializer_list, which can be used as an argument to a constructor.

If the class has a constructor that accepts an initializer_list as a parameter, the initialization list syntax can only be used for that constructor. The elements in the list must be of the same type or convertible to the same type.

 

3. Statement

3.1 auto

C++11 uses auto for automatic type determination. This requires explicit initialization, allowing the compiler to set the type of the variable to the type of the initial value.

auto maton = 112 ; // maton is type int 

auto pt = &maton ; // pt id type int *

double fm ( double , int ) ;

auto pf = fm ; // pf is type double (*) (double , int )

关键字auto还可简化模板声明。

例:
如果 il 是一个std::initializer_list<double>对象,则可将下述代码:

for ( std :: initializer_list < double > :: iterator p = il.begin() ; p != il.end() ; p++ )

替换为如下代码:
for ( auto p = il.begin() ; p != il.end() ; p ++ )

 

3.2 decltype

关键字decltype将变量的类型声明为表达式指定的类型。

e.g.

int j = n ;

double x ;

decltype ( x*n ) q ; // q same type as x*n , i.e. , double 

decltype ( &x ) pd ; // pd same type as &x , i.e. , double *

 

3.3 返回类型后置

doubel f1 ( double , int ) ; // traditi0nal syntax

auto f2 ( double , int ) -> double ; // new syntax , return type is double 

可以配合3.2的decltype来指定模板函数的返回类型。

template < typename T , typename U >

auto eff ( T t , U u ) -> decltype ( T*U ) { }

 

3.4 模板别名:using =

typedef std :: vector < std :: string > :: iterator itType ; // old

using itType = std :: vector < std :: string > :: iterator ;

using 可以用于模板具体化, 但typedef不能。

 

3.5 nullptr指针

空指针

 

4. 智能指针

如果在程序中使用new从堆(自由存储区)分配内存,等到不再需要时,应使用delete将其释放。

C++引入了智能指针auto_ptr,以帮助自动完成这个过程。

C++11摒弃了auto_ptr并新增了unique_ptr,shared_ptr和weak_ptr。

 

5. 异常规范方面的修改

添加关键字noexcept

 

6. 作用域内枚举

7.对类的修改

允许构造函数被继承和彼此调用。

7.1 显示转换运算符

8. 模板和STL方面的修改

8.1 基于范围的for循环

double  prices [ 5 ] = { 4.99 , 10.99 , 6.87 , 7.99 , 8.49 } ;

for ( double x : prices )

  std :: cout << x << std :: endl ;

8.2 新的STL容器

forward_lsit // 单向链表

unordered_map

unordered_multimap

unordered_set

unordered_multiset

8.3 新的STL方法

cbegin ( ) 和 cend ( )

8.4 valarray升级

8.5 摒弃export

8.6 尖括号

C++11不在要求在声明嵌套模板时使用空格将尖括号分开

 

9. 右值引用

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324446206&siteId=291194637