r语言 - C++ / Rcpp - 更改字符串中的特定字符

r - C++ / Rcpp - Changing a specific character in a string

本文关键字:字符 字符串 C++ 语言 Rcpp      更新时间:2023-10-16

我是C和C++的新手,所以我在Rstudio中使用Rcpp库在元音之间将字符"S"更改为"Z"。 我正在尝试使用带有 .at 函数 t 的迭代器 i 将其更改为"Z",但该行似乎是错误的。 知道我做错了什么吗?

// [[Rcpp::depends(BH)]]
#include <Rcpp.h>
#include <boost/algorithm/string.hpp>
//cc represents the current_character
#define cc   *i
string vowels = "AEIOU";
/* Define .begin .end functions
*/
i = word.begin();
while(i != word.end()){
if( cc == 'S' && is(vowels,pc) && is(vowels,nc) ) {
// the line below seems to e wrong
word.at(i) == 'Z';
i += 1;
} else {
i += 1;
}
}

这个程序中有很多潜在的错误。

首先,正如@StephanLechner指出的那样,您可能不是指"=="

word.at(i( == 'Z';

宏"cc"非常危险,至少它需要一个((,但最好不要使用它。

循环中存在潜在的"偏差一"错误。

is(vowels,nc)

可能会在单词末尾使用一个。

is(vowels,nc)

NC 是什么?如果 nc 是另一个宏,它会隐藏 true 值。