Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的Rcpp快速排序错误? < >我使用RCPP来构建QuaseQuoice的C++函数Quask排序。 我试图通过R中的CXX函数编译和加载此函数。 我需要使用CXX功能。我在R中得到以下信息:_C++_R_Rcpp - Fatal编程技术网

R中的Rcpp快速排序错误? < >我使用RCPP来构建QuaseQuoice的C++函数Quask排序。 我试图通过R中的CXX函数编译和加载此函数。 我需要使用CXX功能。我在R中得到以下信息:

R中的Rcpp快速排序错误? < >我使用RCPP来构建QuaseQuoice的C++函数Quask排序。 我试图通过R中的CXX函数编译和加载此函数。 我需要使用CXX功能。我在R中得到以下信息:,c++,r,rcpp,C++,R,Rcpp,错误:无法将参数“1”的“Rcpp::IntegerVector{aka Rcpp::Vector}”转换为“int*”,无法将参数“1”转换为“int” 快速排序(int*,int,int)'make:**[file15ecb338ec.o]错误1 谁能告诉我这有什么问题吗 incl <- 'int quicksort(int xx[], int left, int right) { int i = left; int j = right; int tmp; int pivot =

错误:无法将参数“1”的“Rcpp::IntegerVector{aka Rcpp::Vector}”转换为“int*”,无法将参数“1”转换为“int” 快速排序(int*,int,int)'make:**[file15ecb338ec.o]错误1

谁能告诉我这有什么问题吗

incl <- 'int quicksort(int xx[], int left, int right) {

int i = left; 
int j = right;
int tmp;
int pivot = xx[(left + right) / 2];

/* partition */
while (i <= j) {
while (xx[i] < pivot)
i++;
while (xx[j] > pivot)
j--;
if (i <= j) {
tmp = xx[i];
xx[i] = xx[j];
xx[j] = tmp;
i++;
j--;
}
}

/* recursion */
if (left < j){
quicksort(xx, left, j);
}
if (i < right){
quicksort(xx, i, right);
}

return (quicksort(xx,i,j));
}
'
sortCpp <- cxxfunction(signature( x = "integer",left = "integer",
                                  right = "integer"),
                       body = 'IntegerVector arr(x);
                       int a = as<int>(left);
                       int b = as<int>(right);
                       return wrap(quicksort(arr,a,b));',
                       include = incl,
                       plugin = "Rcpp",
                       verbose = TRUE)

incl问题正是编译器所说的。您的函数需要一个
int*
,但您正在向它传递一个
Rcpp::IntegerVector
。查看for
Rcpp::IntegerVector
,可以通过
.begin()
检索原始指针,因此可以通过
快速排序(arr.begin(),a,b)
调用

另外,除非您真的需要使用自己的快速排序,否则我只使用标准库的
std::sort

src <- 'Rcpp::IntegerVector v = Rcpp::IntegerVector(vp);
        std::sort(v.begin(), v.end());
        return wrap(v);'
fun <- cxxfunction(signature(vp="integer"), body=src, plugin="Rcpp")
fun( c(5, 2, 7, -3) )

src I将CXX函数中的行修改为“return wrap(快速排序(arr.begin(),a,b));”我现在收到一个“错误:未在此范围内声明快速排序”错误?我将include放在cxxfunction中的body参数之前,并对其进行了修改,编译时没有出现错误!。当我在整数向量上运行sortCpp(y)时,我在sortCpp(y)中得到错误:缺少参数“left”,没有默认值?你知道这里有什么问题吗?你没有把
传递到
sortCpp
;您定义了
sortCpp
以期望这些参数。编写
quicksort
时,您为要排序的数组部分定义了左右索引。我不确定您是否将范围定义为包含[左,右]或半开放[左,右],但在前一种情况下,要对整个数组进行排序,您需要将
快速排序(arr.begin(),a,b)
替换为
快速排序(arr.begin(),0,arr.size()-1)
,在后一种情况下,您需要
快速排序(arr.begin(),0,arr.size())
。或者,如果要对数组中的特定范围进行排序,可以调用
sortCpp(someVector,左,右)当你用一种没有意义的方式连接我的两个表达式时,表达式是不正确的。我强烈建议你先对C++有更好的理解,否则你会遇到很多挫折。我也确信你的Quask排序不工作,因为它在所有控制路径上都是递归的。它会导致无限的。循环/堆栈溢出。
src <- 'Rcpp::IntegerVector v = Rcpp::IntegerVector(vp);
        std::sort(v.begin(), v.end());
        return wrap(v);'
fun <- cxxfunction(signature(vp="integer"), body=src, plugin="Rcpp")
fun( c(5, 2, 7, -3) )