Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rcpp中字符串与单个值的矢量化比较_R_Rcpp - Fatal编程技术网

Rcpp中字符串与单个值的矢量化比较

Rcpp中字符串与单个值的矢量化比较,r,rcpp,R,Rcpp,当将数值向量与单个值进行比较时,Rcpp中的==运算符按预期工作。即,将向量的每个元素与值进行比较,并返回一个逻辑向量。例如,考虑下列行为,如预期: library(Rcpp) cppFunction(' CharacterVector test_vals(NumericVector x) { if (is_true(any(x == 3))) return ("Values include 3"); return ("3 not found");

当将数值向量与单个值进行比较时,Rcpp中的
==
运算符按预期工作。即,将向量的每个元素与值进行比较,并返回一个逻辑向量。例如,考虑下列行为,如预期:

library(Rcpp)
cppFunction('
CharacterVector test_vals(NumericVector x) {
  if (is_true(any(x == 3))) return ("Values include 3");
  return ("3 not found");
}')
test_vals(1:2)
# [1] "3 not found"
test_vals(1:5)
# [1] "Values include 3"
cppFunction('
CharacterVector test_names(NumericVector x) {
  CharacterVector y = x.attr("names");
  CharacterVector foo(x.size());
  foo.fill("foo");
  if (is_true(any(y == foo))) return ("Names include foo");
  return ("foo not found");
}')
test_names(c(a=1, b=2, foo=3))
# [1] "Names include foo"
test_names(c(foo=3, a=1, b=2))
# [1] "Names include foo"
test_names(c(a=1, b=2))
# [1] "foo not found"
但是,如果我尝试将字符向量与字符标量进行比较,它似乎只测试向量的第一个元素:

cppFunction('
CharacterVector test_names(NumericVector x) {
  CharacterVector y = x.attr("names");
  if (is_true(any(y == CharacterVector::create("foo")))) return ("Names include foo");
  return ("foo not found");
}')
test_names(c(a=1, b=2, foo=3))
# [1] "foo not found"
test_names(c(foo=3, a=1, b=2))
# [1] "Names include foo"
我知道,比较两个相同长度的字符向量似乎以向量化的方式工作,正如预期的那样:

library(Rcpp)
cppFunction('
CharacterVector test_vals(NumericVector x) {
  if (is_true(any(x == 3))) return ("Values include 3");
  return ("3 not found");
}')
test_vals(1:2)
# [1] "3 not found"
test_vals(1:5)
# [1] "Values include 3"
cppFunction('
CharacterVector test_names(NumericVector x) {
  CharacterVector y = x.attr("names");
  CharacterVector foo(x.size());
  foo.fill("foo");
  if (is_true(any(y == foo))) return ("Names include foo");
  return ("foo not found");
}')
test_names(c(a=1, b=2, foo=3))
# [1] "Names include foo"
test_names(c(foo=3, a=1, b=2))
# [1] "Names include foo"
test_names(c(a=1, b=2))
# [1] "foo not found"

这是否意味着在
Rcpp
中没有实现字符向量与单个值的比较,或者我只是不知道如何实现它?

在我们快速讨论之后,这里有一个非常简单的解决方案,因为问题(如所提出的)很简单——没有正则表达式,没有幻想。只需在所有元素上循环,并在找到匹配项后立即返回,否则将使用
false

代码
#包括
//[[Rcpp::导出]]
bool包含(std::vector sv,std::string txt){
用于(自动s:sv){
如果(s==txt)返回true;
}
返回false;
}
/***R
sv包含(sv,“foo”)
[1] 假的
>sv[2]包含(sv,“foo”)
[1] 真的
> 
这实际上只是从臀部开始拍摄,然后再寻找我们可能已经拥有的(大约)10万行Rcpp,或者STL可能拥有的


这同样适用于您前面的命名属性示例,当然,您可以使用相同的
CharacterVector
,和/或使用从它到我们在这里使用的
std::vector
的转换,或者。。。如果您使用的是较旧的编译器,请将的
从C++11样式切换到K+R样式。

问得好。它看起来确实像是
NumericVector
具有适当的
opeator=()
,但是
CharacterVector
可能没有(因为字符通常是不同的鱼群)。我们可以加上它;同时,您可能会为自己编写一个小助手,它可以“手动”处理两个向量。因此,简单地说,您需要一个“contains()”操作符来处理一个字符串向量和一个返回布尔值的字符串?将其表述为
==
对我的阅读来说有点“不合适”,因为这里有多对一的映射。你真的在看集合运算符,对吗?(我同意你的观点,
std::vector
是唯一最好的容器…)是的-这正是我想要的我认为我们应该看看其他的东西-想想
std::vector
并将它交给STL,STL已经可以这样做了…很高兴这有帮助。更多地考虑一下缺少
=
支持:我们通常不会像R那样“回收”(有些语言称之为“广播”)。因此,“多对一”比较有些未知。我认为
Rcpp
等价物是将
Rcpp::String
Rcpp::StringVector
(?)的每个元素进行比较。我不确定。正如我在上面所写的,将两条线连在一起比在文档的煤矿里挖掘要快……归根结底,这是一件事情,例如,
数据。table
已经做得很好,所以我不太担心。更多使用文本数据的人,例如
quanteda
团队可能知道更多。