C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ random_shuffle()

C++算法random_shuffle()

C++算法 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);

参数

first: 一个随机访问迭代器,它指向要重排范围内第一个元素的位置。
last: 一个随机访问迭代器,该位置指向要重排范围内最后一个元素之后的位置。
result: 一个指向该位置的输出迭代器第一个元素在目标范围内的位置。
gen : 称为随机数生成器的特殊功能对象。

返回值

复杂度

复杂度在[first,last)范围内是线性的: 获取随机值并交换元素。

数据竞争

[first,last)范围内的对象被修改。
result和the范围之间的对象返回的值已更改。

异常

如果元素交换的任何随机数生成或迭代器上的操作抛出异常,则此函数都会引发异常。
请注意,无效参数会导致未定义的行为。

示例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

示例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  

示例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

示例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

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4