Article 2 careful about the illusion of container-independent code

  1. Generalized array of containers, parameterized object contains the type
  2. Function generalization of algorithm, parameterized with the iterator type
  3. Pointer generalizations iterators, parameterized type of object points

Second, similar container has a similar function

  1. Contiguous-memory containers provide random access iterators
  2. Provides bidirectional iterator node-based containers
  3. Container supports sequence push_frontor push_back, but does not support the associated container
  4. Complexity associated with the container to provide a number of times lower_bound, upper_boundand equal_rangemember functions, but the container has not sequence

Third, the use package --typdedf

  1. Do not write code like

    class Widget {...};
    vector<Widget> vw;
    Widget bestWidget;
    ... // 给bestWidget一个值
    vector<Widget>::iterator i = bestWidget相等的Widget
    find(vw.begin(), vw.end(), bestWidget);
    
  2. Using typedef define a container type, such that the change easier

    class Widget { ... };
    typedef vector<Widget> WidgetContainer;
    typedef WidgetContainer::iterator WCIterator;
    WidgetContainer cw;
    Widget bestWidget;
    ...
    WCIterator i = find(cw.begin(), cw.end(), bestWidget);
    

Third, the container type hidden in the class

  1. Do not use direct container, to create a class, the use of standard containers on the private area

    class CustomerList {
    private:
        typedef list<Customer> CustomerContainer;
        typedef CustomerContainer::iterator CCIterator;
        CustomerContainer customers;
    public: // 通过这个接口
        ... // 限制list特殊信息的可见性
    };
    

Original: Big Box  clause 2 Carefully container-independent code fantasy


Guess you like

Origin www.cnblogs.com/petewell/p/11607331.html