C++primer中提到的C++11新特性總結

decltype自動類型推定。decltype接受一個表達式,返回表達式的類型。可以接受最高層的修飾符,比如:const  int i; decltype(i) c =  I; c 類型爲const int 類型

如果decltype中的參數被括號括起來,則被定義爲,reference類型。

如果decltype((variable))則,定義的類型總是reference類型

constexpr 常量表達式。

int *const p = nullptr;  //p is aconst pointer to int 

定義別名 :

using SI = int;   //SI 爲新類型名,和int類型相同。 g++ 調試通過。VS2012不支持

命名空間別名  :

namespace newname = oldname;

const int *p = nullptr;      // p   is a pointer to a   constint

constexpr int *q = nullptr; //  q   isa   const pointer  to  int

在g++4.7上調試,int j = 0;  constexpr int  *p1 = &j; //未調式通過。

constexpr 定義函數時,返回值必須是字面值。

string 類型中添加to_string()函數,stoT  T代表任意類型,如T=I  stoi 字符串到int轉換。

default 關鍵字

設置默認構造函數時使用。在類中定義了構造函數後,還想繼續使用默認構造函數,則可以在函數成員列表後添加 =default ;=delete表示不定義。可以使用在不允許使用複製構造函數和賦值構造函數上

struct Sales_data {

   //  constructors added

   Sales_data() = default;

C++ Primer, Fifth Edition

Sales_data(conststd::string &s){}

};//VS2012 調試不通過,g++4.7通過

 

emplace//在容器中插入元素。後邊的元素依次向後移動。

template <class... Args>

iterator emplace (const_iterator position,Args&&... args); 在指定的位置插入元素。

emplace_back(SS);在容器尾部插入元素SS

auto

auto 關鍵字, 可以直接聲明對象,且該對象的類型由對象的初始化表達式推定出來。

使用auto關鍵字時,必須有初始化式。auto 普通類型不接受最高層修飾符

const int ci =i, &cr = ci;

auto b =ci;  // b   is an   int (top-level   const  in  ci   is dropped)

auto c =cr;  // c   is an   int (cr  is an alias for   ci  whose  const  is top-level)

auto d =&i;  //  d   isan   int*(&   of an int  object is  int*)

auto e =&ci; // e   is   const int* (&  of a  const  object is low-level  const)

當定義爲指針類型時,const不會丟失。

顯式指定刪除及顯式指定缺省的函數聲明 具備如下函數形式:

 struct A{

 A()=default; //C++11

 virtual ~A()=default; //C++11

 };

稱之爲缺省的函數。“=default;”記號部分指示編譯器生成函數的缺省實作體。缺省的函數有兩處優勢:其一,比手工實作出來的代碼更有效率,其二,把程序員從手工定義那些函數所引發的瑣細事務中解脫出來。

與缺省函數對應,也存在指定刪除函數:

 int func()=delete;

聲明刪除的函數在防止複製對象的方面尤其有用。C++類自動聲明一個複製構造函數和一個賦值操作。要禁止這種默認行爲,使用記號=delete:

 struct NoCopy{

 NoCopy & operator =( constNoCopy & ) = delete;

 NoCopy ( const NoCopy & ) =delete;

 };

 NoCopy a;

 NoCopy b(a); //compilation error,copy ctor is deleted

容器類:

forward_list  //前向list

內部實現指向下一個元素只有一個指針。而list有兩個。插入,移動,提取元素,比其他順序容器效率高。確定是不支持隨機存取。

vector中增加shrink_to_fit 函數,可以縮小容量,使其和size大小相同。

增加data函數

 

    value_type* data() noexcept;

const value_type* data() constnoexcept; 

可以利用偏移量進行訪問vector中的元素。

int* p =myvector.data();

 

  *p = 10;

  ++p;

  *p = 20;// p 移動到myvector[1]的位置。

  p[2] = 100;   //相對於p所指位置2單位的偏移量.也就是現在的myvector[3]

shared_ptr函數指針。

template <class T> class shared_ptr;   //<memory>可以自己定義刪除函數。成員函數調用reset時,調用刪除函數。

make_shared<T>(args) //返回一個shared_ptr類型

shared_ptr 指針get()成員函數返回管理的對象。

operator*

operator->  兩個都是對管理的對象進行解引用操作dereferences pointer to the managed object 

reset

replaces the managed object 
(public member function)

swap

swaps the managed objects 
(public member function)

Observers

get

returns a pointer to the managed object 
(public member function)

operator*
operator->

dereferences pointer to the managed object 
(public member function)

use_count

returns the number of shared_ptr objects referring to the same managed object 
(public member function)

unique

checks whether the managed object is managed only by the current shared_ptr instance 
(public member function)

operator bool

checks if there is associated managed object 
(public member function)

owner_before

provides owner-based ordering of shared pointers 
(public member function)

 

新增加final關鍵字定義類爲final 則類無法被繼承。

override關鍵字,

在基類爲virtual關鍵字修飾,子類可以使用override修飾,子類中的方法必須和父類中的方法參數必須相同。否則出錯。使用override修飾的子類,父類中必須存在且爲virtual修飾。

在template<typename  Type>中我們可以把Type定義爲friend,例如:

template<typename Type>

class Bar {

         friendType;

} 不管類Bar爲什麼類型,Type都是它的friend函數。

我們無法爲一個模板類定義typedef,但是我們可以使用using定義別名。例如:

template<typename T> using twin =pair<T,T>;  twin<string>  //pair<string,string>

 

lambda表達式

lambda 定義式   [capture_list] (argument。。。) { body ;}

capture_list 如果是& 則表示捕獲的爲引用傳遞,[&]  body中會自動推斷參數。

如果是= 則表示值傳遞,[=]  body 中會自動推斷參數.

而捕獲列表變量之間以逗號隔開。

捕獲列表應該保持最小捕獲原則。在body中需要到的參數,應該在捕獲列表中寫出來。

如果捕獲的參數前加”&”符號,則表示是引用。

initializer_list

初始化列表。

inline namespace  當聲明命名空間爲inline時,則在引用命名空間中的對象時,可以直接引用,而不用添加命名空間的名字

lambda表達式中如果需要定義返回值,則必須使用Trailingreturn type 例如:

[]()->int

{

}  //表示返回值爲int類型。

初始化:

新標準中,可以直接通過{} 對類內的成員在定義時進行初始化。VS2012不支持,g++4.7支持;如成員變量 int  mm {10} //mm直接被初始化爲;也可以直接使用” = ” 進行初始化。

對其他變量進行初始化時可以直接使用 {} 進行。

例如一個vector<int> vce;

vce = {1,2,3}; vce 初始化爲含有三個元素的vector

function

function定義在functional頭文件中,是定義函數指針的。

例如function<int(int,int)>f1; //定義一個指向返回int類型,參數爲兩個int類型的函數。

mem_fn(Ret T::* pm))  //把一個成員函數轉換爲函數對象。The return type is unspecified, but the type returned is a function object class with the properties described above。

bind函數bind (Fn&& fn, Args&&... args);

 

auto newCallable = bind (Callable,arg_list);  arg_list 由逗號隔開。

可以使用using  namespace std::placeholders ; 提供的佔位符來確定具體那位被綁定。

綁定的時候應該進行依次綁定,否則調用的時候需要使用佔位符進行標註,否則會出錯。

例如:auto ss = bind (chenk,_1,6);調用的時候表示參數1需要自己輸入,參數2爲6,佔位符其實就是表示調用ss時所對應的參數的位置的值出現在bind的對應位置上。
g(_1, _2)
 to
 f(a, b, _2, c, _1)
 That is, calling  g  calls  f  using g ’s arguments for the placeholders along with the
bound arguments, a ,  b , and  c . For example, calling  g(X, Y) calls
 f(a, b, Y, c, X)
不要和bind1st 等弄混,template <class Operation, class T>
  binder1st<Operation> bind1st (const Operation& op, const T& x);這個主要是和函數對象的操作符一起使用。

move(arg); 去掉arg的語義,把它當成一個右值。

左值是既可以出現在表達式左邊,又可以出現在右邊。左值表示永久存在。

右值只能出現在右邊。右值代表臨時存在。

noexcept

noexcept 修飾時,表示不拋出任何異常。成員函數中,noexcept跟隨在const 或者引用限定符後,而在final,override,或者 = 0前面。

void recoup(int ) noexcept;

void recoup(int) throw();   //這兩個函數其實是等同的,都是說不拋出異常。

noexcept(e); 如果e不拋出異常,則返回true。否則返回false;

父類,子類對應的成員方法,必須有相同的noexcept值。

random

頭文件<random>

Engine 產生一系列隨機無符號整數數

Distribution 使用一個Engine依據特殊的規則返回分配的隨機數。

產生僞隨機數:

default_random_engine e;

cout << e();  //會產生一個僞隨機數。

使用uniform_int_distribution<unsigned>u(0,9) 產生0-9之間的數字。

u(e) //產生0-9之間的數字,包括0和9

一個給定的隨機數生成器,將會每次運行時都產生相同的隨機數序列。

可以通過定義一個seed來控制,當兩個engine的seed相同時,則產生的隨機數序列也是相同的。反之亦然。

一般可以使用系統時間作爲seed值,但是當程序時在一個單獨的自動反覆運行的機械上時,這個方法將失效。因爲每次產生的seed種子都相同,

產生real random

使用: uniform_real_distribution

cmath中增加lround函數,四捨五入。

bernoulli_distribution 伯努利方式這個屬於一個普通函數,而不是模板,最後產生的結果只有bool類型,true或false。

normal_distribution<> 正態分佈方式產生。

tuple

tuple類似於pair,但是他的參數可以是任意多個。

使用get<pos>(tuple  t) ; 獲得第pos的t的元素

tuple_size<tupleType>::value  ;//獲得tupleType中元素類型的個數。

tuple_element<I , tupleType>::type  //獲得第i個元素的類型

tuple 定義了 < 和 == 比較的時候只能是兩種類型相同的時候。

 

 

新版union中可以包含對象。

placement new表達式:

new (place-address) type

new (place-address) type(initializer-list);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章