C++ 是否可以 random_shuffle 一个 int 元素数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14720134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to random_shuffle an array of int elements?
提问by Computernerd
I was reading up on this : http://www.cplusplus.com/reference/algorithm/random_shuffle/and wondered if its possible to random_shuffle an array of int elements. This is my code
我正在阅读这个:http: //www.cplusplus.com/reference/algorithm/random_shuffle/ 并想知道是否有可能 random_shuffle 一个 int 元素数组。这是我的代码
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[10]={1,2,3,4,5,6,7,8,9,10};
cout << a << endl << endl;
random_shuffle(a[0],a[9]);
cout<<a;
}
I got this error:
我收到此错误:
error C2893: Failed to specialize function template
'iterator_traits<_Iter>::difference_type *std::_Dist_type(_Iter)'.
My question are:
我的问题是:
Is it possible to shuffle an int array using
random_shuffle
. If yes, I would like to learn how to do it.Is
random_shuffle
only applicable to templates?What does my error mean?
是否可以使用
random_shuffle
. 如果是的话,我想学习如何去做。是否
random_shuffle
只适用于模板?我的错误是什么意思?
回答by dasblinkenlight
You need to pass pointers to a[0]
and a[10]
, not the elements themselves:
您需要将指针传递给a[0]
and a[10]
,而不是元素本身:
random_shuffle(&a[0], &a[10]); // end must be 10, not 9
In C++11, you can use std::begin
and std::end
:
在 C++11 中,您可以使用std::begin
和std::end
:
random_shuffle(std::begin(a), std::end(a));
回答by Theo20185
Try replacing
尝试更换
random_shuffle(a[0],a[9]);
with
和
random_shuffle(&a[0], &a[10]);
From: http://www.java2s.com/Code/Cpp/STL-Basics/Userandomshufflealgorithmswitharray.htm
来自:http: //www.java2s.com/Code/Cpp/STL-Basics/Userandomshufflealgorithmswitharray.htm
回答by Mankarse
random_shuffle
takes iterators, rather than elements. Try either:
random_shuffle
接受迭代器,而不是元素。尝试:
std::random_shuffle(a, a + 10);
or
或者
std::random_shuffle(std::begin(a), std::end(a));
std::random_shuffle
can be used on any pair of random access iterators, and will shuffle the elements in the range denoted by those iterators.
std::random_shuffle
可用于任何一对随机访问迭代器,并将对这些迭代器表示的范围内的元素进行打乱。
The error occurs because int
s are not iterators, and so std::random_shuffle
is unable to use the given int
s as iterators.
发生错误是因为int
s 不是迭代器,因此std::random_shuffle
无法使用给定的int
s 作为迭代器。
回答by Renato Aloi
Worked for me this way:
以这种方式为我工作:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[10]={0,1,2,3,4,5,6,7,8,9};
for (unsigned i = 0; i < 10; i++)
{
cout << a[i];
}
cout << endl;
random_shuffle(&a[0],&a[10]);
for (unsigned i = 0; i < 10; i++)
{
cout << a[i];
}
cout << endl;
}
回答by Grant Wilson
Just changing the arr to a pointer does not solve the solution. This will make the array swap to one type of permutation. This means that if you rerun the program, your array will be shuffled into the exact same way as it did in the previous run.
仅将 arr 更改为指针并不能解决解决方案。这将使数组交换为一种排列。这意味着如果您重新运行该程序,您的数组将按照与上次运行时完全相同的方式进行洗牌。
To fix this - the function offers a third parameter which acts as a seed. So the correct implementation of the function is as follows.
为了解决这个问题 - 该函数提供了第三个参数作为种子。所以函数的正确实现如下。
1) Have a function or a lamda that generates a random number. This will act as your seed.
1) 具有生成随机数的函数或 lamda。这将充当您的种子。
int myrandom (int i) { return std::rand()%i;}
Make sure to set the seed of the internal random number generator.
确保设置内部随机数生成器的种子。
std::srand ( unsigned ( std::time(0) ) );
2) Insert this function as the third arguement in the random_shuffle function call.
2) 将此函数作为 random_shuffle 函数调用中的第三个参数插入。
std::random_shuffle ( myvector.begin(), myvector.end(), myrandom);
This will result in an always random shuffled array. Make sure to include the following:
这将导致始终随机打乱的数组。确保包括以下内容:
#include <algorithm> // std::random_shuffle
#include <vector> // std::vector
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand