随机随机播放和函数指针 [英] Random shuffle and function pointers

查看:62
本文介绍了随机随机播放和函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下函数,该函数会在给定范围内生成一个整数:

I have the following function, which generates an integer in a given range:

 18 int choosecard(){
 19     boost::uniform_int<> dist(0,51);
 20     boost::variate_generator< boost::mt19937&, boost::uniform_int<> > cardchoice(gen, dist);
 21     return cardchoice();
 22 }

我想将其用作std :: random_shuffle的参数,以便可以对52个整数的向量进行混洗.

I want to use it as an argument to std::random_shuffle, so that I can shuffle a vector of 52 integers.

 64     int (*pchoosecard)(void) = choosecard;
 65 
 66     std::random_shuffle(cards.begin(), cards.end(), pchoosecard);

但是我收到了一个我不明白的错误:

However I get this error that I don't understand:

$ make
g++ -I /usr/local/boost_1_45_0/ -c main.cpp
In file included from /usr/include/c++/4.4/algorithm:62,
                 from /usr/local/boost_1_45_0/boost/any.hpp:13,
                 from /usr/local/boost_1_45_0/boost/program_options/value_semantic.hpp:12,
                 from /usr/local/boost_1_45_0/boost/program_options/options_description.hpp:13,
                 from /usr/local/boost_1_45_0/boost/program_options.hpp:15,
                 from main.cpp:1:
/usr/include/c++/4.4/bits/stl_algo.h: In function ‘void std::random_shuffle(_RAIter, _RAIter, _Generator&) [with _RAIter = __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, _Generator = int (*)()]’:
main.cpp:66:   instantiated from here
/usr/include/c++/4.4/bits/stl_algo.h:4998: error: too many arguments to function
make: *** [main.o] Error 1

当我注释掉调用std :: random_shuffle的行时,一切编译正常.

Everything compiles fine when I comment out the line that calls std::random_shuffle.

有人能阐明这个问题吗?

Is anyone able to shed some light on this problem?

推荐答案

参数太多"是因为 std :: random_shuffle 试图将太多参数传递给您给它的回调.因此,实际上很少个参数.

There are "too many arguments" because std::random_shuffle is trying to pass too many arguments to the callback that you gave it. So it's really too few parameters.

您的 choosecard 应该接受一个类型为 ptrdiff_t 的参数,该参数基本上告诉函数该元素的数量.(您不应在此函数中对卡的数量进行硬编码.)因此,将其插入 boost :: uniform_dist 构造函数.

Your choosecard is supposed to accept one parameter, of type ptrdiff_t, which tells the function the number of elements, basically. (You are not supposed to hardcode the number of cards in this function.) So you plug that in to the boost::uniform_dist constructor.

这篇关于随机随机播放和函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆