【问题标题】:How to find the depth of each node in std::map?如何在std :: map中找到每个节点的深度?
【发布时间】:2018-10-12 02:31:44
【问题描述】:

如果我构建自己的二叉树,那么我可以找到每个节点的深度。 示例代码如下

template<class datatype>
void binary_node<datatype>::printNodeWithDepth(int currentNodeDepth)
{
    if ( left )
        left->printNodeWithDepth(currentNodeDepth+1);
    std::cout << value << " and the depth is " << currentNodeDepth << std::endl;
    if ( right)
        right->printNodeWithDepth(currentNodeDepth+1);
}

但想知道,既然 map 是一个 b-tree,是否可以为 std::map 写类似的东西?

【问题讨论】:

标签: c++ dictionary std stdmap c++-standard-library


【解决方案1】:

std::map 不保证是一个 b-tree,它只是保证至少有同样好的运行时复杂度。为了打开其他潜在实现的大门,该接口不包括用于检查此类实现细节的功能。 :)

【讨论】:

  • 谢谢。想知道,就像人们如何越狱 iPhone,如果你们中的任何人对 std::map 做了一些调整?
  • @nsivakr:您可能希望使用(可视)调试器在运行时检查std::map
  • @nsivakr:您不是在问如何破坏特定的 STL 实现,而是在一般情况下如何破坏所有实现。比喻不是打破 iPhone,而是打破所有制造商的所有手机。 STL 在标头中实现...如果您真的想实现这一点,只需阅读您的特定实现是如何完成的。
  • 谢谢大卫。现在很有意义。
  • 我查看了 gcc 对 std::map 的实现,我可以告诉你当时(可能是 3.4?)他们使用的是红黑树。
猜你喜欢
  • 2021-12-02
  • 2011-04-25
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
相关资源
最近更新 更多