使用二进制搜索的模板递归

时间:2015-07-10 15:52:34

标签: c++ templates search recursion binary

考虑一下我写的这个工作代码:

/My name is \w+ and I'm \K\d+(?= years old.)/

所以#include <iostream> constexpr int MIN_FOO = 0, MAX_FOO = 100; template <int N> void foo() {std::cout << "foo<" << N << ">() called.\n";} template <int N> void foo (char, double, bool) {std::cout << "foo<" << N << ">(char, double, bool) called.\n";} template <int Low, int High, typename... Args> void searchFooBinary (int key, Args... args) { // if (LOW > HIGH) {std::cout << "Error with searchFooBinary.\n"; return;} constexpr int Mid = (Low + High) /2; if (key == Mid) foo<Mid>(std::forward<Args>(args)...); // Want to generalize this using 'f'. else if (key < Mid) searchFooBinary<Low, Mid - 1>(key, std::forward<Args>(args)...); else searchFooBinary<Mid + 1, High>(key, std::forward<Args>(args)...); } template <typename... Args> void executeFooBinarySearch (int n, Args... args) { searchFooBinary<MIN_FOO, MAX_FOO>(n, std::forward<Args>(args)...); } int main() { executeFooBinarySearch(99); executeFooBinarySearch (99, 'a', 1.5, true); } 是一个模板函数,这里传递一个运行时int,foo使用二进制搜索来找到模板参数的正确int值。到目前为止一切顺利,但我不想为每个新功能searchFooBinary编写这个二进制搜索功能。如何将foofoo的使用概括为更一般的searchFooBinary?如果不允许使用模板函数指针,那么实现此目标的解决方法是什么?

1 个答案:

答案 0 :(得分:2)

如何使用仿函数:

#include <iostream>

constexpr int MIN_FOO = 0,  MAX_FOO = 100;

struct Foo
{
    template <int N>
    void operator() (char, double, bool) {std::cout << "Foo<" << N << ">(char, double, bool) called.\n";}
};

struct Bar
{
    template <int N>
    void operator() () {std::cout << "Bar<" << N << ">() called.\n";}   
};


template <int Low, int High, typename Fun, typename... Args>
void searchBinary (int key, Fun f, Args... args)
{
    constexpr int Mid = (Low + High) /2;
    if (key == Mid)
    {
        f.template operator()<Mid>(std::forward<Args>(args)...);
    }
    else if (key < Mid)
    {
        searchBinary<Low, Mid - 1>(key, f, std::forward<Args>(args)...);
    }
    else
    {
        searchBinary<Mid + 1, High>(key, f, std::forward<Args>(args)...);
    }
}

template <typename Fun, typename... Args>
void executeBinarySearch (int n, Fun f, Args... args)
{
    searchBinary<MIN_FOO, MAX_FOO, Fun>(n, f, std::forward<Args>(args)...);
}

int main()
{
    executeBinarySearch (99, Foo(), 'a', 1.5, true);
    executeBinarySearch (99, Bar());    
}

<强>输出

Foo<99>(char, double, bool) called.
Bar<99>() called.

实例:http://ideone.com/fSoG5B

相关问题