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
replaces the managed object |
|
swaps the managed objects |
|
Observers |
|
returns a pointer to the managed object |
|
dereferences pointer to the managed object |
|
returns the number of shared_ptr objects referring to the same managed object |
|
checks whether the managed object is managed only by the current shared_ptr instance |
|
checks if there is associated managed object |
|
provides owner-based ordering of shared pointers |
新增加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);