[英]What is the difference between shuffle and random_shuffle c++
函数std :: shuffle已在C ++ 11中引入:
template< class RandomIt, class URNG >
void shuffle( RandomIt first, RandomIt last, URNG&& g );
它与std :: random_shuffle的重载之一具有相同的签名,这也是在C ++ 11中引入的:
template< class RandomIt, class RandomFunc >
void random_shuffle( RandomIt first, RandomIt last, RandomFunc&& r );
不同之处在于第三个参数,其中:
URNG必须满足UniformRandomNumberGenerator的要求
这都是? 差异只是shuffle
执行额外的编译时检查? 这个行为是否相同?
std::random_shuffle
使用std::rand()
函数来随机化项目,而std::shuffle
使用urng
这是一个更好的随机生成器,虽然使用std::random_shuffle
的特定重载,你可以获得相同的行为(和std::shuffle
一样,但这要求你做一些工作来传递第三个参数。
观看Stephan T. Lavavej的演讲,他解释了为什么std::rand
是一个糟糕的函数以及C ++程序员应该使用的东西:
要点是, std::shuffle
是对std::random_shuffle
的改进,而C ++程序员应该更喜欢使用前者。
如果您在cppreference.com仔细阅读文档,你会发现, RandomFunc
传递给random_shuffle
具有不同的接口 。 它被调用为r(n)
。 这在C ++ 11之前就存在了。
std::shuffle
使用标准方法获取随机数并调用g()
。 这个标准化的随机数生成器与C ++ 11和std::shuffle
一起引入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.