stl概论
stl以抽象概念为主题而非以实际类为主的结构,形成了一个严谨的接口标准。在此接口之下,任何组件都有最大的独立性,并以所谓迭代器胶合起来,或以所谓配接器互相配接,或以所谓仿函数动态选择某种策略。
C++允许我们自行定义型别,C++template允许我们将型别参数化,藉由两者结合并透过traits变成技法形成STL。
stl六大组件:containers,algorithms,iterators,functors,adapters,allocators。
template<class T, class Sequence = deque<T> >
class stack
{
// friend bool operator == <T> (const stack<T>&, const stack<T>&); // OK
// friend bool operator == <T> (const stack&, const stack&); // OK
friend bool operator == <> (const stack&, const stack&); // OK
friend bool operator < <> (const stack&, const stack&);
// friend bool operator == (const stack&, const stack&); // error
};
..... // 实现省略
这种奇特的语法是为了实现所谓的 bound friend templates,也就是所说 class template 的某个具体实现与friend function template的某个具现体有一对一的关系。
int main()
{
stack<int> x;
stack<int> y;
cout << (x == y) << endl; // OK
stack<char> y1;
cout << (x == y1) << endl; // error
}