0、前言
STL的六大组件,容器是以类模板完成,算法是以函数模板完成,仿函数是一种将operator()重载的类模板,迭代器是一种将operator++和operator*等指针习惯行为重载的类模板,配接器则复杂很多不同配接器实现不同,容器配接器和迭代器配接器是一种类模板,函数配接器是一种函数模板。
1、配接器概述与分类
配接器的定义:将一个class的接口转换为另一个class的接口,使得原本接口不兼容而不能合作的class可以一起运作。配接器在STL组件的灵活组合运用功能上,扮演着轴承、转接器的角色。配接器的本质是一种设计模式。
STL所提供的各种配接器中,改变仿函数接口的称为函数配接器,改变容器接口的称为容器配接器,改变迭代器接口的称为迭代器配接器。
- 容器配接器:queue和stack两个容器就是一种配接器,底层实现是deque。
- 迭代器配接器:包括insert iterators、reverse iterators、iostream iterators。这些接口由<iterator>获得,实际定义于<stl_iterator.h>。
- 仿函数配接器:仿函数配接器是配接器中数量最大的一个族群,其配接灵活性也更高,可以配接、配接、再配接。这些配接操作包括系结(bind)、否定(negate)、组合(compose),以及对一般函数或成员函数的修饰。这些接口由<functional>获得,实际定义于<stl_functiona.h>。仿函数迭代器的价值在于,通过它们之间的绑定、组合、修饰能力,几乎可以无限制地创造出各种可能的表达式,搭配STL算法一起演出。
容器配接器内藏一个容器成员变量,这样容器配接器就可以调用容器成员变量中所需的函数,可以封住了容器成员变量对外接口,只开放容器配接器自己的几个函数。不同的迭代器配接器实现略有不同,insert iterators内藏一个指向容器的指针成员变量,reverse iterators内藏一个迭代器成员变量,iostream iterators内藏一个指向流的指针成员变量。函数配接器内藏一个成员对象,其类别等同于它所要配接的对象,函数配接器是一个函数模板,内藏的成员对象作为参数调用相应的仿函数类模板对象的成员函数返回。
2、容器配接器
(1)stack
stack的底层由deque构成,具体是通过deque作为stack的成员变量来实现配接功能,stack都是封住了所有的deque对外接口,只开放符合stack原则的几个函数,所以说stack是一种配接器,一个作用于容器之上的配接器。
(2)queue
queue的底层由deque构成,具体是通过deque作为queue的成员变量来实现配接功能,queue都是封住了所有的deque对外接口,只开放符合queue原则的几个函数,所以说queue是一种配接器,一个作用于容器之上的配接器。
3、迭代器配接器
(1)insert iterators
insert iterators可以将一般迭代器的赋值操作转变为插入操作。这样的迭代器包括尾端插入操作back_insert_iterator,头端插入操作front_insert_iterator,以及任意位置插入操作insert_iterator,这三个迭代器对应STL提供的函数分别是back_inserter()、front_inserter()、inserter()。具体实现方法:在insert iterators的operator=操作符中调用底层容器的push_front()或push_back()或insert()操作函数,insert iterators的其他操作符无意义。
template <class Container>
class back_insert_iterator {
protected:
Container* container;
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
explicit 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>
class front_insert_iterator {
protected:
Container* container;
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
explicit 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>
class insert_iterator {
protected:
Container* container;
typename Container::iterator iter;
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
insert_iterator(Container& x, typename Container::iterator i)
: container(&x), iter(i) {}
insert_iterator<Container>&
operator=(const typename Container::value_type& value) {
iter = 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; }
};
(2)reverse iterators
reverse iterators可以将一般迭代器的行进方向逆转,使原本应该前进的operator++变成后退操作,使原本后退的operator--变成前进操作。具体实现方法:reverse iterators调用operator*就是取前一个元素值,operator++就是--current,operator--就是++current,这样就达到了反向迭代器的目的了。
template <class Iterator>
class reverse_iterator
{
protected:
Iterator current;
public:
typedef typename iterator_traits<Iterator>::iterator_category
iterator_category;
typedef typename iterator_traits<Iterator>::value_type
value_type;
typedef typename iterator_traits<Iterator>::difference_type
difference_type;
typedef typename iterator_traits<Iterator>::pointer
pointer;
typedef typename iterator_traits<Iterator>::reference
reference;
typedef Iterator iterator_type;
typedef reverse_iterator<Iterator> self;
public:
reverse_iterator() {}
explicit reverse_iterator(iterator_type x) : current(x) {}
reverse_iterator(const self& x) : current(x.current) {}
#ifdef __STL_MEMBER_TEMPLATES
template <class Iter>
reverse_iterator(const reverse_iterator<Iter>& x) : current(x.current) {}
#endif /* __STL_MEMBER_TEMPLATES */
iterator_type base() const { return current; }
reference operator*() const {
Iterator tmp = current;
return *--tmp;
}
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
self& operator++() {
--current;
return *this;
}
self operator++(int) {
self tmp = *this;
--current;
return tmp;
}
self& operator--() {
++current;
return *this;
}
self operator--(int) {
self tmp = *this;
++current;
return tmp;
}
self operator+(difference_type n) const {
return self(current - n);
}
self& operator+=(difference_type n) {
current -= n;
return *this;
}
self operator-(difference_type n) const {
return self(current + n);
}
self& operator-=(difference_type n) {
current += n;
return *this;
}
reference operator[](difference_type n) const { return *(*this + n); }
};
(3)iostream iterators
iostream iterators可以将迭代器绑定到某个iostream对象身上。绑定到istream对象身上的称为istream_iterator,拥有输入功能;绑定到ostream对象身上的称为ostream_iterator,拥有输出功能。
具体实现方法:istream_iterator绑定一个istream对象,其实就是istream_iterator内部维护一个istream成员,客户端对于这个迭代器所做的operator++操作,会被导引调用迭代器内部所含的那个istream成员的输出操作operator>>,此时只要迭代器中有value值就operator>>输入;ostream_iterator绑定一个ostream对象,其实就是ostream_iterator内部维护一个ostream成员,客户端对于这个迭代器所做的operator=操作,会被导引调用迭代器内部所含的那个ostream成员的输入操作operator<<,此时只要迭代器调用赋值操作就operator<<将得到的值输出。
template <class T, class Distance = ptrdiff_t>
class istream_iterator {
friend bool
operator== __STL_NULL_TMPL_ARGS (const istream_iterator<T, Distance>& x,
const istream_iterator<T, Distance>& y);
protected:
istream* stream;
T value;
bool end_marker;
void read() {
end_marker = (*stream) ? true : false;
if (end_marker) *stream >> value;
end_marker = (*stream) ? true : false;
}
public:
typedef input_iterator_tag iterator_category;
typedef T value_type;
typedef Distance difference_type;
typedef const T* pointer;
typedef const T& reference;
istream_iterator() : stream(&cin), end_marker(false) {}
istream_iterator(istream& s) : stream(&s) { read(); }
reference operator*() const { return value; }
#ifndef __SGI_STL_NO_ARROW_OPERATOR
pointer operator->() const { return &(operator*()); }
#endif /* __SGI_STL_NO_ARROW_OPERATOR */
istream_iterator<T, Distance>& operator++() {
read();
return *this;
}
istream_iterator<T, Distance> operator++(int) {
istream_iterator<T, Distance> tmp = *this;
read();
return tmp;
}
};
template <class T>
class ostream_iterator {
protected:
ostream* stream;
const char* string;
public:
typedef output_iterator_tag iterator_category;
typedef void value_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
ostream_iterator(ostream& s) : stream(&s), string(0) {}
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; }
};
4、仿函数配接器
函数配接器内藏一个成员对象,其类别等同于它所要配接的对象。
(1)not1、not2
not1、not2对返回值进行逻辑否定
template <class Predicate>
class unary_negate
: public unary_function<typename Predicate::argument_type, bool> {
protected:
Predicate pred;
public:
explicit unary_negate(const Predicate& x) : pred(x) {}
bool operator()(const typename Predicate::argument_type& x) const {
return !pred(x);
}
};
template <class Predicate>
inline unary_negate<Predicate> not1(const Predicate& pred) {
return unary_negate<Predicate>(pred);
}
template <class Predicate>
class binary_negate
: public binary_function<typename Predicate::first_argument_type,
typename Predicate::second_argument_type,
bool> {
protected:
Predicate pred;
public:
explicit binary_negate(const Predicate& x) : pred(x) {}
bool operator()(const typename Predicate::first_argument_type& x,
const typename Predicate::second_argument_type& y) const {
return !pred(x, y);
}
};
template <class Predicate>
inline binary_negate<Predicate> not2(const Predicate& pred) {
return binary_negate<Predicate>(pred);
}
(2)bind1st、bind2nd
bind1st、bind2nd对参数进行绑定
template <class Operation>
class binder1st
: public unary_function<typename Operation::second_argument_type,
typename Operation::result_type> {
protected:
Operation op;
typename Operation::first_argument_type value;
public:
binder1st(const Operation& x,
const typename Operation::first_argument_type& y)
: op(x), value(y) {}
typename Operation::result_type
operator()(const typename Operation::second_argument_type& x) const {
return op(value, x);
}
};
template <class Operation, class T>
inline binder1st<Operation> bind1st(const Operation& op, const T& x) {
typedef typename Operation::first_argument_type arg1_type;
return binder1st<Operation>(op, arg1_type(x));
}
template <class Operation>
class binder2nd
: public unary_function<typename Operation::first_argument_type,
typename Operation::result_type> {
protected:
Operation op;
typename Operation::second_argument_type value;
public:
binder2nd(const Operation& x,
const typename Operation::second_argument_type& y)
: op(x), value(y) {}
typename Operation::result_type
operator()(const typename Operation::first_argument_type& x) const {
return op(x, value);
}
};
template <class Operation, class T>
inline binder2nd<Operation> bind2nd(const Operation& op, const T& x) {
typedef typename Operation::second_argument_type arg2_type;
return binder2nd<Operation>(op, arg2_type(x));
}
(3)compose1、compose2
compose1、compose2用于函数合成
template <class Operation1, class Operation2>
class unary_compose : public unary_function<typename Operation2::argument_type,
typename Operation1::result_type> {
protected:
Operation1 op1;
Operation2 op2;
public:
unary_compose(const Operation1& x, const Operation2& y) : op1(x), op2(y) {}
typename Operation1::result_type
operator()(const typename Operation2::argument_type& x) const {
return op1(op2(x));
}
};
template <class Operation1, class Operation2>
inline unary_compose<Operation1, Operation2> compose1(const Operation1& op1,
const Operation2& op2) {
return unary_compose<Operation1, Operation2>(op1, op2);
}
template <class Operation1, class Operation2, class Operation3>
class binary_compose
: public unary_function<typename Operation2::argument_type,
typename Operation1::result_type> {
protected:
Operation1 op1;
Operation2 op2;
Operation3 op3;
public:
binary_compose(const Operation1& x, const Operation2& y,
const Operation3& z) : op1(x), op2(y), op3(z) { }
typename Operation1::result_type
operator()(const typename Operation2::argument_type& x) const {
return op1(op2(x), op3(x));
}
};
template <class Operation1, class Operation2, class Operation3>
inline binary_compose<Operation1, Operation2, Operation3>
compose2(const Operation1& op1, const Operation2& op2, const Operation3& op3) {
return binary_compose<Operation1, Operation2, Operation3>(op1, op2, op3);
}
(4)ptr_fun
ptr_fun能够将一般函数当做仿函数使用,主要实现方法就是重载operator()操作符。一般函数当做仿函数传给STL算法,就语言层面就是可以的,就好像原生指针可被当做迭代器传给STL算法。这样的目的是一般函数没有配接能力,无法传给STL算法。
template <class Arg, class Result>
class pointer_to_unary_function : public unary_function<Arg, Result> {
protected:
Result (*ptr)(Arg);
public:
pointer_to_unary_function() {}
explicit pointer_to_unary_function(Result (*x)(Arg)) : ptr(x) {}
Result operator()(Arg x) const { return ptr(x); }
};
template <class Arg, class Result>
inline pointer_to_unary_function<Arg, Result> ptr_fun(Result (*x)(Arg)) {
return pointer_to_unary_function<Arg, Result>(x);
}
template <class Arg1, class Arg2, class Result>
class pointer_to_binary_function : public binary_function<Arg1, Arg2, Result> {
protected:
Result (*ptr)(Arg1, Arg2);
public:
pointer_to_binary_function() {}
explicit pointer_to_binary_function(Result (*x)(Arg1, Arg2)) : ptr(x) {}
Result operator()(Arg1 x, Arg2 y) const { return ptr(x, y); }
};
template <class Arg1, class Arg2, class Result>
inline pointer_to_binary_function<Arg1, Arg2, Result>
ptr_fun(Result (*x)(Arg1, Arg2)) {
return pointer_to_binary_function<Arg1, Arg2, Result>(x);
}
(5)mem_fun、mem_fun_ref
mem_fun能够将成员函数当做仿函数使用,主要实现方法就是重载operator()操作符,于是成员函数可以搭配各种泛型算法。当容器的元素类别是X&或X*,而我们又以虚拟成员函数作为仿函数,便可以藉由泛型算法完成所谓的多态调用,这是泛型与多态之间的一个重要接轨。
template <class S, class T>
class mem_fun_t : public unary_function<T*, S> {
public:
explicit mem_fun_t(S (T::*pf)()) : f(pf) {}
S operator()(T* p) const { return (p->*f)(); }
private:
S (T::*f)();
};
template <class S, class T>
class const_mem_fun_t : public unary_function<const T*, S> {
public:
explicit const_mem_fun_t(S (T::*pf)() const) : f(pf) {}
S operator()(const T* p) const { return (p->*f)(); }
private:
S (T::*f)() const;
};
template <class S, class T>
class mem_fun_ref_t : public unary_function<T, S> {
public:
explicit mem_fun_ref_t(S (T::*pf)()) : f(pf) {}
S operator()(T& r) const { return (r.*f)(); }
private:
S (T::*f)();
};
template <class S, class T>
class const_mem_fun_ref_t : public unary_function<T, S> {
public:
explicit const_mem_fun_ref_t(S (T::*pf)() const) : f(pf) {}
S operator()(const T& r) const { return (r.*f)(); }
private:
S (T::*f)() const;
};
template <class S, class T, class A>
class mem_fun1_t : public binary_function<T*, A, S> {
public:
explicit mem_fun1_t(S (T::*pf)(A)) : f(pf) {}
S operator()(T* p, A x) const { return (p->*f)(x); }
private:
S (T::*f)(A);
};
template <class S, class T, class A>
class const_mem_fun1_t : public binary_function<const T*, A, S> {
public:
explicit const_mem_fun1_t(S (T::*pf)(A) const) : f(pf) {}
S operator()(const T* p, A x) const { return (p->*f)(x); }
private:
S (T::*f)(A) const;
};
template <class S, class T, class A>
class mem_fun1_ref_t : public binary_function<T, A, S> {
public:
explicit mem_fun1_ref_t(S (T::*pf)(A)) : f(pf) {}
S operator()(T& r, A x) const { return (r.*f)(x); }
private:
S (T::*f)(A);
};
template <class S, class T, class A>
class const_mem_fun1_ref_t : public binary_function<T, A, S> {
public:
explicit const_mem_fun1_ref_t(S (T::*pf)(A) const) : f(pf) {}
S operator()(const T& r, A x) const { return (r.*f)(x); }
private:
S (T::*f)(A) const;
};
#ifdef __STL_CLASS_PARTIAL_SPECIALIZATION
template <class T>
class mem_fun_t<void, T> : public unary_function<T*, void> {
public:
explicit mem_fun_t(void (T::*pf)()) : f(pf) {}
void operator()(T* p) const { (p->*f)(); }
private:
void (T::*f)();
};
template <class T>
class const_mem_fun_t<void, T> : public unary_function<const T*, void> {
public:
explicit const_mem_fun_t(void (T::*pf)() const) : f(pf) {}
void operator()(const T* p) const { (p->*f)(); }
private:
void (T::*f)() const;
};
template <class T>
class mem_fun_ref_t<void, T> : public unary_function<T, void> {
public:
explicit mem_fun_ref_t(void (T::*pf)()) : f(pf) {}
void operator()(T& r) const { (r.*f)(); }
private:
void (T::*f)();
};
template <class T>
class const_mem_fun_ref_t<void, T> : public unary_function<T, void> {
public:
explicit const_mem_fun_ref_t(void (T::*pf)() const) : f(pf) {}
void operator()(const T& r) const { (r.*f)(); }
private:
void (T::*f)() const;
};
template <class T, class A>
class mem_fun1_t<void, T, A> : public binary_function<T*, A, void> {
public:
explicit mem_fun1_t(void (T::*pf)(A)) : f(pf) {}
void operator()(T* p, A x) const { (p->*f)(x); }
private:
void (T::*f)(A);
};
template <class T, class A>
class const_mem_fun1_t<void, T, A> : public binary_function<const T*, A, void> {
public:
explicit const_mem_fun1_t(void (T::*pf)(A) const) : f(pf) {}
void operator()(const T* p, A x) const { (p->*f)(x); }
private:
void (T::*f)(A) const;
};
template <class T, class A>
class mem_fun1_ref_t<void, T, A> : public binary_function<T, A, void> {
public:
explicit mem_fun1_ref_t(void (T::*pf)(A)) : f(pf) {}
void operator()(T& r, A x) const { (r.*f)(x); }
private:
void (T::*f)(A);
};
template <class T, class A>
class const_mem_fun1_ref_t<void, T, A> : public binary_function<T, A, void> {
public:
explicit const_mem_fun1_ref_t(void (T::*pf)(A) const) : f(pf) {}
void operator()(const T& r, A x) const { (r.*f)(x); }
private:
void (T::*f)(A) const;
};
#endif /* __STL_CLASS_PARTIAL_SPECIALIZATION */
// Mem_fun adaptor helper functions. There are only four:
// mem_fun, mem_fun_ref, mem_fun1, mem_fun1_ref.
template <class S, class T>
inline mem_fun_t<S,T> mem_fun(S (T::*f)()) {
return mem_fun_t<S,T>(f);
}
template <class S, class T>
inline const_mem_fun_t<S,T> mem_fun(S (T::*f)() const) {
return const_mem_fun_t<S,T>(f);
}
template <class S, class T>
inline mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)()) {
return mem_fun_ref_t<S,T>(f);
}
template <class S, class T>
inline const_mem_fun_ref_t<S,T> mem_fun_ref(S (T::*f)() const) {
return const_mem_fun_ref_t<S,T>(f);
}
template <class S, class T, class A>
inline mem_fun1_t<S,T,A> mem_fun1(S (T::*f)(A)) {
return mem_fun1_t<S,T,A>(f);
}
template <class S, class T, class A>
inline const_mem_fun1_t<S,T,A> mem_fun1(S (T::*f)(A) const) {
return const_mem_fun1_t<S,T,A>(f);
}
template <class S, class T, class A>
inline mem_fun1_ref_t<S,T,A> mem_fun1_ref(S (T::*f)(A)) {
return mem_fun1_ref_t<S,T,A>(f);
}
template <class S, class T, class A>
inline const_mem_fun1_ref_t<S,T,A> mem_fun1_ref(S (T::*f)(A) const) {
return const_mem_fun1_ref_t<S,T,A>(f);
}
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_FUNCTION_H */