'const Rcpp::CharacterVector&'的转换元素到“标准::字符串" [英] Converting element of 'const Rcpp::CharacterVector&' to 'std::string'

查看:28
本文介绍了'const Rcpp::CharacterVector&'的转换元素到“标准::字符串"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种 Rcpp 方法可以将 const CharacterVector& 的元素或迭代器转换为 std::string.如果我尝试以下代码

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]);
}
}

将发生编译器时错误:

no known conversion for argument 1 from ‘const type {aka SEXPREC* const}’ to ‘const char*’

到目前为止,我使用 C API 进行转换:

So far, I use C API to do the conversion:

glue.assign(CHAR(STRING_ELT(src.asSexp(), i)));

我的 Rcpp 版本是 0.10.2.

My Rcpp version is 0.10.2.

顺便说一下,我知道有一个 Rcpp::as.

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.

感谢您的回答.

推荐答案

在 Rcpp 0.12.7 中,我可以使用 Rcpp::as<std::vector<std::string>>.以下函数返回 test 数组的第二个元素:

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];
}

这篇关于'const Rcpp::CharacterVector&amp;'的转换元素到“标准::字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆