简体   繁体   English

在 C++ 的嵌套 for 循环中迭代 map 的时间复杂度是多少?

[英]What is the time complexity for iterating through a map in nested for loop in C++?

I know the map in C++ is a balanced binary search tree.我知道 C++ 中的 map 是平衡二叉搜索树。 But, when I iterate through the map in nested for loop, I wonder what the time complexity would be但是,当我在嵌套 for 循环中遍历 map 时,我想知道时间复杂度是多少

for (auto x : mp) {
        for (auto y : mp) {
              //
              //
        }
 }

The C++ standard requires that forward iterators are amortized constant time, so each of your loops are Theta(n). C++ 标准要求前向迭代器是摊销常数时间,因此您的每个循环都是 Theta(n)。 Because they're nested, the complexity will be Theta(n^2).因为它们是嵌套的,所以复杂度将是 Theta(n^2)。

If you have a (balanced or not) binary search tree with parent links, then incrementing an iterator is O(log n) in the worst case, but O(1) amortized over the whole tree iteration, because each node in the traversal is visited at most 3 times (once when you first encounter the node, once after traversing the left subtree below it, and once after traversing the right subtree below it).如果您有一个(平衡与否)具有父链接的二叉搜索树,那么在最坏的情况下增加一个迭代器是 O(log n),但 O(1) 在整个树迭代中摊销,因为遍历中的每个节点都是最多访问 3 次(第一次遇到节点时,遍历其下的左子树后一次,遍历其下的右子树后一次)。

Depends on the implementation of the iterator of mp .取决于mp的迭代器的实现。 Every for-loop creates one iterator instance, but in the background they could rely on the same objects.每个 for 循环都会创建一个迭代器实例,但在后台它们可以依赖相同的对象。

In the most and simple cases, it will be outer over inner loop, so easily O(n²).在大多数和最简单的情况下,它将在外循环内循环,所以很容易 O(n²)。

In a strange case it might be O(n) but then the implementation of the iterator is messed by logic (maybe someone can comment a good reason)在一个奇怪的情况下,它可能是 O(n) 但是迭代器的实现被逻辑弄乱了(也许有人可以评论一个很好的理由)

And in a even stranger case it doesn't determine, when the first iterator is disturbed by the creation of the next one.在更奇怪的情况下,它无法确定第一个迭代器何时被下一个迭代器的创建所干扰。 The main reason there would be lazy static loop variables or static pointer used in the iterators class.主要原因是在迭代器 class 中使用了惰性 static 循环变量或 static 指针。

And to your special case when the elements are only available as a binary tree, think about how an iterator there will work: next() will be either left, right or none(=backwards up), so a fixed search length with the maximum recursive tree length.对于您的特殊情况,当元素只能作为二叉树使用时,请考虑那里的迭代器将如何工作:next() 将是左、右或无(=向后),因此具有最大的固定搜索长度递归树的长度。 Balanced means there is in general a set of l/r and at the tip log(n) elements of none, regarding this as a constant factor meaning n*=nx log(n).平衡意味着通常有一组 l/r 并且在尖端 log(n) 元素没有,将其视为常数因子,表示 n*=nx log(n)。 So the expected answer is O( (n*log(n))² ) = O( n² * log(n)² ) and regarding what outruns the Landau is the first part, so again O(n²) when it comes to the relevant number of elements.所以预期的答案是 O( (n*log(n))² ) = O( n² * log(n)² ) 并且关于什么超过了 Landau 是第一部分,所以当涉及到 O(n²)相关数量的元素。

Even then it may be performance wise to check the inner structure of map and create an iterator based on the keys, not on map.get().即使这样,检查 map 的内部结构并基于键创建迭代器,而不是基于 map.get() 可能是性能明智的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM