Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.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
C++ 带有lambda的模板类_C++_Templates_Lambda_C++17 - Fatal编程技术网

C++ 带有lambda的模板类

C++ 带有lambda的模板类,c++,templates,lambda,c++17,C++,Templates,Lambda,C++17,让我们假设您对向量或数组的每个元素执行一些操作。还假设我们要计算“满足”某个谓词的所有元素 因为lambda必须有状态,我们可以这样做: #include <cstdint> #include <functional> template<typename T, class F> class CountAcummulator{ size_t count = 0; F f; public: constexpr CountAcummul

让我们假设您对向量或数组的每个元素执行一些操作。还假设我们要计算“满足”某个谓词的所有元素

因为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();
}
#包括
#包括
模板
类计数算符{
大小\u t计数=0;
F;
公众:
constexpr计数算符(F):F(std::move(F)){
constexpr void运算符()(T const&a){
if(std::invoke(f,a))
++计数;
}
constexpr size\u t get()常量{
返回计数;
}
};
int main(){
int x[]={1,2,3,4,5};
自动f=[](整数a){
返回a>3;
};
计数算符ca{f};
用于(自动i:x){
//做点什么
ca(i);
}
返回ca.get();
}
链接:

不幸的是,我发现这很难使用。如果我们添加投影(另一个lambda),这个类将不可用


有什么方法可以改进这个类以使其可用吗?

有一个标准算法,称为计算满足谓词的所有元素。这应该是首选的方法,除非你有充分的理由不这样做

#include <algorithm>

int main(){
    int x[] = { 1, 2, 3, 4, 5 };

    auto f = [](int a){
        return a > 3;
    };

    return std::count_if(std::begin(x), std::end(x), f);
}
#包括
int main(){
int x[]={1,2,3,4,5};
自动f=[](整数a){
返回a>3;
};
返回std::count_if(std::begin(x),std::end(x),f);
}

。当然可以,但假设我们对每个数组进行处理,同时进行一些其他处理。查看示例实现并模仿它。您不需要自定义类来实现此算法。假设我有对象向量,我需要执行几个计数if、min、max、sum(累计)和删除一些uniq_ptr。我需要单程完成。你想改进吗?如果你只是想得到一般性的批评,这可能是错误的网站。但是,如果你一次只能数一件事,那就数一数。如果我想找到10个不同的计数,同时做一些昂贵的工作呢。另外,如果迭代器也很昂贵,并且在迭代器上运行的速度非常慢怎么办?@Nick我可能会使用std::acculate一次计算多个事物。我看不出迭代器速度慢有什么关系——不管你做什么,你都必须对它们进行一次迭代。没有任何算法可以避免访问每个元素一次。如果迭代器速度慢,则3次计数将花费3倍以上的时间。关于累加,我可以在std::touple中累加?您可以使用任何类型作为累加器,包括tuple。