65,211
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <functional>
using namespace std;
int iArray[10] = {10, 2, 2, 14, 5, 6, 74, 29, 9, 101};
vector<int> v(iArray, iArray + 10);
void Display(vector<int> &v, const char* s)
{
cout<<s<<endl;
copy(v.begin(), v.end(), ostream_iterator<int>(cout, "\t"));
cout<<endl;
}
unsigned int RandInt(const unsigned int n)
{
return rand() % n;
}
int main()
{
srand((unsigned int)time(NULL));
Display(v, "Before shuffle:");
pointer_to_unary_function<unsigned int, unsigned int> ptr_RandInt = ptr_fun(RandInt);
random_shuffle(v.begin(), v.end(), ptr_RandInt);
Display(v, "After shuffle:");
return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
const int POKER_NUM = 52; //52张扑克牌
void print_poker(int PokerNum)
{
cout << PokerNum << " ";
}
class MyRand
{
public:
int operator()(int index)
{
return rand() % POKER_NUM;
}
};
int main()
{
srand( (unsigned)time(NULL) ); //设置随即数生成器的种子
vector<int> poker; //一副牌,牌点数从 1 计
//初始化
for (int num = 0; num < POKER_NUM; ++num)
{
poker.push_back(num+1);
}
//用默认随机数洗一遍
random_shuffle(poker.begin(), poker.end());
for_each(poker.begin(), poker.end(), print_poker);
cout << endl;
//用自定义随机数再洗一遍
random_shuffle(poker.begin(), poker.end(), MyRand());
copy(poker.begin(), poker.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
const int POKER_NUM = 52; //52张扑克牌
void print_poker(int PokerNum)
{
cout << PokerNum << " ";
}
class MyRand
{
public:
int operator()(int index)
{
return rand() % POKER_NUM;
}
};
int main()
{
srand( (unsigned)time(NULL) ); //设置随即数生成器的种子
vector<int> poker; //一副牌,牌点数从 1 计
//初始化
for (int num = 0; num < POKER_NUM; ++num)
{
poker.push_back(num+1);
}
//用默认随机数洗一遍
random_shuffle(poker.begin(), poker.end());
for_each(poker.begin(), poker.end(), print_poker);
cout << endl;
//用自定义随机数再洗一遍
random_shuffle(poker.begin(), poker.end(), MyRand());
copy(poker.begin(), poker.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}