zoukankan      html  css  js  c++  java
  • the philosophy behind of the design of the STL

    The concept of STL is based on a separation of data and operations. The data is managed by container classes, and the operations are defined by configurable algorithms. Iterators are the glue between these two components and they let any algorithm interact with any container.

     

    //violate using the iterator as the glue between the containers and the algorithm 
    template<T>
    Print(List<T> & container)
    {
        for(T item: container)
         {
            //...
         }
    }
     
    //correct
    template<T>
    Print(iterator<T> begin, iterator<T> end)
    {
        for(;begin < end && begin != end; begin ++)
        {
            //...
        }
    }
    1. The STL concept contradicts the original idea of object-oriented programming: the STL serarates data and algorithms rather than combining them. While you can combine every kinds of container with ever kind of algorithm, so the result is a very flexible but still rather small framework.
    2. The particular implementation of any container is not defined by the C++ standard library. However, the behavior and complexity secified by the standard do not leave much room for variation.
    3. The STL containers provide only those special member functions that in general have "good" performance, where "good" normal means constant or logarithmic complexity. e.g., a direct element access by using operator [] is not provided for lists. This is because lists don't provide random access, and so an operator [] would have bad performance. Which is also most like why there is no resize() for array. 
  • 相关阅读:
    Java框架-mybatis02基本的crud操作
    Java框架-mybatis01查询单个数据
    MAC常用命令
    性能测试工具Jmeter13-Jmeter跨线程组调用token
    性能测试工具Jmeter12-Jmeter连接配置带跳板机(SSH)的mysql服务器
    Java基础29-子父类中的成员变量
    Java基础28-继承
    Java基础27-单例设计模式
    启动项目时报spawn cmd ENOENT
    npm安装教程
  • 原文地址:https://www.cnblogs.com/Cmpl/p/3747328.html
Copyright © 2011-2022 走看看