如何从Rcpp函数打印整数向量?在我的功能中,我想打印IntegerVector a.在RI中使用例如调用此函数compnz_next(5,3,c(1,2,2))
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
IntegerVector compnz_next(int n, int k, IntegerVector a) {
bool more = true;
int i;
static int h = 0;
static int t = 0;
for ( i = 0; i < k; i++ ) {
a[i] = a[i] - 1;
}
if ( 1 < t ) {
h = 0;
}
h = h + 1;
t = a[h-1];
a[h-1] = 0;
a[0] = t - 1;
a[h] = a[h] + 1;
more = ( a[k-1] != ( n - k ) );
Rcout << "a vector is:" << more << std::endl;
for ( i = 0; i < k; i++ ) {
a[i] = a[i] + 1;
}
return a;
}
Run Code Online (Sandbox Code Playgroud)
G. *_*eck 14
请尝试以下行:
Rf_PrintValue(a);
Run Code Online (Sandbox Code Playgroud)
为了完整性,稍后,我们现在还有两个选择:
R> library(Rcpp)
R> cppFunction('void printVector(IntegerVector v) { print(v); } ')
R> printVector(c(1L, 3L, 5L))
[1] 1 3 5
Run Code Online (Sandbox Code Playgroud)
这只是将Rf_PrintValue()函数从R 包装成一个更容易输入的print()函数.
R> cppFunction('void printVector2(IntegerVector v) {
+ Rcpp::Rcout << v << std::endl; } ')
R> printVector2(c(1L, 3L, 5L))
1 3 5
R>
Run Code Online (Sandbox Code Playgroud)
(较新的)函数通过适当的方式实现,operator()<<因此我们可以<<像使用其他C++类型一样使用.它也适用于数字向量和矩阵.