c++ - 调用模板函数内的迭代器“指向”的函子
问题描述
下面的模板函数为存储在向量中的一系列二进制函数对象apply_all
采用迭代器范围 ( )。当我迭代时,我希望使用两个给定的参数和. 结果将被写入( ),但我不知道如何使用迭代器变量调用仿函数:itBegin -> itEnd
a
b
Output Iterator
outIt
template <typename InIt, typename OutIt, typename A, typename B>
void apply_all(InIt itBegin, InIt itEnd, OutIt outIt, const A& a, const B& b)
{
for(auto it = itBegin; it != itEnd; it++)
{
*outIt = ... // how do I call the functor pointed to by *it?
outIt++;
}
}
解决方案
在 C++17 中,只需使用std::invoke
*outIt = std::invoke(*it, a, b);
或普通(如果不是指向成员函数的指针)
*outIt = (*it)( a, b);
推荐阅读
- visual-studio-code - VSCode - 使用 javascript 文件时,如何为块注释添加一对新的自定义分隔符?
- django - 如何创建一个折线图,指示用户在哪个月份写了更多或更少的博客?
- amazon-emr - 将 Spark 处理的中间数据复制到目标 S3 时出现 AWS EMR 性能问题
- python - 子类不继承父类
- python - Discord.py 附件未出现在邮件返回中
- php - Laravel livewire:你将如何实现等效于 Vue 的 v-if v-else
- java - 在登录按钮单击时,我想将密码从“0000”重置为当前日期格式,即使重新启动应用程序 pswd 应该是日期格式
- arrays - 从 Angular 中的 observable 保存数据时遇到问题
- typescript - TypeScript 声明特定的 SCSS 模块
- html - 如何将 li 元素放在同一行?