在 C/C++ 中是否有与 GNU-R 等效的函数,它()
Is there an equivalent function in C/C++ to GNU-R which()?
让我解释一下'which'函数的作用:
来自 GNU-R 帮助:
哪些指数是正确的?
给出逻辑对象的"TRUE"索引,允许数组索引。
或显示一些代码:(GNU-R 从 1 开始计算索引)
> x <- c(1,2,3,1,3,5);
> which(x == 1);
[1] 1 4
> which(x == 3);
[1] 3 5
> ll <- c(TRUE,FALSE,TRUE,NA,FALSE,FALSE,TRUE);
> which(ll);
[1] 1 3 7
有谁知道 C/C++ 中的类似功能?
感谢您的帮助
林尼
你必须
明白R是矢量化的,而C首先是处理单个原子数据片段:单个int
,double
,...
通过C++,您可以研究用于解决此问题的 STL 算法。
最后,在R和C++交叉点,我们的Rcpp包在C++中有一些矢量化操作,模仿了一些操作;参见Rcpp-sugar pdf小插图了解更多信息(和/或我们在Rcpp上的一些演讲)。
创建一个可以使用匹配值初始化的函子对象,并使用 std::for_each
对列表进行迭代。 所以例如:
vector<int> values;
//fill your vector with values;
struct match_functor
{
vector<int> value_array;
int match_value;
match_functor(int value): match_value(value) {}
void operator() (int input_value)
{
if(match_value == input_value)
value_array.push_back(input_value);
}
};
match_functor matches(1);
std::for_each(values.begin(), values.end(), matches);
现在可以使用 matches.value_array[INDEX]
访问结果值数组。
作为替代方法,如果您只想获得原始向量的指示,而不是实际值,那么您可以对函子对象执行以下操作:
struct match_functor
{
vector<int> index_array;
int match_value;
int index;
match_functor(int value): match_value(value), index(0) {}
void operator() (int input_value)
{
if(match_value == input_value)
index_array.push_back(index);
index++;
}
};
match_functor matches(1);
matches = std::for_each(values.begin(), values.end(), matches);
现在matches.index_array[INDEX]
将保存与值1
匹配的原始向量的指示,而不是原始向量的实际值。
算法std::find_if
应该可以解决问题 - 结合我应该添加的循环。
相关文章:
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- lapacke/gnu c :lapacke_zheevx()函数中的怪异错误
- GNU g++ 4.9.2 查找函数调用的编译错误
- GNU c 和 c++ 中的 SIMD 函数问题
- 使用 GNU/Linux read() 函数的正确方法
- G++ 4.6 -std=gnu++0x:静态局部变量构造函数调用计时和线程安全
- 如何阻止GNU GCC破坏dll导入函数名
- 如何在 Google App Engine Python 2.7 运行时中模拟 GNU C 库 drem / 余数函数
- GMP(GNU 多重精度):"mpz_mod"函数错误
- GNU gdb无法进入模板函数(OS X Mavericks)
- 在 C/C++ 中是否有与 GNU-R 等效的函数,它()
- 我应该在openMP并行区域(for循环,任务)内使用gnu并行模式函数吗?