📜  在C ++中使用STL随机排列数组

📅  最后修改于: 2021-06-01 01:42:53             🧑  作者: Mango

给定一个数组,任务是将整个数组改组并打印。
例子

Input  (1, 2, 3, 4, 5, 6, 7}
Output  {3, 1, 6, 7, 2, 4, 5}

Input  (1, 2, 3}
Output  {3, 1, 2}

STL包含两种方法,可用于获取经过改组的数组。它们分别是shuffle()random_shuffle()

洗牌

此方法使用g作为统一随机数生成器,随机地重新排列[first,last)范围内的元素。它将每个元素的值与其他随机选取的元素的值交换。它通过调用g()确定所选择的元素。
模板

template 
  void shuffle (RandomAccessIterator first, 
                RandomAccessIterator last, 
                URNG&& g)
{
  for (auto i=(last-first)-1; i>0; --i) {
    std::uniform_int_distribution d(0, i);
    swap (first[i], first[d(g)]);
  }
}

执行

CPP
// C++ program to shuffle
// the given array
// using shuffle() method
 
#include 
using namespace std;
 
// Shuffle array
void shuffle_array(int arr[], int n)
{
 
    // To obtain a time-based seed
    unsigned seed = 0;
 
    // Shuffling our array
    shuffle(arr, arr + n,
            default_random_engine(seed));
 
    // Printing our array
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}
 
// Driver code
int main()
{
 
    int a[] = { 10, 20, 30, 40 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    shuffle_array(a, n);
 
    return 0;
}


CPP14
// C++ program to shuffle
// the given array
// using random_shuffle() method
 
#include 
using namespace std;
 
// Shuffle array
void shuffle_array(int arr[], int n)
{
 
    // To obtain a time-based seed
    unsigned seed = 0;
 
    // Shuffling our array using random_shuffle
    random_shuffle(arr, arr + n);
 
    // Printing our array
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}
 
// Driver code
int main()
{
 
    int a[] = { 10, 20, 30, 40 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    shuffle_array(a, n);
 
    return 0;
}


输出:
30 10 20 40

注意:由于程序中使用随机函数,每次输出可能会有所不同。

random_shuffle

此函数在[first,last)范围内随机重新排列元素。它将每个元素的值与其他随机选取的元素交换。提供时,函数gen确定在每种情况下都选择哪个元素。否则,该函数将使用一些未指定的随机性来源。
模板

template 
  void random_shuffle (RandomAccessIterator first, 
                       RandomAccessIterator last,
                       RandomNumberGenerator& gen)
{
  iterator_traits::difference_type i, n;
  n = (last-first);
  for (i=n-1; i>0; --i) {
    swap (first[i], first[gen(i+1)]);
  }
}

执行

CPP14

// C++ program to shuffle
// the given array
// using random_shuffle() method
 
#include 
using namespace std;
 
// Shuffle array
void shuffle_array(int arr[], int n)
{
 
    // To obtain a time-based seed
    unsigned seed = 0;
 
    // Shuffling our array using random_shuffle
    random_shuffle(arr, arr + n);
 
    // Printing our array
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
    cout << endl;
}
 
// Driver code
int main()
{
 
    int a[] = { 10, 20, 30, 40 };
 
    int n = sizeof(a) / sizeof(a[0]);
 
    shuffle_array(a, n);
 
    return 0;
}
输出:
10 40 20 30

注意:由于程序中使用随机函数,每次输出可能会有所不同。

哪个更好?

  • C11 ++之后引入的shuffle使用的功能优于random_shuffle使用的rand()。
  • 随机播放是对random_shuffle的改进,我们应该更喜欢使用前者以获得更好的结果。
  • 如果我们没有在random_shuffle中传递随机生成函数,那么它将使用其未指定的随机值,因此所有后续值都将被关联。
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”