C++ 算法 中的 random_shuffle函数

首页 / C++入门教程 / C++ 算法 中的 random_shuffle函数

C++算法 random_shuffle()通过将参数中的元素放在随机位置来对它们进行重新排序。

第一个版本使用内部随机数生成器,第二个版本使用随机数生成器,这是一种特殊的函数对象,明确地将其作为参数传递。

random_shuffle - 语法

generator by default (1)	

template <class RandomAccessIterator>
                 void random_shuffle (RandomAccessIterator first, RandomAccessIterator last);

specific generator (2)
	
template <class RandomAccessIterator, class RandomNumberGenerator>
  void random_shuffle (RandomAccessIterator first, RandomAccessIterator last,
                       RandomNumberGenerator& gen);

random_shuffle - 参数

firts:一个随机访问迭代器,指向要重新排列的参数内第一个元素的位置。

last:一个随机访问迭代器,该位置指向要重排参数中最后一个元素之后的位置。

result:一个输出迭代器,指向目标参数中第一个元素的位置。

gen :一种特殊函数对象,称为随机数生成器。

random_shuffle - 返回值

没有

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-algorithm-random-shuffle-function.html

来源:LearnFk无涯教程网

random_shuffle - 例子1

让我们看一个简单的示例来演示random_shuffle()的用法:

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

using namespace std;

int main() {
  vector<int> v(10); 
  iota(v.begin(), v.end(), 0);  //generating a value from 0-9

  cout << "before: ";
  copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  cout << endl;

  random_shuffle(v.begin(), v.end());

  cout << " after: ";
  copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
  cout << endl;
  
  return 0;
}

输出:

before: 0 1 2 3 4 5 6 7 8 9 
after: 4 3 7 8 0 5 2 1 6 9

random_shuffle - 例子2

让我们看另一个简单的例子:

#include <iostream>
#include <vector>
#include <algorithm>
 
using namespace std;
 
int main()
{
  int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  vector<int> v(a, a+10);
 
  cout <<"Here are the values in the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  cout << "\n\nNow we randomize the order of the values.";
  random_shuffle(v.begin(), v.end());
 
  cout <<"\n\nHere are the revised contents of the vector:\n";
  for (vector<int>::size_type i=0; i<v.size(); i++)
    cout <<v.at(i)<<" ";
 
  return 0;
}

输出:

Here are the values in the vector:
1 2 3 4 5 6 7 8 9 10 

Now we randomize the order of the values.

Here are the revised contents of the vector:
5 4 8 9 1 6 3 2 7 10  

random_shuffle - 例子3

让我们看另一个简单的例子:

#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
using namespace std;
 
void print(vector <string> vs)
{   
    vector <string>::iterator i;
    for(i = vs.begin(); i != vs.end(); i++)
    {
        cout << setw(2) << *i << "  ";
    }
    cout << endl;
}
 
int main()
{
    string s[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    vector <string> vs(s, s + 13);
 
    cout << "Original order : ";
    print(vs);
    cout << "Shuffling cards in uniformly random order ... "
         << endl;
    random_shuffle(vs.begin(), vs.end());
    cout << "Pick any three cards ... " << endl;
    cout << "You have got   : ";
    cout << vs.back() << ", ";
    vs.pop_back();
    cout << vs.back() << ", ";
    vs.pop_back();
    cout << vs.back() << endl;
    vs.pop_back();
    
    return 0;
}

输出:

Original order :  A   2   3   4   5   6   7   8   9  10   J   Q   K  
Shuffling cards in uniformly random order ... 
Pick any three cards ... 
You have got   : 9, 8, 4

random_shuffle - 例子4

让我们看另一个简单的例子:

#include <iostream>    //std::cout
#include <algorithm>   //std::random_shuffle
#include <vector>      //std::vector
#include <ctime>       //std::time
#include <cstdlib>     //std::rand, std::srand

using namespace std;

// random generator function:
int myrandom (int i) { return rand()%i;}

int main () {
  srand ( unsigned ( time(0) ) );
  vector<int> myvector;

 //set some values:
  for (int i=1; i<10; ++i) myvector.push_back(i);//1 2 3 4 5 6 7 8 9

 //using built-in random generator:
  random_shuffle ( myvector.begin(), myvector.end() );

 //using myrandom:
  random_shuffle ( myvector.begin(), myvector.end(), myrandom);

 //print out content:
  cout << "myvector contains:";
  for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    cout << ' ' << *it;

  cout << '\n';

  return 0;
}

输出:

myvector contains: 9 7 5 6 3 4 2 8 1

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

Vue开发实战 -〔唐金州〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

编辑训练营 -〔总编室〕

DevOps实战笔记 -〔石雪峰〕

讲好故事 -〔涵柏〕

李智慧 · 高并发架构实战课 -〔李智慧〕

技术领导力实战笔记 2022 -〔TGO 鲲鹏会〕

AI大模型系统实战 -〔Tyler〕

好记忆不如烂笔头。留下您的足迹吧 :)