将 Rcpp::List 转换为 C++ vector of vectors of const char*
Convert Rcpp::List to C++ vector of vectors of const char*
我正在尝试为使用 const char*
字符串的现有 C++ 库构建一个 Rcpp 接口。我想我需要使用 Rcpp::List 来传递一些输出索引,这是一个参差不齐的二维字符串数组。但是我无法将其转换为外部函数所需的 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")))
*/
行 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]
解决方案
#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")))
*/
也许这有帮助。在这里,我们将 Rcpp::StringVector
和 const char *
和 char *
的两个向量从 Rcpp::StringVector
传递到 两个 向量。我有一半希望必须进行 const-cast 但它只是按原样构建。
代码
#include <Rcpp.h>
void consumeConstChar(std::vector<const char*> v) {
for (auto s : v) Rcpp::Rcout << s << std::endl;
}
void consumeChar(std::vector<char*> v) {
for (auto s : v) Rcpp::Rcout << s << std::endl;
}
// [[Rcpp::export]]
void doStuff(Rcpp::StringVector x) {
std::vector<const char*> vcc;
std::vector<char*> vc;
for (int i=0; i<x.size(); i++) {
vcc.push_back(x[i]);
vc.push_back(x[i]);
}
consumeConstChar(vcc);
consumeChar(vc);
}
/*** R
x <- c("The quick", "brown", "fox", "jumped")
doStuff(x)
*/
演示
R> Rcpp::sourceCpp("~/git/Whosebug/58741017/answer.cpp")
R> x <- c("The quick", "brown", "fox", "jumped")
R> doStuff(x)
The quick
brown
fox
jumped
The quick
brown
fox
jumped
R>
您将 Indexes
声明为 const
但稍后尝试更改它:
const std::vector<std::vector<const char*>> Indexes;
// ^^^^^
// [...]
Indexes.push_back(temp2);
删除 const
限定符以编译代码。
我正在尝试为使用 const char*
字符串的现有 C++ 库构建一个 Rcpp 接口。我想我需要使用 Rcpp::List 来传递一些输出索引,这是一个参差不齐的二维字符串数组。但是我无法将其转换为外部函数所需的 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")))
*/
行 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]
解决方案
#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")))
*/
也许这有帮助。在这里,我们将 Rcpp::StringVector
和 const char *
和 char *
的两个向量从 Rcpp::StringVector
传递到 两个 向量。我有一半希望必须进行 const-cast 但它只是按原样构建。
#include <Rcpp.h>
void consumeConstChar(std::vector<const char*> v) {
for (auto s : v) Rcpp::Rcout << s << std::endl;
}
void consumeChar(std::vector<char*> v) {
for (auto s : v) Rcpp::Rcout << s << std::endl;
}
// [[Rcpp::export]]
void doStuff(Rcpp::StringVector x) {
std::vector<const char*> vcc;
std::vector<char*> vc;
for (int i=0; i<x.size(); i++) {
vcc.push_back(x[i]);
vc.push_back(x[i]);
}
consumeConstChar(vcc);
consumeChar(vc);
}
/*** R
x <- c("The quick", "brown", "fox", "jumped")
doStuff(x)
*/
演示
R> Rcpp::sourceCpp("~/git/Whosebug/58741017/answer.cpp")
R> x <- c("The quick", "brown", "fox", "jumped")
R> doStuff(x)
The quick
brown
fox
jumped
The quick
brown
fox
jumped
R>
您将 Indexes
声明为 const
但稍后尝试更改它:
const std::vector<std::vector<const char*>> Indexes;
// ^^^^^
// [...]
Indexes.push_back(temp2);
删除 const
限定符以编译代码。