- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我在 http://www.sgi.com/tech/stl/nth_element.html 阅读了 std::nth_element
的描述
template <class RandomAccessIterator>
void nth_element(RandomAccessIterator first, RandomAccessIterator nth,
RandomAccessIterator last);
注意前提是
我的问题是:
调用 std::nth_element(a.begin(), a.end(), a.end())
是否有效?如果有,它的作用是什么?无论如何,它不违反上述先决条件。语言标准(或其他文档)中的任何地方声明 nth
必须指向 a
中的元素?
最佳答案
它是有效的,可能是空操作,但标准不保证。使用给定的数据,两个先决条件变为:
[a.begin(), a.end()) is a valid range.
[a.end(), a.end()) is a valid range.
这两个都是真的,虽然第二个间隔是空的。来自标准 25.3.2/1:
After nth_element the element in the position pointed to by nth is the element that would be in that position if the whole range were sorted. Also for any iterator i in the range [first, nth) and any iterator j in the range [nth, last) it holds that: !(*i > *j) or comp(*j, *i) == false.
如果对整个范围进行排序,则原始 a.end()
将位于 a.end()
并且对于第二部分,范围 [nth , last)
是空的,所以没有要计算 !(*i > *j)
和 comp(*j, *i) == false< 的元素
条件。
关于c++ - std::nth_element(a.begin(), a.end(), a.end()) 有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11245359/
我很难掌握应该如何使用 std::nth_element ,因为我有点生疏。 裁判说: Rearranges the elements in the range [first,last), in su
有人知道 std::nth_element 的不同实现的预期运行时间和最坏情况下的运行时间吗?我几乎每天都使用这个算法。 我对最近的 Microsoft 编译器附带的 STL 版本特别感兴趣,但有关此
StackOverflow 和其他地方有很多声称 nth_element 是 O(n) 并且通常使用 Introselect 实现的声明:http://en.cppreference.com/w/cp
我明白 (1) std::nth_element对跨越 [first, last) 的数组元素进行排序,使得 !(*j > *i) 适用于 [first, nth) 中的任何 i 和 [nth, la
我正在尝试使用第 nth_element 找到 (x,y) 点的 vector 的中值 cv::Point2f medOffset; vector tempOffset
我想在一个类中将函数 nth_element 与我自己的排序函数(应该可以访问对象的数据)一起使用。目前,我正在做以下事情: class Foo { public: glm::vec3 *po
我想用 C++ 计算 float 组的中值: float Median( FloatArray const * constFloatArray ) { FloatArray scratc
我想从给定的未排序 vector 中获取第 n 个最小的元素。我发现标准库中有一个方法。但是我不明白下面的结果。 我使用条目为 {3,4,5,2,3} 的 vector ,并希望获得第二小的元素。如果
来自 std::nth_element 的文档我们有: template void nth_element( RandomIt first, RandomIt nth, RandomIt last )
前面章节中,已经给大家介绍了 sort()、stable_sort()、partial_sort() 这些函数的功能和用法,本节再介绍一个排序函数,即 nth_element() 函数。 不过,在系统
我看过一段代码: template T getMedian(vector& data_vec) { assert(!data_vec.empty()); typename vector
下面的代码将无法编译。第 2 行到最后一行 (nth_element...) 有错误。这似乎与比较器有关。编译器声称“术语不计算为采用 2 个参数的函数”。如何修复编译错误? struct R
我想在 python 中实现 Vantage Point Tree,但它使用 C++ 中的 std::nth_element。 所以我想在 Python 或 numpy 中找到等效的“nth_elem
根据 cppreference.com,C++ STL 排序算法的复杂度为: 排序:O(N log(N)) partial_sort:“大约”O(N log(M)),其中 M 是距离(中间优先) nt
我不想得到排序数组,只是第 n 个元素的值。例如,给定数组 a = [20, 5, 1, -3] 我希望能够查询 nth_element(a,2) = 1 在 C++ 中,有一个函数 std::nt
我在任何地方都没有找到这个特定的主题... 我在 23 个整数的 std::vector 中调用 nth_element() 算法大约 400,000 次,更精确的“无符号短”值。 我想提高计算速度,
我有一个算法可以在我的双核 3 GHz Intel 处理器上平均运行 250 毫秒,我正在尝试优化它。目前,我有一个 std::nth_element在 std::vector 上调用了大约 6,00
我正在将一些 C++ 代码移植到 C#。 C# 是否有等价于 std::nth_element()还是我需要自己动手? 最佳答案 我假设您正在寻找一个访问器,该访问器通过对集合执行部分排序来返回无序集
我感兴趣的是找到 vector 中等于中位数的第一个(最左边)元素的最有效方法。找到中位数很简单: std::nth_element(first, middle, last); auto median
我正在自学 c++ 和 eigen,所以也许这是一个简单的问题。 给定 n 和 0 "<"m "<"n,以及一个 n vector d 的 float 。具体来说: VectorXf d = Vect
我是一名优秀的程序员,十分优秀!