将Rcpp :: CharacterVector转换为std :: string [英] convert Rcpp::CharacterVector to std::string
问题描述
我尝试在 Rcpp 功能中打开文件,因此我需要文件名为char *或std :: string。
I am trying to open a file within an Rcpp function, so I need the file name as a char* or std::string.
到目前为止,我已经尝试过以下操作:
So far, I have tried the following:
#include <Rcpp.h>
#include <boost/algorithm/string.hpp>
#include <fstream>
#include <string>
RcppExport SEXP readData(SEXP f1) {
Rcpp::CharacterVector ff(f1);
std::string fname = Rcpp::as(ff);
std::ifstream fi;
fi.open(fname.c_str(),std::ios::in);
std::string line;
fi >> line;
Rcpp::CharacterVector rline = Rcpp::wrap(line);
return rline;
}
但显然, as
对于 Rcpp :: CharacterVector
不起作用,因为我得到了编译时错误。
But apparently, as
does not work for Rcpp::CharacterVector
as I get a compile time error.
foo.cpp: In function 'SEXPREC* readData(SEXPREC*)':
foo.cpp:8: error: no matching function for call to 'as(Rcpp::CharacterVector&)'
make: *** [foo.o] Error 1
有一个简单的方法一个字符串从参数或以某种方式从Rcpp函数参数打开一个文件?
Is there a simple way to get a string from the argument or somehow open a file from the Rcpp function argument?
推荐答案
Rcpp :: as()
期望 SEXP
作为输入,而不是 Rcpp :: CharacterVector
。尝试将 f1
参数直接传递给 Rcpp :: as()
,例如:
Rcpp::as()
expects a SEXP
as input, not a Rcpp::CharacterVector
. Try passing the f1
parameter directly to Rcpp::as()
, eg:
std::string fname = Rcpp::as(f1);
或:
std::string fname = Rcpp::as<std::string>(f1);
这篇关于将Rcpp :: CharacterVector转换为std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!