将一个Class的接口转换成另一个Class的接口,使原本因接口不兼容而不能合作的Class可以一起运作。分为:迭代器适配器(Iterator Adpater)、容器适配器(Contaier Adpater)、仿函数配接器。
应用于迭代器
Insert Iterator
它的作用是将一般迭代器的赋值(assign)操作转变为插入(insert)操作。
Iterator Adapter修饰之后的结果当成是一种迭代器,依然可以用*符号,但不同的是当你对它进行赋值的时候,它将自动进行insert操作,也就是说该iterator只是将assign操作变成了push_back,push_front,insert,而其他操作++,--,*全部返回原先的iterator。
//back_insert_iterator:将赋值操作变成尾插
template <class Container>
class back_insert_iterator
{
protected:
Container* container;
public:
explict back_insert_iterator(Container& x) : container(&x) {}
// 主要提供的方法
back_insert_iterator<Container>& operator=(const typename Container::value_type& value)
{
container->push_back(value);
return *this;
}
// 关闭下列方法
back_insert_iterator<Container>& operator*() { return *this; }
back_insert_iterator<Container>& operator++() { return *this; }
back_insert_iterator<Container>& operator++(int) { return *this; }
}
template <class Container>
inline back_insert_iterator<Container> back_inserter(Container& x)
{
return back_insert_iterator<Container>(x);
} //front_insert_iterator:将赋值操作变成头插
template <class Container>
class front_insert_iterator
{
protected:
Container* container;
public:
explict front_insert_iterator(Container& x) : container(&x) {}
// 主要提供的方法
front_insert_iterator<Container>& operator=(const typename Container::value_type& value)
{
container->push_front(value);
return *this;
}
// 关闭下列方法
front_insert_iterator<Container>& operator*() { return *this; }
front_insert_iterator<Container>& operator++() { return *this; }
front_insert_iterator<Container>& operator++(int) { return *this; }
}
template <class Container>
inline front_insert_iterator<Container> front_inserter(Container& x)
{
return front_insert_iterator<Container>(x);
} //insert_iterator:将赋值操作变成在指定位置后插入,便于连续赋值
template <class Container>
class insert_iterator
{
protected:
Container* container;
typename Container::iterator iter;
public:
explictinsert_iterator(Container& x, typename Container::iterator i) : container(&x), iter(i) {}
// 主要提供的方法
insert_iterator<Container>& operator=(const typename Container::value_type& value)
{
container->insert(iter, value);
++iter;
return *this;
}
// 关闭下列方法
insert_iterator<Container>& operator*() { return *this; }
insert_iterator<Container>& operator++() { return *this; }
insert_iterator<Container>& operator++(int) { return *this; }
}
template <class Container, class Iterator>
inline insert_iterator<Container> front_inserter(Container& x, Iterator i)
{
typedef typename Container::iterator iter;
return insert_iterator<Container>(x, iter(i));
}
reverse_iterator
template <class Iterator>
class reverse_iterator
{
protected:
Iterator current;
public:
typedef Iterator iterator_type; // 正向迭代器
typedef reverse_iterator<Iterator> self; // 反向迭代器
reverse_iterator() {}
explicit reverse_iterator(iterator_type x) : current(x) {}
explicit reverse_iterator(const self& x) : current(x.current) {} iterator_type base() const { return currrent; }
reference operator*() const
{
Iterator tmp = current;
return *--tmp;
}
reference operator->() const
{
return &(operator*());
}
self& operator++()
{
--current;
return *this;
}
self& operator++(int)
{
self tmp = current;
--current;
return tmp;
}
self& operator--()
{
++current;
return *this;
}
self& operator--(int)
{
self tmp = current;
++current;
return tmp;
}
self operato+(difference_type n) const
{
return self(current - n);
}
self& operato+=(difference_type n)
{
current -= n;
return self;
}
self operato-(difference_type n) const
{
return self(current + n);
}
self& operato-=(difference_type n)
{
current += n;
return self;
}
reference operator[](difference_type n) const
{
return *(*this + n)
}
};
IOstreamIterator
将迭代器绑定到某个iostream对象身上
//istream_iterator
template <class T, class Distance = ptrdiff_t>
class istream_iterator
{
protected:
istream* stream;
T value;
bool end_mark;
void read()
{
end_mark = (*stream) ? true :false;
if(end_mark)
{
*stream >> value;
}
end_mark = (*stream) ? true :false;
}
public:
istream_iterator() : stream(&cin), end_mark(false) {}
istream_iterator(istream& s) : stream(&s), end_mark(false) { read(); }
reference operator*() const { return value; }
reference operator->() const { return &(operator*()); }
istream_iterator<T, Distance>& operator++()
{
read();
return *this;
}
istream_iterator<T, Distance>& operator++(int)
{
istream_iterator<T, Distance> tmp = *this;
read();
return tmp;
}
}; //ostream_iterator
template <class T>
class ostream_iterator
{
protected:
ostream* stream;
const char* string; // 每次输出后的间隔符
public:
ostream_iterator(ostream& s) :stream(&s), string() {}
ostream_iterator(ostream& s, const char* c): stream(&s), string(c) {}
ostream_iterator<T>& operator=(const T& value)
{
*stream << value;
if(string)
{
*stream << string;
}
return *this;
}
ostream_iterator<T>& operator*() { return *this; }
ostream_iterator<T>& operator++() { return *this; }
ostream_iterator<T>& operator++(int) { return *this; }
};
应用于仿函数
任何算法在接受仿函数时总是在演算过程中调用其operator(),但是一般函数和成员函数却无法使用,所以stl又提供配接器使得一般函数和成员函数与其他配接器和算法结合起来。成员函数必须经过mem_fun处理,一般函数必须经过ptr_fun处理,虽然函数指针可以传给算法使用但是它无任何配接能力。