简体   繁体   English

建立比较器功能的向量

[英]Make a vector of comparator functions

My problem is to test an implementation of quicksort with different comparator functions: std::less and std::greater . 我的问题是测试具有不同比较器功能的quicksort的实现: std::lessstd::greater But I do not want to copy-paste testing code that differs just in two comparators, so I would like to put them into a vector (or maybe something else?) and iterate over them. 但是我不想复制粘贴仅在两个比较器中不同的测试代码,因此我想将它们放入向量(或其他东西)中并对其进行迭代。

To simplify this post, lets say I would like to write a loop over a vector of two functions that get 0 and 1 as their arguments and output a boolean. 为了简化这篇文章,可以说我想在两个函数的向量上编写一个循环,这些函数以01作为参数并输出一个布尔值。 Here is my take at that: 这是我的看法:

#include <iostream>
#include <vector>
#include <functional>

int main() {
    auto fs = std::vector<>{std::less<int>{}, std::greater<int>{}};

    for (auto f: fs) {
        std::cout << f(0, 1) << " ";
    } std::cout << std::endl;
}

My g++ 6.1.1 compiler rightfully complains that I have not specified the template arguments for the vector. 我的g++ 6.1.1编译器正确地抱怨我没有为向量指定模板参数。 I have been trying things like std::function<bool(int, int)> and others with no luck. 我一直在尝试诸如std::function<bool(int, int)>而其他方法都没有运气。

Could you tell me how to fix this piece of code? 您能告诉我如何修复这段代码吗?


Update: The exact error I am getting: 更新:我得到的确切错误:

% g++ -std=c++14 -Wall deleteme.cpp && ./a.out
deleteme.cpp: In function ‘int main()’:
deleteme.cpp:6:27: error: wrong number of template arguments (0, should be at least 1)
     auto fs = std::vector<>{std::less<int>{}, std::greater<int>{}};
                           ^
In file included from /usr/include/c++/6.1.1/vector:64:0,
                 from deleteme.cpp:2:
/usr/include/c++/6.1.1/bits/stl_vector.h:214:11: note: provided for ‘template<class _Tp, class _Alloc> class std::vector’
     class vector : protected _Vector_base<_Tp, _Alloc>
           ^~~~~~
deleteme.cpp:8:18: error: unable to deduce ‘auto&&’ from ‘fs’
     for (auto f: fs) {
                  ^~

Only from C++17, template arguments of the constructor can be used to deduce the template arguments of the type, so you will have to write std::vector<std::function<bool(int,int)>> instead of std::vector<> . 仅从C ++ 17开始,构造函数的模板参数可用于推断类型的模板参数,因此您将必须编写std::vector<std::function<bool(int,int)>>而不是std::vector<>

Please note that std::function has a performance overhead compared to calling functions directly, so you might want to check out variadic template arguments (and swallowing ) for getting the last few percentages 请注意,与直接调用函数相比, std::function具有性能开销,因此您可能需要检查可变参数模板参数(并吞下 )以获取最后几个百分比

As you use different types, you might use tuple, and iterate over tuple: 当您使用不同的类型时,您可能会使用元组,并遍历元组:

namespace detail {
    template <typename F, typename TUPLE, std::size_t ... Is>
    void run_for_each(F&& f, TUPLE&& t, std::index_sequence<Is...>)
    {
        const int dummy[] = {0, (f(std::get<Is>(std::forward<TUPLE>(t))), void(), 0)...};
        static_cast<void>(dummy); // Avoid warning for unused variable
    }
}

template <typename F, typename TUPLE>
void run_for_each(F&& f, TUPLE&& t)
{
     detail::run_for_each(std::forward<F>(f),
                          std::forward<TUPLE>(t),
                          std::make_index_sequence<
                              std::tuple_size<std::decay_t<TUPLE>>::value>());
}

And then 接着

int main() {
    auto t = std::make_tuple(std::less<int>{}, std::greater<int>{});

    run_for_each([](auto&&f) {std::cout << f(0, 1) << " ";}, t);
    std::cout << std::endl;
}

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM