Tag:pac sorting algorithm random tor namespace space ++ rand compare
#include "iostream" #include "vector" using namespace std; #include "string" #include "algorithm" void Main_mergr () {vector <int > V1;v1.push_back (1); V1.push_back (2); V1.push_back (3); Vector<int > V2;v2.push_back (1); v2.push_back (6); V2.push_back (8); Vector<int > V3;v3.resize (v1.size () + v2.size ());//This step is quite important.//merge two ordered sequences and store them in another sequence. Unordered is an error. Merge (V1.begin (), V1.end (), V2.begin (), V2.end (), V3.begin ()); for (Vector<int>::iterator it = V3.begin (); it! = v3. End (); it++) {cout << *it << "";}} Class Teacher{public:teacher (int age, String name) {m_age = Age;m_name = name;} void Printt () {cout << "name:" << m_name << "m_age" << m_age << Endl;} Protected:public:int m_age;string m_name;}; BOOL Compare (Teacher & Tem1, Teacher &tem2) {return (Tem1.m_age > Tem2.m_age);} void Main_sort () {Teacher T1 (1, "DSD"), T2 (3, "DSFSD"), T3 ("DFSDD");vector<teacher> Tem;tem.push_Back (T1); Tem.push_back (T2); Tem.push_back (T3);//rearrange elements in the specified range in ascending order by default. To change the collation, you can enter a comparison function. Sort (Tem.begin (), Tem.end (), Compare); for (Vector<teacher>::iterator it = Tem.begin (); It! = Tem.end (); it++) {it-& Gt;printt ();}} void Main_random_shuffle () {vector<int > V1;v1.push_back (1) ; V1.push_back (9); V1.push_back (3), Random_shuffle (V1.begin (), V1.end ()),//random order of elements within the specified range for (VECTOR<INT>:: Iterator it = V1.begin (); It! = V1.end (); it++) {cout << *it << "";} cout << Endl;reverse (V1.begin (), V1.end ());//Make sequence reverse for (vector<int>::iterator it = V1.begin (); It! = V1.end () ; it++) {cout << *it << "";}} int main () {main_mergr (); cout << endl;main_sort (); Main_random_shuffle (); System ("Pause");}
A sort algorithm commonly used in STL