简体   繁体   English

迭代std :: set / std :: map的时间复杂度是多少?

[英]What's the time complexity of iterating through a std::set/std::map?

What's the time complexity of iterating through a std::set / std::multiset / std::map / std::multimap ? 迭代std::set / std::multiset / std::map / std::multimap的时间复杂度是多少? I believe that it is linear in the size of the set/map, but not so sure. 我相信它在集合/地图的大小上是线性的,但不是那么肯定。 Is it specified in the language standard? 是否在语言标准中指定?

In the draft C++11 standard N3337 the answer can be found in § 24.2.1 paragraph 8: C ++ 11标准N3337草案中 ,答案可以在第24.2.1段第8段中找到:

All the categories of iterators require only those functions that are realizable for a given category in constant time (amortized). 所有迭代器类别仅需要在恒定时间内(摊销)可以为给定类别实现的那些函数。

Since each operation on an iterator must be constant time, iterating through n elements must be O(n) . 由于迭代器上的每个操作必须是恒定时间,因此遍历n元素必须是O(n)

I believe that it is linear in the size of the set/map, but not so sure. 我相信它在集合/地图的大小上是线性的,但不是那么肯定。

That is correct. 那是正确的。 Iterating through an entire set or map is amortized O(N) 迭代整个集合或映射是摊销 O(N)

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

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