STL max

编程入门 行业动态 更新时间:2024-10-13 16:21:42
本文介绍了STL max_element的复杂性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,根据此处的链接: www.cplusplus/reference/ algorithm / max_element / , max_element 函数是O(n),显然对于所有STL容器。它是否正确?应该不是一个集合的O(log n)(作为一个二叉树实现)?

So according to the link here: www.cplusplus/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)?

在一个有点相关的说明,我一直使用cplusplus

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

推荐答案

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

It's linear because it touches every element.

使用相同的比较器 甚至可以在集合或其他有序容器上使用它是毫无意义的,因为你可以使用 .rbegin()在常量时间。

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.

大多数算法适用于无序范围(包括 max_element 要求范围有序(例如, set_union , set_intersection )一些需要其他属性的范围(例如 push_heap , pop_heap )。

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).

更多推荐

STL max

本文发布于:2023-11-29 22:23:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:STL   max

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!