Boolan/STL 與泛型編程 part1

@(boolan C++)[STL]
2017-11-20 12:10:37 / herohlc


使用一個東西不明白它的道理不高明。

item1. 認識headers/版本

1. 泛型編程

泛型編程 - GP/Generic Programming,使用template 為主要工具來編寫程序。

STL是GP的一個成功的實踐。

2. C++標準庫學習

標準庫學習層級:

  1. level 0: 使用標準庫
  2. level 1: 認識標準庫
  3. level 2: 良好使用標準庫
  4. level 3:擴展標準庫

3. C++標準庫 vs. STL

C++ 標準庫/C++ Standard Library

標準模版庫/STL/Standard Template Library

STL是標準庫的子集,標準庫包含STL之外的其他部件。

4. 標準庫頭文件

  1. 標準庫的header files 不帶”.h”, 例如 :#include <vector>
  2. 新式的C header files 不帶”.h”, 例如: #include <cstdio>
  3. 舊式的C header files 仍然可用,例如: #include <stdio.h>
  4. 新式的header files內的組件封裝在 namespace std下
  5. 舊式的header files內的組件未封裝在 namespace std下

頭文件用新不用舊。

5. 有用的網站

cppreference.com
cplusplus.com - The C++ Resources Network
https://gcc.gnu.org/


item2. STL體系結構基礎介紹

1. STL六大部件

Image Copied on 2017-11-25 at 14.32.58 下午.png

注意上圖各組件之間的關系。

  1. STL中的GP區別OOP,數據結構在容器中實現,處理容器的部分功能在算法中實現(容器的基本操作仍然在容器中實現,有的容器有實現自己的find等算法)。
  2. 迭代器是泛化的指針。

2. Example: 一些部件

count_if

http://www.cplusplus.com/reference/algorithm/count_if/?kw=count_if

header
#include <algorithm>

not1

not1 - C++ Reference

header
#include <functional>

bind2nd

bind2nd - C++ Reference

header
#include <functional>

less

http://www.cplusplus.com/reference/functional/less/?kw=less

header
#include <functional>

3. 前閉后開區間 [)

Image Copied on 2017-11-25 at 14.34.58 下午.png
  1. 引用container.end() 很危險。

item3. 容器分類與測試/array

1. 容器 - 結構與分類

Image Copied on 2017-11-25 at 14.38.49 下午.png

容器分類: sequence containers / associative containers

sequence containers

  1. vector
  2. deque
  3. list
  4. array (C++11)
  5. forward_list(C++11)

associative containers

  1. map/ multimap / unordered_map(C++11) / unordered_multimap(C++11)
  2. set /multiset /unordered_set(C++11) / unordered_multiset(C++11)

containers 比較

  1. array 存儲固定數量的元素。
  2. forward_list只保存一個指針所以比list更節省內存。
  3. sequence 與associative的差別在于,sequence 存放數據時是有序的存儲(邏輯上有序,非內存存儲上有序)
  4. set/map實現的數據結構為紅黑樹-高度平衡二叉樹。

一種被廣泛采用的hash-table實現:separate chaining

2. std::array

array存儲結構

Image Copied on 2017-11-25 at 14.41.10 下午.png

array info

std::array - cppreference.com

clock/qsort/bsearch

clock - C++ Reference
http://www.cplusplus.com/reference/cstdlib/qsort/?kw=qsort
http://www.cplusplus.com/reference/cstdlib/bsearch/?kw=bsearch


item4. 容器分類與測試/vector

1. vector存儲結構

Image Copied on 2017-11-25 at 14.41.16 下午.png
  1. vector向后增長所以只有push_back操作
  2. vector的擴容方式:兩倍增長,renew 2倍新內存,拷貝舊數據到新內存

item5. 容器分類與測試/list/forward_list

1. std::list

list存儲結構

Image Copied on 2017-11-25 at 14.41.27 下午.png

如果容器提供了自己的sort/find,請采用容器提供的sort/find。

2. std::forward_list

forward_list存儲結構

Image Copied on 2017-11-25 at 14.41.32 下午.png
  1. forward_list 僅能提供對front元素的操作

3. gnu::slist

4. std::deque

deque存儲結構

Image Copied on 2017-11-25 at 14.41.21 下午.png

5. std::stack/queue

  1. stack/queue是deque的適配器(container adapter),容器適配器
  2. stack/queue僅提供push/pop方法
  3. 限于stack/queue的數據處理方式,stack/queue不提供迭代器操作,算法當然也不支持。

stack/queue存儲結構

Image Copied on 2017-11-25 at 14.46.21 下午.png

Image Copied on 2017-11-25 at 14.46.30 下午.png

item6. 容器分類與測試/ map/set/multimap/multiset/unordered_map/unordered_set/unordered_multimap/unordered_multiset

1. std::multiset

multiset存儲結構

Image Copied on 2017-11-25 at 14.41.36 下午.png

2. std::multimap

multimap存儲結構

Image Copied on 2017-11-25 at 14.41.39 下午.png

3. std::unordered_multiset

hash-table存儲結構

Image Copied on 2017-11-25 at 14.42.14 下午.png

hash table 中bucket一定要比元素個數多。如果元素個數>=bucket個數則擴充bucket個數。

4. std::unordered_multimap

5. std::unordered_set

6. std::unordered_map

7. std::set

8. std::map


item 7. 分配器/allocator

分配器負責容器內存管理。的容器大多數情況下使用默認的分配器。

container 的allocator 參數

template <typename _TP, typename _Alloc = std::allocator<_TP>>
class vector : protected _Vector_base<_TP,_Alloc>

template <typename _TP, typename _Alloc = std::allocator<_TP>>
class list : protected _List_base<_TP,_Alloc>

template <typename _TP, typename _Alloc = std::allocator<_TP>>
class deque : protected _Deque_base<_TP,_Alloc>

1. __gnu::_cxx:: allocator

頭文件與用法

#include <ext/array_allocator.h>
#include <ext/mt_allocator.h>
#include <ext/debug_allocator.h>
#include <ext/pool_allocator.h>
#include <ext/bitmap_allocator.h>
#include <ext/malloc_allocator.h>
#include <ext/new_allocator.h>

list<string, __gnu_cxx::array_allocator<string>> c1;
list<string, __gnu_cxx::mt_allocator<string>> c1;
list<string, __gnu_cxx::debug_allocator<string>> c1;
list<string, __gnu_cxx::pool_allocator<string>> c1;
list<string, __gnu_cxx::bitmap_allocator<string>> c1;
list<string, __gnu_cxx::malloc_allocator<string>> c1;
list<string, __gnu_cxx::new_allocator<string>> c1;

不建議直接使用allocator分配內存。


?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容