2

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.

wush978
  • 3,114
  • 2
  • 19
  • 23
  • Dirk will probably tell you that you are more likely to get help with Rcpp questions on the [mailing list](https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel) as many of the devs are on there daily. – Simon O'Hanlon Mar 13 '13 at 10:34
  • We do watch what happens on SO. I'll post an answer here later. – Romain Francois Mar 13 '13 at 16:00

2 Answers2

1

I am confused as that what you want -- a CharacterVector is a vector of character strings (as in R) so you can only map it to std::vector<std::string> >. Here is a very simple, very manual example (and I thought we had auto-converters for this, but maybe not. Or no more.

#include <Rcpp.h>  

// [[Rcpp::export]] 
std::vector<std::string> ex(Rcpp::CharacterVector f) {  
  std::vector<std::string> s(f.size());   
  for (int i=0; i<f.size(); i++) {  
    s[i] = std::string(f[i]);  
  }  
  return(s);     
}

And here it is at work:

R> sourceCpp("/tmp/strings.cpp")
R> ex(c("The","brown","fox"))  
[1] "The"   "brown" "fox" 
R>
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Well, the conversion failed if I add a modifier `const` to `Rcpp::CharacterVector f` in your example. In my opinion, `const` is an important modifier for specifying a mutable or immutable variable. ([const-correctness](http://en.wikipedia.org/wiki/Const-correctness)). – wush978 Mar 14 '13 at 07:42
  • Therefore, there is no Rcpp way to do with `const`. I see. Thanks – wush978 Mar 15 '13 at 06:12
  • Not all possible `const` variants have been written. If *you* miss one, *you* should think about contributing a patch. – Dirk Eddelbuettel Mar 15 '13 at 10:34
  • I will, but not before knowing how to do it. I read [Rcpp Extending](http://cran.r-project.org/web/packages/Rcpp/vignettes/Rcpp-extending.pdf) this morning, but failed to make the implicit conversion work. – wush978 Mar 15 '13 at 12:43
  • You could start easier with just a one-off free function as an as<> replacement. Also, we deal with SEXP underneath so we cannot _really_ guarantee `const correctness` as the pointer leaks through. Which limits our motivation in adding all those signatures. But if you want to, go for it. – Dirk Eddelbuettel Mar 15 '13 at 14:14
  • I am interested in how to do the reverse. i.e. convert a std::vector into a CharacterVector – qed Jul 27 '14 at 01:41
1

In Rcpp 0.12.7, I can use Rcpp::as<std::vector<std::string> >. The following function returns the second element of the test array:

std::string test() {
  Rcpp::CharacterVector test = Rcpp::CharacterVector::create("a", "z");
  std::vector<std::string> test_string = Rcpp::as<std::vector<std::string> >(test);
  return test_string[1];
}
krlmlr
  • 25,056
  • 14
  • 120
  • 217