I am wondering if there is a Rcpp
way to convert an element or iterator of const CharacterVector&
to std::string
. If I try the following code
void as(const CharacterVector& src) {
std::string glue;
for(int i = 0;i < src.size();i++) {
glue.assign(src[i]);
}
}
a compiler-time error will occurred:
no known conversion for argument 1 from ‘const type {aka SEXPREC* const}’ to ‘const char*’
So far, I use C API to do the conversion:
glue.assign(CHAR(STRING_ELT(src.asSexp(), i)));
My Rcpp version is 0.10.2.
By the way, I do know there is a Rcpp::as
.
glue.assign(Rcpp::as<std::string>(src[i]));
the above code will produce a runtime-error:
Error: expecting a string
On the otherhand, the following code run correctly:
typedef std::vector< std::string > StrVec;
StrVec glue( Rcpp::as<StrVec>(src) );
However, I do not want to create a temporal long vector of string in my case.
Thanks for answering.