mercredi 6 novembre 2019

Convert Rcpp::List to C++ vector of vectors of const char*

I'm trying to build an Rcpp interface to an existing C++ library that uses const char* for strings. I think I need to use Rcpp::List to pass some output indices, which is a ragged 2D array of strings. But I am having trouble converting this to the C++ primitives required by the external function.

#include <Rcpp.h>

// Enable C++11 via this plugin (Rcpp 0.10.3 or later)
// [[Rcpp::plugins(cpp11)]]

// [[Rcpp::export]]
void test(Rcpp::List IndexList){

      // convert list to this
      const std::vector<std::vector<const char*>> Indexes;
      for (int i = 0; i < IndexList.size(); i++){
          Rcpp::StringVector temp = IndexList[i];
          std::vector<const char*> temp2;
          temp2.clear();
          for (int j = 0; j < temp.size(); j++){
            Rcpp::Rcout << temp[j];
            temp2.push_back(temp[j]);
          }
          Indexes.push_back(temp2);
      }
}

/*** R

test(list(c("a", "b"), c("cde")))

*/

The line Indexes.push_back(temp2); throws an error which ever way I try to build this object (which I need to pass to another function).

Line 19 passing 'const std::vector<std::vector<const char*> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<const char*>; _Alloc = std::allocator<std::vector<const char*> >; std::vector<_Tp, _Alloc>::value_type = std::vector<const char*>]' discards qualifiers [-fpermissive]

Aucun commentaire:

Enregistrer un commentaire