STL max_element 的复杂度

发布于 2024-09-11 04:55:43 字数 331 浏览 1 评论 0原文

所以根据这里的链接: http://www.cplusplus.com/reference/algorithm/ max_element/max_element 函数的复杂度为 O(n),显然对于所有 STL 容器而言。这是正确的吗?一个集合(作为二叉树实现)不应该是 O(log n) 吗?

与此相关的是,我一直使用 cplusplus.com 来解决更容易回答的问题,但我很好奇其他人对该网站的看法。

So according to the link here: http://www.cplusplus.com/reference/algorithm/max_element/ , the max_element function is O(n), apparently for all STL containers. Is this correct? Shouldn't it be O(log n) for a set (implemented as a binary tree)?

On a somewhat related note, I've always used cplusplus.com for questions which are easier to answer, but I would be curious what others think of the site.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

放血 2024-09-18 04:55:43

它是线性的,因为它涉及到每个元素。

使用相同的比较器在集合或其他有序容器上使用它是没有意义的,因为您可以在恒定时间内使用 .rbegin() 。

如果您不使用相同的比较函数,则无法保证顺序会一致,因此,它必须触及每个元素,并且必须至少是线性的。

尽管算法可能专门针对不同的迭代器类别,但无法根据迭代器范围是否有序来专门化它们。

大多数算法都适用于无序范围(包括 max_element),一些算法需要对范围进行排序(例如 set_unionset_intersection),有些则需要其他属性范围(例如 push_heappop_heap)。

It's linear because it touches every element.

It's pointless to even use it on a set or other ordered container using the same comparator because you can just use .rbegin() in constant time.

If you're not using the same comparison function there's no guarantee that the orders will coincide so, again, it has to touch every element and has to be at least linear.

Although algorithms may be specialized for different iterator categories there is no way to specialize them base on whether an iterator range is ordered.

Most algorithms work on unordered ranges (max_element included), a few require the ranges to be ordered (e.g. set_union, set_intersection) some require other properties for the range (e.g. push_heap, pop_heap).

岁月如刀 2024-09-18 04:55:43

对于所有 STL 容器,max_element 函数的复杂度都是 O(n)。

这是不正确的,因为 max_element 适用于迭代器,而不是容器。如果您给它来自集合的迭代器,它无法知道它们来自集合,因此将遍历所有迭代器以查找最大值。所以正确的句子是:

所有前向迭代器的 max_element 函数都是 O(n)

此外,如果您知道自己正在操作一个集合,那么您已经可以使用比 O(n) 更快地提供最大元素的方法,那么为什么要使用 最大元素

The max_element function is O(n) for all STL containers.

This is incorrect, because max_element applies to iterators, not containers. Should you give it iterators from a set, it has no way of knowing they come from a set and will therefore traverse all of them in order looking for the maximum. So the correct sentence is:

The max_element function is O(n) for all forward iterators

Besides, if you know that you're manipulating a set, you already have access to methods that give you the max element faster than O(n), so why use max_element ?

小女人ら 2024-09-18 04:55:43

它是一个STL算法,所以它不知道关于容器的任何信息。因此,这种线性搜索是它在前向迭代器上可以做的最好的事情。

It is an STL algorithm, so it does not know anything about the container. So this linear search is the best it can do on a couple on forward iterators.

十秒萌定你 2024-09-18 04:55:43

STL 算法不知道您从哪个容器中获取迭代器、它是否是有序的以及使用了什么顺序约束。它是一种线性算法,检查范围内的所有元素,同时跟踪迄今为止看到的最大值。

请注意,即使您可以使用元编程技术来检测从中获取迭代器的容器类型,也不能保证您可以跳到最后一个元素来获取最大值:

int values[] = { 1, 2, 3, 4, 5 };
std::set<int, greater<int> > the_set( values, values+5 );
std::max_element( the_set.begin(), the_set.end() ); //??

即使迭代器来自集合,它也是如此。不是最后一个,而是第一个包含最大值的元素。对于更复杂的数据类型,可以使用与最小/最大值无关的其他键对集合进行排序。

STL algorithms do not know what container you took the iterators from, whether or not it is ordered and what order constraints were used. It is a linear algorithm that checks all elements in the range while keeping track of the maximum value seen so far.

Note that even if you could use metaprogramming techniques to detect what type of container where the iterators obtained from that is not a guarantee that you can just skip to the last element to obtain the maximum:

int values[] = { 1, 2, 3, 4, 5 };
std::set<int, greater<int> > the_set( values, values+5 );
std::max_element( the_set.begin(), the_set.end() ); //??

Even if the iterators come from a set, it is not the last, but the first element the one that holds the maximum. With more complex data types the set can be ordered with some other key that can be unrelated to the min/max values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文