C++中实现数组的随机排序

// random_shuffle example
#include
#include
#include
#include
#include
#include
using namespace std;


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


// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;


int main () {
  srand ( unsigned ( time (NULL) ) );
  vector myvector;
  vector::iterator it;


  // 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(), p_myrandom);


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


  cout << endl;


  return 0;
}

你可能感兴趣的:(c++,c语言)