×
您的位置: 首页 > 编程笔记

shuffle和random_shuffle c ++之间的区别是什么(What is the difference between shuffle and random_shuffle c++)

C++ 时间:2020-06-15  查看:576   收藏

The function std::shuffle has been introduced in C++11:

template< class RandomIt, class URNG >
void shuffle( RandomIt first, RandomIt last, URNG&& g );

and it has the same signature as one of the overloads of std::random_shuffle which was also introduced in C++11:

template< class RandomIt, class RandomFunc >
void random_shuffle( RandomIt first, RandomIt last, RandomFunc&& r );

The difference is in the third parameter where:

URNG must meet the requirements of UniformRandomNumberGenerator

Is this all? Is the difference just that shuffle performs an extra compile time check? Is the behavior otherwise the same?

解决方案

If you read the documentation at cppreference.com closely, you will find that the RandomFunc passed to random_shuffle has a different interface. It is invoked as r(n). This existed before C++11.

std::shuffle uses a standardized way of getting random numbers and invokes g(). This standardized random number generators where introduced with C++11 together with std::shuffle.

函数 std :: shuffle 已在C ++ 11中引入:



 模板<类RandomIt,类URNG> 
 void shuffle(RandomIt first,RandomIt last,URNG& g);



,它具有与 std::random_shuffle 这也是在C ++ 11中引入的:



  template<类RandomIt,类RandomFunc> 
 void random_shuffle(RandomIt first,RandomIt last,RandomFunc& r);



区别在于第三个参数,其中:




URNG必须符合UniformRandomNumberGenerator的要求




这是全部吗?是不是只是 shuffle 执行额外的编译时检查?


解决方案

如果您仔细阅读cppreference.com上的文档,您会发现传递给 Random_shuffle 的RandomFunc 具有不同的接口。它被作为 r(n)调用。这存在于C ++ 11之前。



std :: shuffle 使用一种标准化的方式获取随机数和调用 g()。这个标准化的随机数生成器引入了C ++ 11和 std :: shuffle 。


 

0% (0)
0% (0)