极客班C++ STL(容器)第二周笔记
為什么80%的碼農都做不了架構師?>>> ??
極客班 C++ STL (容器算法)第二周筆記
標簽(空格分隔): C++
1. 容器(下)
1.1 Stack
a. 概述
Stack 是一種先進先后出(First In Last Out)的數據結構,只有一個出口。 特點:
- 支持的操作有
-
- push , pop , top
-
- 只能訪問頂端元素,不允許便利
- 要使用Stack,必須引入<stack> 頭文件(標準庫)
b. Stack的底層數據結構(1)
查看標準庫頭文件,我們可以知道,STL stack是以deque作為默認底層結構的:
// TEMPLATE CLASS stack template<class _Ty,class _Container = deque<_Ty> >class stack{ // LIFO queue implemented with a container//...}聯系我們在C++ OOP面向對象設計接觸到的方法,這種設計就是一種對既有接口的包裝,適配,即采用了adapter模式。該模式在這里是包裝了deque,并給出了push/pop/top等棧特有的接口。
由于stack不允許遍歷,所以沒有iterator。
c. Stack的底層數據結構(2)
在 1.1小節中,我們看到了stack的底層定義,發現在模板參數里的容器選項中,是傳入了一個deque<_Ty>作為默認參數。 所以,我們除了deque<T>,list<T>其實也是可以拿來作為底層的數據結構的。
//e.g.std::stack<int, std::list<int>> s; s.push(1); s.pop(); s.top(); s.size();1.2 Queue
a. 概述
Queue呢,就是一種先進先出的數據結構, 有兩個出口。 - 支持四種操作:push(增加元素),移除元素(pop),獲取最前面的元素(front),獲取最后面的元素(back) - 只能訪問最前或者最后的元素 - 需要引入標準庫<queue>才可以使用
#include<queue> int main() {std::queue<int> q;//初始化一個存放int型別的隊列q.push(1); //插入元素q.pop(); //移除元素q.back(); //獲取最后一個元素q.front(); //獲取最前面的元素 }b. queue的底層數據結構(1)
查看標準庫,我們可知,與stack一樣,queue也是包裝了deque<T>
// TEMPLATE CLASS queue template<class _Ty,class _Container = deque<_Ty> >class queue{ // FIFO queue implemented with a container//...}有了這一層的認識,我們可以知道,由于不允許遍歷,和stack一樣所以queue也沒有迭代器(iterator)。
c. queue的底層數據結構(2)
跟stack類似,queue也是可以以list作為底層數據結構的。具體示例暫時不給出了(接口不變,只是換了底層實現)。
1.3 Map and Multimap
1.3.1 Map 概述
Map的特性
- 是一種關聯容器,存儲的是key/value pair
- 不允許key重復
- map存儲的對象必須是具有可排序性的
其中,
- _Kty 就是對應的鍵key
- _Ty 就是對應的值value
- _Pr 對應的排序算法,默認是less<T>算法,即按_Kty排序,那么,_Kty必須要實現比較操作符operator <
- 我們也可以自定義算法(通過仿函數來實現)
- _Alloc 內存分配算法(針對這個鍵值對的)
1.3.2 例子
//STL_test.h//map value struct Employee {Employee(){}Employee(const std::wstring& wszName):Name(wszName){}std::wstring Name;void print() const{std::wcout << Name << "\n";} };//仿函數,定義比較大小 struct ReversId :public std::binary_function<int, int, bool> {bool operator()(const int& key1, const int &key2){return (key1 <= key2) ? false : true;} };//for_each 打印 struct FunctorPrintMapValue{void operator()(const std::pair<int, Employee> pair){pair.second.print();} }; //STL_test.cppint main() {const int size = 3;std::pair<int, Employee> items[size] = {std::make_pair(1, Employee(L"Tom")),std::make_pair(2, Employee(L"Jerry")),std::make_pair(3, Employee(L"Alice"))};std::map<int, Employee, ReversId> m(items, items + size);std::for_each(m.begin(), m.end(), FunctorPrintMapValue());system("pause");return 0; }結果:
我們可以看出,按照自定義的less算法,確實實現了按key倒排序。
1.4 Set and Multset
set 跟map略有不一樣,感覺上像是map的特殊版本,因為,在set中,
- 存儲的對象本身,既是key,又是value。
- 不允許有重復的key
- set存儲的對象,必須是具有可排序性
- 要達到這個目標,那么存儲的對象必須實現了operator <操作符
- 支持自定義排序行為(通過仿函數實現)
- 必須是引入<set>標準庫,通過std::set 訪問
下面是一個示例,接著Employee示例來:
//STL_test.h//set 按名字比較排序仿函數 struct FunctorEmployeeNameComparer:public std::binary_function<Employee,Employee,bool> {bool operator()(const Employee& EmpLeft, const Employee& EmpRight) {return EmpLeft.getName() < EmpRight.getName();} };//for_each 打印set struct FunctorPrintSetValue {void operator()(const Employee& emp){emp.print();} }; //STL_test.cpp//2. setconst int nSize = 4;Employee person[] = {Employee(L"Tom"),Employee(L"Jerry"),Employee(L"Alice"),Employee(L"Tony")};std::set<Employee, FunctorEmployeeNameComparer> epSet(person, person + nSize);std::for_each(epSet.begin(), epSet.end(), FunctorPrintSetValue());結果:
2. STL整體結構,仿函數(仿函數適配器),binder1st
3. binder2nd, mem_fun, mem_fun_ref,一些注意的問題
4. 泛型算法_非變異算法
轉載于:https://my.oschina.net/lxrm/blog/659980
總結
以上是生活随笔為你收集整理的极客班C++ STL(容器)第二周笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初步了解JSONP
- 下一篇: Effective C++:unio