c++的深入學習

c++輸入輸出提速

  1. std::cin.tie(nullptr); 解除的是C++運行庫層面的對數據傳輸的綁定
  2. std::sync_with_stdio(false); 這個函數是一個“是否兼容stdio”的開關,C++爲了兼容C,保證程序在使用了std::printf和std::cout的時候不發生混亂,將輸出流綁在了一起。

以上參考自:https://www.cnblogs.com/letgo/p/5631247.html

c++11特性

  1. auto 用於從初始化表達式中推斷出變量的數據類型。因此,auto定義的變量必須有初始值。 auto在c++14中可以作爲函數返回值

  2. decltype 的作用是選擇並返回操作數的數據類型

  3. nullptr 是爲了解決原來C++標準中NULL的二義性問題而引進的一種新的類型,因爲NULL實際上代表的是0。

  4. constexpr 類型以便由編譯器來驗證變量的值是否爲一個常量表達式,必須在編譯期間計算出它的值並且它的值不可以被改變。const只保證了運行時不直接被修改(但這個東西仍然可能是個動態變量),const修飾的是類型,constexpr修飾的是用來算出值的那段代碼

  5. stl容器 array 跟數組並沒有太大區別,相對於數組,增加了迭代器等函數。

  6. stl容器 unordered_map 與std::map用法基本差不多,但STL在內部實現上有很大不同,std::map使用的數據結構爲二叉樹,而std::unordered_map內部是哈希表的實現方式,哈希map理論上查找效率爲O(1)。但在存儲效率上,哈希map需要增加哈希表的內存開銷。

  7. stl容器 forward_list

  8. stl容器 unordered_set

  9. lambda 表達式形式
    [capture](parameters)->return-type{body}();

    [] 不捕獲任何變量
    [&] 以引用方式捕獲所有變量
    [=] 用值的方式捕獲所有變量(可能被編譯器優化爲const &)
    [=, &foo] 以引用捕獲foo, 但其餘變量都靠值捕獲
    [&, foo] 以值捕獲foo, 但其餘變量都靠引用捕獲
    [bar] 以值方式捕獲bar; 不捕獲其它變量
    [this] 捕獲所在類的this指針

  10. 基於範圍的for循環,支持數組和容器

  11. 元組類型 tuple
    詳細介紹看這裏

  12. emplace 很重要
    使用mplace_back替代push_back()可以在這上面有進一步優化空間,只調用構造函數不需要調用右值引用轉移構造函數

以上參考自:https://blog.csdn.net/raiven2008/article/details/82114736
https://www.cnblogs.com/chengjundu/p/10893702.html
https://blog.csdn.net/a379039233/article/details/83714770

c++14

  1. auto 可以作爲函數返回值
  2. C++11的constexpr函數只能包含一個表達式,C++14放鬆了這些限制,支持諸如if 和switch等條件語句,支持循環,其中包括基於區間(range)的for 循環
  3. hash模板

以上參考自:http://c.biancheng.net/view/523.html

常用函數

  1. sort 不穩定排序
  2. stable_sort 穩定排序
  3. partial_sort 部分排序,前k個有序的
vector<int> v{5,6,23,6,1,3,2,7,0};
int k=4;
partial_sort(v.begin(),v.begin()+k,v.end());
for(auto i:v)
	cout << i << " ";
  1. nth_elemen 找到第k個元素,前k-1個小於k,後邊的大於k,但不一定是有序的
vector<int> v{22, 7, 93, 45, 19, 56, 88, 12, 8, 7, 15, 10};
	int k=4;
	nth_element(begin(v),begin(v)+k,end(v));
	for(auto i:v)
		cout << i << " ";
  1. max_element 和 min_element 和 minmax_element 獲取容器最大最小值
  2. binary_search 二分查找,返回 bool
  3. lower_bound 和 upper_bound 查找第一個大於等於 , 查找第一個大於
  4. equal_range 返回的pair對象
  5. all_of any_of none_of
  6. count 和 count_if 計算有多少個等於給定值的
  7. equal 兩個容器是否相等,長度不一樣總返回false
vector<int> v{22, 7, 93, 45, 19, 56, 88, 12, 8, 7, 15, 10};
vector<int> w{22, 7, 93, 45, 19, 56, 88, 12, 8, 7, 15, 10};
cout << equal(begin(v)+1,end(v),w.begin(),w.end()) << endl;
cout << equal(begin(v)+1,end(v),w.begin()) << endl;
  1. next_permutation 和 prev_permutation , 可以自定義比較方式
  2. unique
  3. rotate
  4. reverse
  5. swap
  6. fill
  7. accumulate
  8. distance
  9. next
  10. replace和 replace_if

其他

  1. inline 內聯,防止頻繁調用函數,導致可能的爆棧
    https://www.runoob.com/w3cnote/cpp-inline-usage.html
  2. 開啓O2優化,俗稱加氧
    #pragma GCC optimize(2)
  3. 複數 complex
complex<double> z{1.5, -2.5}; // z: 1.5 - 2.5i
z.imag(99); // z: 1.5 + 99.0i
z.real(-4.5); // z: -4.5 + 99.0i
std::cout << "Real part: " << z.real()<< " Imaginary part: " << z.imag()<< std::endl;
  1. string 的坑點
//res = res + curr+to_string(count);//超出內存限制
 res += curr+to_string(count);  //這樣才能通過

原因是: res+= s是表示在原來字符串後添加字符串。 res = res + s 是開闢了一個新內存存放res+s。如果字符串太長,開闢新空間就需要消耗大量內存。

  1. 一些STL的底層結構
    set 紅黑樹
    map 紅黑樹
    unorderd_map 哈希表
    unorderd_set 哈希表
    multiset 紅黑樹
    multimap 紅黑樹
    priority_queue 堆

  2. bitset的資料 這裏

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