【发布时间】:2020-01-22 13:15:36
【问题描述】:
为了我的优化,我想在 Rcpp 中得到一个像样的toupper。我对 C++ 很陌生,据我所知,我已经做到了:
#include <Rcpp.h>
using namespace Rcpp;
void C_toupper_String(const String& s) {
for (char *p =(char *)s.get_cstring();*p!=0;p++) *p = toupper(*p);
}
// [[Rcpp::export]]
StringVector C_toupper(StringVector const& vecteur) {
StringVector res=clone(vecteur);
for (int i(0); i < res.size(); ++i) {
C_toupper_String(res[i]);
}
return res;
}
/*** R
teststring <- "HeY I arNaud"
C_toupper(teststring)
toupper(teststring)
identical(C_toupper(teststring),toupper(teststring))
*/
但是,它不能正常工作。
> C_toupper(teststring)
[1] "HEY I ARNAUD"
> toupper(teststring)
[1] "HEY I ARNAUD"
> identical(C_toupper(teststring),toupper(teststring))
[1] FALSE
有什么问题?如果可能,我不想将String 转换为std::string,因为我想了解发生了什么:进入C++ 的目的是能够避免复制和转换。
谢谢,
阿诺
【问题讨论】:
-
我不确定你想要达到什么目的,因为
Rcpp::clone()会复制你的数据。 -
Ralf Stubner :我希望能够在我想要的时候做,而不是在我不想做的时候。我想了解 Rcpp::String 的工作原理,并直接能够安全地修改它。无论如何,它甚至不会复制数据,因为我注意到测试字符串已被修改......这完全失败了哈哈
-
teuder.github.io/rcpp4everyone_en/170_string.html get_cstring 正在制作副本,因此您修改的是副本,而不是原件
-
是什么让你认为在 Rcpp 中重新实现会比 R 的内置
toupper函数更快? -
@OlivierSohn 这不正确,链接也没有声明。