[英]fast kth element of sorted array implementation
我试图从JF 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功能,因此必须使用compile标志
-std=c++11
要么
-std=c++0x
对于第二个问题,您可能缺少某些标头或代码确实有问题。
编辑:issorted可能引用了std::is_sorted
。 可在include #include <algorithm>
更换issorted
通过std::is_sorted
。
(如果不存在,则添加#include <algorithm>
)
(使用C ++ 11编译)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.