在C++中改组数组

在C++中改组数组,c++,python,C++,Python,可能重复: 我有一个小数组,我需要在数组中随机移动值。我可以用Python使用Realth.Stuffle来做这件事,但是我似乎可以在C++中找到如何做。 下面是一个python示例,说明了我想在C中执行的操作++ #!/usr/bin/python import random array = [1,2,3,4,5] random.shuffle(array) print array 您可以从中使用 以下是本页的一个基本示例: #include <algorithm>

可能重复:

我有一个小数组,我需要在数组中随机移动值。我可以用Python使用Realth.Stuffle来做这件事,但是我似乎可以在C++中找到如何做。 下面是一个python示例,说明了我想在C中执行的操作++


#!/usr/bin/python

import random

array = [1,2,3,4,5]

random.shuffle(array)

print array
您可以从中使用

以下是本页的一个基本示例:

#include <algorithm>                                                                                                    
#include <vector>                                                                                                       
#include <iostream>                                                                                                     
#include <iterator>                                                                                                     

int main()                                                                                                              
{                                                                                                                       
   const int SIZE=10;

   // create and initialize an array                                                                                                   
   int arr[] = {1,2,3,4,5,6,7,8,9,10};                                                                                  

   std::random_shuffle(arr, arr+SIZE);      

   // copy the contents of the array to output                                                                            
   std::copy(arr, arr+SIZE, std::ostream_iterator<int>(std::cout, " "));                                                
   std::cout << std::endl;                                                                                              

   // shuffling an std:: container, here it's std::vector                                                                                         
   std::vector<int> ivec(arr, arr+SIZE);                                                                                
   std::random_shuffle(ivec.begin(), ivec.end());                                                                       
   std::copy(ivec.begin(), ivec.end(), std::ostream_iterator<int>(std::cout, " "));                                     
}        
您可以使用任何使用随机访问迭代器的东西,比如std::vector或std::deque,或者像上面这样的普通数组。

您可以使用from

以下是本页的一个基本示例:

#include <algorithm>                                                                                                    
#include <vector>                                                                                                       
#include <iostream>                                                                                                     
#include <iterator>                                                                                                     

int main()                                                                                                              
{                                                                                                                       
   const int SIZE=10;

   // create and initialize an array                                                                                                   
   int arr[] = {1,2,3,4,5,6,7,8,9,10};                                                                                  

   std::random_shuffle(arr, arr+SIZE);      

   // copy the contents of the array to output                                                                            
   std::copy(arr, arr+SIZE, std::ostream_iterator<int>(std::cout, " "));                                                
   std::cout << std::endl;                                                                                              

   // shuffling an std:: container, here it's std::vector                                                                                         
   std::vector<int> ivec(arr, arr+SIZE);                                                                                
   std::random_shuffle(ivec.begin(), ivec.end());                                                                       
   std::copy(ivec.begin(), ivec.end(), std::ostream_iterator<int>(std::cout, " "));                                     
}        

你可以使用任何使用随机访问迭代器的东西,比如std::vector或std::deque,或者像上面这样的普通数组。

我不希望结果是随机数,它需要洗牌该数组中的固定数。基本上是将数字在arry@FrozenWasteland:这正是std::random_shuffle所做的…它会洗牌数组/容器的内容。@Birryrree我理解代码,但ostream_迭代器来自哪里,它不会编译。@FrozenWasteland-更新了一个完整的工作示例。SGI的示例代码非常简单,没有显示完整的头include等等。请注意,如果熵很重要,请使用重载,允许您将函数对象作为随机性源传递,或者使用std::shuffle。我不希望结果是随机数,它需要对该数组中的固定数进行随机移动。基本上是将数字在arry@FrozenWasteland:这正是std::random_shuffle所做的…它会洗牌数组/容器的内容。@Birryrree我理解代码,但ostream_迭代器来自哪里,它不会编译。@FrozenWasteland-更新了一个完整的工作示例。SGI的示例代码非常简单,没有显示完整的头includes等等。请注意,如果熵很重要,请使用重载,允许您将函数对象作为随机性源传递,或者使用std::shuffle.sample As[如何精确地实现一个数组的随机数或随机数算法?以随机顺序显示引号?],特别是STD::RealthyStuffle。在机会中,你对算法更感兴趣,而不是学习C++标准库,这里有一些阅读开始:同样的[如何精确地实现一个数组的随机数或随机数算法?以随机顺序显示引文?],特别是C++:随机变量。