STL源码剖析1

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值