【问题标题】:Data structures equivalents of STL containersSTL 容器的数据结构等价物
【发布时间】:2012-06-19 08:21:37
【问题描述】:

我研究数据结构,我想问一下 STL 容器的等价物是什么。

例如

  • vector = 动态数组
  • 队列 = 队列
  • 堆栈 = 堆栈
  • priority_queue = 堆
  • list = 链表
  • 设置 = 树
  • slist = 单链表
  • bit_vector = 向量布尔
  • 地图 = 对
  • 双端队列 = ?
  • 多重设置 = ?
  • 多图 = ?
  • hash_set = ?
  • hash_map = ?
  • hash_multiset = ?
  • hash_multimap = ?
  • 哈希 = ?
  • bit_set = ?

【问题讨论】:

  • deque = 双端队列。最像一个向量,您也可以在其中 push_front 元素。 multi_set = 多个值(不需要有唯一值); multi_map = 一个键可以关联多个值。 map != pair 但 map 的容器元素是 pair。
  • std::hash 不是容器,而是函子。
  • 史蒂夫:当你说它是函子时,你能说得更具体些吗?
  • @Jay:嗯,std::hash 是一个类模板。 std::hash 的实例化是类。这些类的实例是可调用的。因此,模板、类和对象这三者都可以合理地描述为“函子”。

标签: c++ data-structures stl equivalent


【解决方案1】:

关于 C++ 标准库容器,标准本身尽量不要过多地谈论实现。然而,对插入、删除、查找、范围插入等复杂性的非常具体的限制意味着大多数实现对容器使用相同类型的数据结构。关于你的一些例子:

  • std::list : 双重链表
  • std::set、std::multiset、std::map、std::multimap:自平衡 二叉树,通常是红黑树。
  • hash_*:C++11 提供 unordered_set、unordered_map 和 multi 兄弟姐妹。这些是哈希表。
  • bitset:固定大小的数组

我相信 STL 遵循这些实现。

【讨论】:

    【解决方案2】:

    我认为将 std::map 限定为“对”是不正确的。实际上,有一个名为 std::pair 的 实用程序 实际上只是一对。 std::map 存储唯一键和非唯一值的方式可以使用类似于数组的语法,其索引可以是数字类型,也可以不是数字类型。

    编辑:感谢 juanchopanza,将“容器”更正为“实用程序”。

    【讨论】:

    • 迂腐提示:std::pair 不被视为容器,而是“实用程序”。
    【解决方案3】:

    set and multiset-二叉搜索树

    map 和 multimap - 二叉搜索树

    双端队列-双端队列

    hash* 容器是哈希关联容器,实现为哈希表。 例如。 hash_map 包含 pair<key,value> 使用哈希表查找。

    在位集中

    the individual elements are accessed as special references which mimic bool elements

    【讨论】:

      【解决方案4】:
      vector = dynamic array  
      
      queue = queue
      
      stack = stack
      
      priority_queue = priority queue based on binary heap 
                       priority_queue can be implemented using 
                       1. sorted array
                       2. sorted list ( unrolled list, skip list etc)
                       3. any other heap like Fibonacci heap, binomial heap
      
      list = doubly linked list 
      
      set = set based on AVL Tree ( usually ) 
            can implemented using any other binary balancing tree like 
            1. Binary Search Tree
            2. Red black Tree
            3. Splay Tree
      
      slist = single linked list
      
      map = map based on Red Black Tree ( usually ) 
            where every node is 'pair' structure
            can implemented using any other binary balancing tree like 
            1. Binary Search Tree
            2. AVL Tree
            3. Splay Tree
      
      deque = deque
      
      hash_set = set implemented using hash
      
      hash_map = hash table
      
      hash = 'Not Available', used as functor
      

      【讨论】:

        猜你喜欢
        • 2011-04-01
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 2011-05-09
        • 1970-01-01
        • 1970-01-01
        • 2011-01-20
        • 1970-01-01
        相关资源
        最近更新 更多