排序数组实现的快速第k个元素
fast kth element of sorted array implementation
我试图从J.F.Sebastian那里理解这个算法,但我试图编译它(gcc/g++ 4.8
(得到一个奇怪的编译器错误:
const int n=101,m=31,k=*16-1;
int i;
srand(time(NULL));
for(i=0;i<n;i++) x[i]=rand();
std::sort(x,x+m,std::greater<float>());
std::sort(x+m,x+n,std::greater<float>());
float v=nsmallest_iter(x,x+m,x+m+1,x+n,n-1-k,std::greater<float>());
编辑
添加我得到的-std=c++11
标志:
from blabla.cpp:2:
blabla.cpp: In instantiation of ‘typename std::iterator_traits<_Iterator>::value_type nsmallest_iter(RandomAccessIterator, RandomAccessIterator, RandomAccessIterator, RandomAccessIterator, size_t, Compare) [with RandomAccessIterator = float*; Compare = std::greater<float>; typename std::iterator_traits<_Iterator>::value_type = float; size_t = long unsigned int]’:
blabla.cpp:58:64: required from here
blabla.cpp:28:66: error: ‘issorted’ was not declared in this scope
assert(issorted(firsta,lasta,less) && issorted(firstb,lastb,less));
^
blabla.cpp:28:35: error: ‘issorted’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
assert(issorted(firsta,lasta,less) && issorted(firstb,lastb,less));
^
blabla.cpp:28:66: note: ‘issorted’ declared here, later in the translation unit
assert(issorted(firsta,lasta,less) && issorted(firstb,lastb,less));
^
有人知道怎么解决这个问题吗?
您使用的是C++11功能,因此您必须使用编译标志
-std=c++11
或
-std=c++0x
对于第二个问题,您可能缺少一些标头,或者代码有问题。
edit:isorted可能指的是std::is_sorted
。它可以在include #include <algorithm>
中找到
用std::is_sorted
替换issorted
(如果不存在,则添加#include <algorithm>
(
(用C++11编译(
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 如何将元素添加到数组的线程安全函数?
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 输出没有重复元素的动态数组(收缩数组)C++
- 数组中最大的非重复元素
- 数组元素打印的递归方法
- 对字符数组中的元素执行逐位操作
- 缓存std::数组的选定元素,并在c++中自动保持其一致性
- 如何计算数组中元素的位数?(不是数组的长度),并计算其数字的总和
- 通过交换元素使数组相同
- 按平均值替换数组中的元素
- 打印矢量数组中的所有元素
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- 如何将元素从向量转移到新数组?
- 如何在向量数组中插入元素?
- 为什么 2 个相同数组的元素彼此相等
- C++ 中的二维整数数组,每行中的元素数量不均匀