see the following example.
Ptrdiff_t myrandom (ptrdiff_t I) {return rand () % I ;}< br/> ptrdiff_t (* p_myrandom) (ptrdiff_t) = myrandom; // a function pointer is defined here, method function pointing to random number generation <br/> template <typename T> void vrand (vector <t> & V, ptrdiff_t (* p_myrandom) (ptrdiff_t )) <br/> // define a template function, which can change an ordered sequence to a random sequence <br/> {<br/> If (p_myrandom = NULL) <br/>{< br/> random_shuffle (v. begin (), V. end (); // call the template function in STL to generate a random sequence <br/>}< br/> else <br/>{< br/> random_shuffle (v. B Egin (), V. end (), p_myrandom); // you can call the template function in STL to generate a random sequence with one more parameter, method to generate a random number <br/>}</P> <p> int vrandtest () <br/>{< br/> vector <char> VI; <br/> vector <char >:: iterator VP; <br/> srand (unsigned (Time (null); // This is required because it generates a random seed and the subsequent random seed will change. Otherwise, the random sequence generated each time is the same <br/> for (INT I = 0; I <10; I ++) vi. push_back (I + 'A'); <br/> for (Vp = VI. begin (); VP! = VI. end (); ++ vp) <br/> cout <* VP <""; <br/> cout <Endl; <br/> vrand <char> (Vi, myrandom); // call the template function to generate a random sequence. <br/> for (Vp = VI. begin (); VP! = VI. End (); ++ vp) <br/> cout <* VP <""; <br/> cout <Endl; <br/>}
Because templates are used in the preceding example, you can perform random sorting for various types.
The above example shows C ++. By using STL, we can simply implement random sequences, reflecting the powerful STL.