參考連接:http://blog.csdn.net/huang_xw/article/details/8764346 ios
http://blog.jobbole.com/44015/ c++
http://blog.csdn.net/kturing/article/details/45286823正則表達式
auto:自動類型推斷和返回值佔位,與c++98定義不一樣(臨時變量定義)express
// auto a; // 錯誤,沒有初始化表達式,沒法推斷出a的類型 // auto int a = 10; // 錯誤,auto臨時變量的語義在C++11中已不存在, 這是舊標準的用法。 // 自動幫助推導類型 auto a = 10; //咱們可使用valatile,pointer(*),reference(&),rvalue reference(&&) 來修飾auto auto k = 5; auto* pK = new auto(k); //函數和模板參數不能被聲明爲auto //不能用於類型轉換或其餘一些操做,如sizeof和typeid int value = 123; auto x2 = (auto)value; // no casting using auto //定義在一個auto序列的變量必須始終推導成同一類型 auto x1 = 5, x2='a'; //wrong //auto不能自動推導成CV-qualifiers(constant & volatile qualifiers),除非被聲明爲引用類型 const int i = 99; auto j = i; // j is int, rather than const int j = 100 // Fine. As j is not constant auto& k = i; // Now k is const int& k = 100; // Error. k is constant //auto會退化成指向數組的指針,除非被聲明爲引用 int a[2]={0,1}; auto ar = a; count<<typeid(ar).name()<<endl; // int auto &ar = a; count<<typeid(ar).name()<<endl; // int [2]
nullptr: 替代NULL的宏定義,VS2010以前的版本,不支持此關鍵字。數組
//0(NULL)和nullptr能夠交換使用,可是nullptr是一個爲空的指針,並非一個整形,不能替代0 int* p1 = 0; int* p2 = nullptr; if(p1 == p2) {} if(p2) {} //不能將nullptr賦值給整形 int n2 = nullptr; // error if(0 == nullptr) {} // error if(nullptr) {} // error //重載時,使用nullptr時 對應的是參數爲指針的函數 void foo(int) {cout << "int" << endl;} void foo(char*) {cout << "pointer" << endl;} foo(0); // calls foo(int) foo(nullptr); // calls foo(char*)
decltype(E)是一個標識符或者表達式的推斷數據類型(「declared type」),它能夠用在變量聲明中做爲變量的數據類型
override: 表示函數應當重寫基類中的虛函數。app
class B { public: virtual void f(short) {std::cout << "B::f" << std::endl;} }; class D : public B { public: virtual void f(int) override {std::cout << "D::f" << std::endl;} }; //error:'D::f' : method with override specifier 'override' did not override any base class methods
final: 表示派生類不該當重寫這個虛函數。ide
class B { public: virtual void f(int) {std::cout << "B::f" << std::endl;} }; class D : public B { public: virtual void f(int) override final {std::cout << "D::f" << std::endl;} }; class F : public D { public: virtual void f(int) override {std::cout << "F::f" << std::endl;} }; //被標記成final的函數將不能再被F::f重寫
enum class Options {None, One, All}; //強類型枚舉由關鍵字enum class標識,它不會將枚舉常量暴露到外層做用域中,也不會隱式轉換爲整形 Options o = Options::All;
unique_ptr: 若是內存資源的全部權不須要共享,就應當使用這個(它沒有拷貝構造函數),可是它能夠轉讓給另外一個unique_ptr(存在move構造函數)。有點像auto_ptr std::unique_ptr<int> p1(new int(42)); std::unique_ptr<int> p2 = std::move(p1); // transfer ownership shared_ptr: 若是內存資源須要共享,那麼使用這個 weak_ptr:持有被shared_ptr所管理對象的引用,可是不會改變引用計數值。它被用來打破依賴循環 auto_ptr: 已廢棄
Lambda函數
[captures] (params) -> ret {Statments;} #include <iostream> using namespace std; int main() { auto func = [] () { cout << "Hello world"; }; func(); // now call the function } /* [] 不截取任何變量 [&} 截取外部做用域中全部變量,並做爲引用在函數體中使用 [=] 截取外部做用域中全部變量,並拷貝一份在函數體中使用 [=, &foo] 截取外部做用域中全部變量,並拷貝一份在函數體中使用,可是對foo變量使用引用 [bar] 截取bar變量而且拷貝一份在函數體重使用,同時不截取其餘變量 [this] 截取當前類中的this指針。若是已經使用了&或者=就默認添加此選項。 */ string name; cin>> name; return global_address_book.findMatchingAddresses( // notice that the lambda function uses the the variable 'name' [&] (const string& addr) { return name.find( addr ) != string::npos; } );
參考連接:http://blog.csdn.net/srzhz/article/details/7934652oop
控制默認函數——默認或者禁用測試
class X { // … X& operator=(const X&) = delete; // 禁用類的賦值操做符 X(const X&) = delete; }; //可是其實能夠用privae啊!
std::bind
//bind()接受一個函數(或者函數對象,或者任何你能夠經過」(…)」符號調用的事物),生成一個其有某一個或多個函數參數被「綁定」或從新組織的函數對象 int f(int, char, double); // 綁定f()函數調用的第二個和第三個參數, // 返回一個新的函數對象爲ff,它只帶有一個int類型的參數 auto ff = bind(f, _1, ‘c’, 1.2); int x = ff(7); // f(7, ‘c’, 1.2); //「_1″是一個佔位符對象,用於表示當函數f經過函數ff進行調用時,函數ff的第一個參數在函數f的參數列表中的位置 //bind()也能夠被看作是bind1st()和bind2nd()的替代品
std::function
template< class R, class... Args >
class function<R(Args...)>
struct X { int foo(int); }; // 所謂的額外參數, // 就是成員函數默認的第一個參數, // 也就是指向調用成員函數的對象的this指針 function<int (X*, int)> f; f = &X::foo; // 指向成員函數 X x; int v = f(&x, 5); // 在對象x上用參數5調用X::foo() function<int (int)> ff = std::bind(f, &x, _1); // f的第一個參數是&x v = ff(5); // 調用x.foo(5) //能夠看作是C++98標準庫中函數對象mem_fun_t, pointer_to_unary_function等的替代品
初始化
int arr[3]{1, 2, 3}; vector<int> iv{1, 2, 3}; map<int, string>{{1, "a"}, {2, "b"}}; string str{"Hello World"};
for循環
map<string, int> m{{"a", 1}, {"b", 2}, {"c", 3}}; for (auto p : m){ cout<<p.first<<" : "<<p.second<<endl; }
int my_array[5] = {1, 2, 3, 4, 5}; // double the value of each element in my_array: for (int &x : my_array) { x *= 2; }
std::tuple
相似std::pair
//init template <class ...Types> class tuple; typedef std::tuple <int, double, long &, const char *> test_tuple; long lengthy = 12; test_tuple proof (18, 6.5, lengthy, "Ciao!"); auto record = std::make_tuple("Hari Ram", "New Delhi", 3.5, 'A'); //ge/set eles lengthy = std::get<0>(proof); // Assign to 'lengthy' the value 18. int len = proof.get<1>(); //獲取第二個值 std::get<3>(proof) = " Beautiful!"; // Modify the tuple’s fourth element. //two tuples have the same elements type typedef std::tuple <int , double, string > tuple_1 t1; typedef std::tuple <char, short , const char * > tuple_2 t2 ('X', 2, "Hola!"); t1 = t2; // Ok, first two elements can be converted, std::string name ; float gpa ; char grade ; std::tie(name, std::ignore, gpa, grade) = record ; // std::ignore helps drop the place name std::cout << name << ' ' << gpa << ' ' << grade << std::endl ; std::tie(std::ignore,std::ignore,y) = tp; //std::ignore佔位符來表示不解某個位置的值,只解第三個值了 //size type std::tuple_size<T>::value returns the number of elements in the tuple T, std::tuple_element<I, T>::type returns the type of the object number I of the tuple T. std::tuple_element<1,Tuple>::type second = std::get<1> (mytuple); //tuple_cat鏈接多個tupe int main() { std::tuple<int, std::string, float> t1(10, "Test", 3.14); int n = 7; auto t2 = std::tuple_cat(t1, std::make_pair("Foo", "bar"), t1, std::tie(n)); //the variable ele is reference n = 10; print(t2); } //輸出結果: (10, Test, 3.14, Foo, bar, 10, Test, 3.14, 10
右尖括號
int my_array[5] = {1, 2, 3, 4, 5};
// double the value of each element in my_array:
for (int &x : my_array) {
x *= 2;
}
explicit
In C++11, the explicit
keyword can now be applied to conversion operators. As with constructors, it prevents the use of those conversion functions in implicit conversions. However, language contexts that specifically require a boolean value (the conditions of if-statements and loops, as well as operands to the logical operators) count as explicit conversions and can thus use a bool conversion operator
long long int
c++03中long int至少和int類型位數同樣,在某些編譯器中是64位,有的是32,在C++11中引入long long int,至少與long int位數同樣,很多於64位。
static_assert(express, error_msg)
靜態斷言,在編譯期間測試,大多測試編譯器環境,例如判斷long int類型的位數是否爲64
sizeof
c++03能夠用在基本類型和對象中,可是不能夠用在對象成員中
struct MyType{MemberType member}; sizeof(MyType::member);//ok
正則表達式
std::regex
std::match_results
std::regex_search
散列表
std::unorderd_set
std::unorderd_multiset
std::unorderd_map
std::unorderd_multimap
constexpr
容許將變量聲明爲constexpr類型讓編譯器來驗證變量的值是不是一個常量表達式。
聲明爲constexpr的變量必定是一個常量,並且必須用常量表達式初始化:
constexpr int mf = 0; // 0 是常量表達式 constexpr int limit = mf + 1; // mf + 1 是常量表達式 constexpr int sz = size(); // 只有當 size() 是一個constexpr函數時纔是一條正確的聲明語句
隨機數生成器