【发布时间】:2019-10-13 17:45:33
【问题描述】:
假设您对向量或数组的每个元素进行操作。还假设我们要计算所有“满足”某个谓词的元素。
因为 lambda 必须有状态,我们可以这样做:
#include <cstdint>
#include <functional>
template<typename T, class F>
class CountAcummulator{
size_t count = 0;
F f;
public:
constexpr CountAcummulator(F f) : f(std::move(f)){}
constexpr void operator()(T const &a){
if (std::invoke(f, a))
++count;
}
constexpr size_t get() const{
return count;
}
};
int main(){
int x[] = { 1, 2, 3, 4, 5 };
auto f = [](int a){
return a > 3;
};
CountAcummulator<int, decltype(f)> ca{ f };
for(auto i : x){
// do something
ca(i);
}
return ca.get();
}
链接:https://gcc.godbolt.org/z/pjtcCG
不幸的是,我发现这很难使用。如果我们添加投影(另一个 lambda),这个类将无法使用。
有什么办法可以改进这个类以使其可用?
【问题讨论】:
-
当然可以,但是假设我们在对数组进行 for_each 的同时进行其他一些处理。
-
查看示例实现并模仿它。您不需要自定义类来实现此算法。
-
假设我有对象向量,我需要做几个 count_if、min、max、sum(累积)并删除一些 uniq_ptr。我需要一次性完成。
-
您想要改进什么?如果您只想进行一般性批评,这可能是错误的网站。
标签: c++ templates lambda c++17