// random_shuffle example #include <iostream> // cout #include <algorithm> // random_shuffle #include <vector> // vector #include <ctime> // time #include <cstdlib> // rand, 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() ); // print out content: cout << "using built-in random generator , myvector contains:"; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; // using myrandom: random_shuffle ( myvector.begin(), myvector.end(), myrandom); // print out content: cout << " using myrandom , myvector contains:"; for (vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it) cout << ' ' << *it; cout << '\n'; return 0; }
输出
using built-in random generator , myvector contains: 1 4 8 5 9 7 2 3 6 using myrandom , myvector contains: 3 7 8 9 4 2 5 1 6
原文链接: https://www.cnblogs.com/sea-stream/p/10891797.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/295397
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!