11

我想使用 C++11 的可变参数模板来实现一个通用的“随机选择器”功能。

像这样的东西...

template <typename T>
T randomPicker(T one, T two, T three)
{
    int pick = 3 * (rand() / double(RAND_MAX));
    switch (pick)
    {
        case 0:
            return one;
        case 1:
            return two;
        default:
            return three;
    }
}

...除了一般化以接受任意数量的参数(每个类型相同,如上所述 - 尽管接受任何类型作为参数并在返回时将所选类型转换为某些特定类型 T 也是可以接受的)。

我理解使用模板递归来实现类型安全 printf 等的想法。可变参数模板也可以用于创建上述那种功能吗?任何提示表示赞赏!

4

5 回答 5

8

像这样的东西,虽然我无法测试它:

template <typename First, typename... Others>
First randompicker(First first, Others ...args) {
    const size_t len = sizeof...(args) + 1;
    if (rand() / double(RAND_MAX) < 1.0 / len) {
        return first;
    }
    return randompicker(args...);
}

template <typename Only>
Only randompicker(Only only) {
    return only;
}

I'm not sure whether the overload there is right -- presumably a parameter pack can be empty, I don't know whether I can still overload for one argument without ambiguity.

Admittedly this uses more random numbers than your example with 3 args, and may be more sensitive to bias from rounding errors. So, you could pick a random number from 0 to len-1 at the start, and then call a recursive function that selects the nth argument out of the parameter pack:

template <typename First, typename... Others>
First select(size_t idx, First first, Others ...args) {
    if (idx == 0) return first;
    return select(idx-1, args...);
}

template <typename Only>
Only select(size_t, Only only) {
    return only;
}

template <typename First, typename... Others>
First randompicker(First first, Others ...args) {
    static std::default_random_engine re;

    const size_t len = sizeof...(args) + 1;
    std::uniform_int_distribution<size_t> range{0, len - 1};

    const size_t idx = range(re);
    return select(idx, first, args...);
}

In all cases, I have n if/else statements instead of an n-way switch. You might be lucky with the optimizer, or you might be able to "unroll the loop" a bit by having First, Second ... A few parameter args before the variable args.

于 2011-11-09T23:12:40.450 回答
8

I am not sure if you have to use variadic templates, but IMHO it is easier to go with initializer lists.

#include <cstddef>
#include <iostream>
#include <random>
#include <algorithm>
#include <initializer_list>
using namespace std;

template <class T>
T random_picker(initializer_list<T> container){
    static default_random_engine re;
    uniform_int_distribution<size_t> range{0, container.size()-1};
    auto random_iterator = container.begin();
    advance(random_iterator, range(re));
    return *random_iterator;
}

int main(){
    for(size_t i = 0; i < 10; ++i)
        cout << random_picker({1, 2, 3, 4, 5, 6, 7, 8, 9, 10}) << endl;
}
于 2011-11-09T23:57:03.863 回答
3

一种方法是您可以执行以下操作:

template<typename T, typename... Args>
T randomPicker(T first, Args ...rest) {
    T array[sizeof...(rest) + 1] = {first, rest...};

    return array[rand() % (sizeof...(rest) + 1)];
}

在 IdeOne 上测试

于 2011-11-09T23:11:07.740 回答
2

This should work. randomPicker chooses which one of the parameters it will return. randomPicker_impl works through the paramters till the correct one is chosen. The overload for Last ensures the template expansion terminates.

Full working code here: ideone.com/2TEH1

template< typename Ret, typename Last >
Ret random_picker_impl( size_t i, Last&& last )
{
   return std::forward<Last>(last);
}

template< typename Ret, typename First, typename Second, typename ... Rest >
Ret random_picker_impl( size_t i, First&& first, Second&& second, Rest&&... rest )
{
   if( i == 0 )
   {
      return std::forward<First>(first);
   }
   else
   {
      return random_picker_impl<Ret>( i-1, std::forward<Second>(second), std::forward<Rest>(rest)... );
   }
}

template< typename First, typename ... Rest >
First random_picker( First&& first, Rest&&... rest )
{
   size_t index = (sizeof...(rest) + 1) * (std::rand() / double(RAND_MAX));
   return random_picker_impl<First>( index, std::forward<First>(first), std::forward<Rest>(rest)... );
}
于 2011-11-09T23:54:33.373 回答
0

The usual way to pick a random element of a linked list is by giving a 1/1, 1/2, 1/3, ..., 1/n chance to replacing the a picked element with the current element at each link.

#include <cstdlib>
#include <ctime>
#include <iostream>

namespace {
template<typename T>
T randomPickerHelper(int& sz, T first) {
    return first;
}
template<typename T, typename ...Args>
T randomPickerHelper(int& sz, T first, Args ...rest) {
    T next = randomPickerHelper(sz, rest...);
    return std::rand() % ++sz ? next : first;
}
}

template<typename T, typename... Args>
T randomPicker(T first, Args ...rest) {
    int sz = 1;
    return randomPickerHelper(sz, first, rest...);
}

int main() {
    std::srand(std::time(0));
    for (int i = 0; i < 1000000; ++i) {
        std::cout << randomPicker(1, 2, 3, 4, 5) << std::endl;
    }
    return 0;
}

Calls rand() a lot of times, though.


template<typename T, typename ...Args>
struct Count {
    static const int sz = Count<Args...>::sz + 1;
};
template<typename T>
struct Count<T> {
    static const int sz = 1;
};

template<typename T>
T pick(int n, T first) {
    return first;
}

template<int N, typename T, typename ...Args>
T pick(int n, T first, Args ...rest) {
    if (n == Count<T, Args...>::sz) {
        return first;
    }
    return pick(n, rest...);
}

template<typename T, typename ...Args>
T randomPicker(T first, Args ...rest) {
    return pick(std::rand() % Count<T, Args...>::sz + 1, first, rest...);
}

I feel like this should be possible, but GCC 4.6.0 doesn't support expanding <Args...>.

于 2011-11-10T00:04:13.033 回答