将Rcpp :: List转换为const char *向量的C ++向量

问题描述 投票:0回答:2

我正在尝试为使用const char*作为字符串的现有C ++库构建Rcpp接口。我想我需要使用Rcpp :: List传递一些输出索引,这是一个参差不齐的2D字符串数组。但是我很难将其转换为外部函数所需的C ++原语。

#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")))

*/

[C0行]在尝试构建该对象的任何方式中都会引发错误(我需要将其传递给另一个函数)。>>

Indexes.push_back(temp2);

解决方案

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]

我正在尝试为使用const char *作为字符串的现有C ++库构建Rcpp接口。我想我需要使用Rcpp :: List传递一些输出索引,这是一个参差不齐的2D字符串数组。 ...

c++11 vector rcpp
2个回答
2
投票

您将#include <Rcpp.h> // Enable C++11 via this plugin (Rcpp 0.10.3 or later) // [[Rcpp::plugins(cpp11)]] // function that wants this data structure void feedme(const std::vector<std::vector<const char *>> &Indexes){ Rcpp::Rcout << " feedme "; for (unsigned i = 0; i < Indexes.size(); i++){ for (unsigned j = 0; j < Indexes[i].size(); j++){ Rcpp::Rcout << Indexes[i][j]; } } } // [[Rcpp::export]] void test(Rcpp::List IndexList){ // convert list to this Rcpp::Rcout << " test "; 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); } feedme(Indexes); } /*** R test(list(c("a", "b"), c("cde"))) */ 声明为Indexes,但稍后尝试更改它:


2
投票

也许这会有所帮助。这里,我们从constRcpp::StringVectortwo

© www.soinside.com 2019 - 2024. All rights reserved.