随机随机播放和函数指针

编程入门 行业动态 更新时间:2024-10-28 14:35:40
本文介绍了随机随机播放和函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

随机随机播放和函数指针

本文发布于:2023-10-28 08:11:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1536048.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!