A sort algorithm commonly used in STL

Source: Internet
Author: User

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&LT;INT&GT;:: 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.