理解STL的鑰匙——迭代器
- 迭代器是一種概念模型睛蛛,滿足一定的行為要求(如: 解引用,自增胧谈,自減忆肾,……)。根據(jù)不同的要求菱肖,STL定義了5種類型的迭代器
Input Iterator客冈,Output Iterator,Forward Iterator蔑滓,Bidirectional Iterator郊酒,Random-access Iterator - 迭代器是一種傳統(tǒng)C指針的推廣(廣義指針)遇绞,指針是一種特殊的迭代器
- 迭代器為算法提供了一種遍歷數(shù)據(jù)結構元素的方法
- 迭代器將算法實現(xiàn)與具體數(shù)據(jù)結構解耦
- 迭代器與容器也是解耦的,盡管每一種容器都會定義它自己的迭代器
理解STL的核心——容器燎窘、算法摹闽、迭代器三個核心組件及其配合使用
其他雜項——函數(shù)對象、分配器褐健、裝飾器
函數(shù)對象
- 函數(shù)概念的推廣
Just as iterators are a generalization of pointers, function objects are a generalization of functions: a function object is anything that you can call using the ordinary function call syntax - 函數(shù)對象不僅對類型進行抽象付鹿,還對操作進行抽象
Function objects are an important part of generic programming because they allow abstraction not only over the types of objects, but also over the operations that are being performed.