定义
- 配接器(adapters):将一个class的接口,转换为另一个class的接口,使得原来不能一起使用相互兼容的classes,可以一起协同工作。
- 配接器是一种设计模式。
STL中提供的各种配接器可分为三类:
- 改变仿函数接口者,称之为function adapter
- 改变容器接口者,称之为container adapter
- 改变迭代器接口者,称之为iterator adapter
container adapter
graph LR
deque --- stack
deque --- queue
deque --- stack
deque --- queue
iterator adapter
graph LR
iterator --- reverse_iterator
iterator --- Insert_iterator
iterator --- iostream_iterator
iterator --- reverse_iterator
iterator --- Insert_iterator
iterator --- iostream_iterator
Insert_iterator --- back_insert_iterator
Insert_iterator --- front_insert_iterator
Insert_iterator --- insert_iterator
插入迭代器:将一般迭代器的赋值操作转变为插入操作。
reverse adapter
反向迭代器:将一般迭代器的行进方向逆转,使得原本应该前进的operator++变为了后退操作,原本应该后退的operator--操作变成了前进操作。
iostream adapter
graph LR
iostream_iterator --- istream_iterator
iostream_iterator --- ostream_iterator
iostream_iterator --- istream_iterator
iostream_iterator --- ostream_iterator
I/O迭代器:将迭代器绑定到某个iostream对象上,使得方便进行输入与输出。
function adapter
graph LR
function_adapter --- 绑定bind
function_adapter --- 否定negate
function_adapter --- 组合compose
function_adapter --- 绑定bind
function_adapter --- 否定negate
function_adapter --- 组合compose
function adapter的价值在于:通过不同适配器之间的绑定,组合和修饰能力,几乎可与无限制地创造出各种可能的表达式,搭配STL算法一同工作。