在 C++ 中随机化向量
    
    Jinku Hu
    2023年10月12日
    
    C++
    C++ Vector
    
 
本文将演示关于如何在 C++ 中对向量元素进行随机化的多种方法。
使用 shuffle 算法来洗牌向量元素
    
std::shuffle 是 C++ <algorithm> 库的一部分,实现了随机排列功能,可以应用于给定范围内的元素。该函数将范围迭代器作为前两个参数,随机数发生器作为第三个参数。随机数生成器是一个函数对象。当代 C++ 推荐使用随机数生成器的标准库实用程序。应该使用 std::random_device 来产生不确定的数字。
最后,必须创建所选的随机数引擎对象,并将其传递给 shuffle 算法,以生成范围内的随机排列。请注意,我们在随机化完成之前和之后打印一个整数向量。
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::shuffle;
using std::string;
using std::vector;
template <typename T>
void printVectorElements(vector<T> &vec) {
  for (auto i = 0; i < vec.size(); ++i) {
    cout << vec.at(i) << "; ";
  }
  cout << endl;
}
int main() {
  vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
  cout << "i_vec1           : ";
  printVectorElements(i_vec1);
  std::random_device rd;
  std::default_random_engine rng(rd());
  shuffle(i_vec1.begin(), i_vec1.end(), rng);
  cout << "i_vec1 (shuffled): ";
  printVectorElements(i_vec1);
  cout << endl;
  return EXIT_SUCCESS;
}
输出:
i_vec1           : 12; 32; 43; 53; 23; 65; 84;
i_vec1 (shuffled): 53; 32; 84; 23; 12; 43; 65;
作为前一种方法的替代,我们可以使用 std::begin 和 std::end 对象实现相同的子程序,将范围迭代器传递给 shuffle 函数。下面的例子可以是一个更通用的版本,用于特定的编码方案。
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::shuffle;
using std::string;
using std::vector;
template <typename T>
void printVectorElements(vector<T> &vec) {
  for (auto i = 0; i < vec.size(); ++i) {
    cout << vec.at(i) << "; ";
  }
  cout << endl;
}
int main() {
  vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
  cout << "i_vec1           : ";
  printVectorElements(i_vec1);
  std::random_device rd;
  std::default_random_engine rng(rd());
  shuffle(std::begin(i_vec1), std::end(i_vec1), rng);
  cout << "i_vec1 (shuffled): ";
  printVectorElements(i_vec1);
  cout << endl;
  return EXIT_SUCCESS;
}
输出:
i_vec1           : 12; 32; 43; 53; 23; 65; 84;
i_vec1 (shuffled): 43; 23; 32; 65; 53; 12; 84;
使用 random_shuffle 算法对向量元素进行随机化
std::random_shuffle 是 C++ 标准库中的另一个实用算法。旧版本的 std::shuffle 已经被最新的 C++ 标准舍弃了。虽然它仍可以在旧版 C++ 的编码环境中使用。
random_shuffle 可以采取用户提供的随机数生成器,但由于旧版本的 C++ 缺乏随机库设施,人们可能只向函数提供范围迭代器。在后一种情况下,random_shuffle 利用实现中定义的随机数生成器,有时正好是 std::rand 函数调用。
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::shuffle;
using std::string;
using std::vector;
template <typename T>
void printVectorElements(vector<T> &vec) {
  for (auto i = 0; i < vec.size(); ++i) {
    cout << vec.at(i) << "; ";
  }
  cout << endl;
}
int main() {
  vector<int> i_vec1 = {12, 32, 43, 53, 23, 65, 84};
  cout << "i_vec1           : ";
  printVectorElements(i_vec1);
  std::random_shuffle(i_vec1.begin(), i_vec1.end());
  cout << "i_vec1 (shuffled): ";
  printVectorElements(i_vec1);
  cout << endl;
  return EXIT_SUCCESS;
}
输出:
i_vec1           : 12; 32; 43; 53; 23; 65; 84;
i_vec1 (shuffled): 23; 53; 32; 84; 12; 65; 43;
        Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
    
作者: Jinku Hu
    
