STD: how to sort

Source: Internet
Author: User

// <Br/> // alg_std: sort. CPP <br/> // <br/> # include <vector> <br/> # include <algorithm> <br/> # include <functional> // for greater <int> () <br/> # include <iostream> <br/> using namespace STD; </P> <p> // return whether first element is greater than the second <br/> bool udgreater (INT elem1, int elem2) <br/>{< br/> return elem1> elem2; <br/>}</P> <p> class csort <br/>{< br/> public: </P> <p> // return whether fi RST element is less than the second <br/> bool operator () (int A, int B) const <br/>{< br/> return a <B; <br/>}; </P> <p> int main () <br/>{< br/> vector <int> V1; <br/> vector <int >:: iterator iter1; <br/> int arr [12]; <br/> int I; <br/> for (I = 0; I <= 5; I ++) <br/>{< br/> v1.push _ back (2 * I); <br/> arr [I] = 2 * I; <br/>}< br/> for (I = 6; I <= 11; I ++) <br/>{< br/> arr [I] = 23-2 * I; <br/>}</P> <p> in T ii; <br/> for (II = 0; II <= 5; II ++) <br/>{< br/> v1.push _ back (2 * II + 1 ); <br/>}</P> <p> cout <"original vector V1 = ("; <br/> for (iter1 = v1.begin (); iter1! = V1.end (); iter1 ++) <br/> cout <* iter1 <""; <br/> cout <")" <Endl; <br/> cout <"original array arr = ("; <br/> for (INT n = 0; n <12; n ++) <br/> cout <arr [N] <""; <br/> cout <")" <Endl; </P> <p> // STD: Sort default sorting method <br/> sort (v1.begin (), v1.end ()); <br/> cout <"sorted vector V1 = ("; <br/> for (iter1 = v1.begin (); iter1! = V1.end (); iter1 ++) <br/> cout <* iter1 <""; <br/> cout <")" <Endl; </P> <p> // use a user-defined function to sort arrays, the third parameter is the udgreater function pointer <br/> // The second parameter is the last pointer of the largest data address & arr [12]; <br/> sort (& arr [0], & arr [12], udgreater); </P> <p> cout <"resorted (udgreater) array arr = ("; <br/> for (INT n = 0; n <12; n ++) cout <arr [N] <""; <br/> cout <")" <Endl; <br/> // use the overload operator () function of the custom class to sort the array, the third parameter is [Class Name ()] <br/> sort (& arr [0], & arr [12], csort (); </P> <p> cout <"resorted (csort () Arr = ("; <br/> for (INT n = 0; n <12; n ++) cout <arr [N] <""; <br/> cout <") "<Endl; </P> <p> // use greater <int> () in the <functional> library to sort the vector V1; <br/> sort (v1.begin (), v1.end (), greater <int> (); <br/> // sort (v1.begin (), v1.end (), csort (); </P> <p> cout <"resorted (greater) vector V1 = ("; <br/> for (iter1 = v1.begin (); iter1! = V1.end (); iter1 ++) <br/> cout <* iter1 <""; <br/> cout <")" <Endl; </P> <p> // a user-defined (UD) binary predicate can also be used <br/> // sort (v1.begin (), v1.end (), udgreater); <br/> sort (v1.begin (), v1.end (), csort (); <br/> cout <"resorted (udgreater) vector V1 = ("; <br/> for (iter1 = v1.begin (); iter1! = V1.end (); iter1 ++) <br/> cout <* iter1 <""; <br/> cout <")" <Endl; </P> <p> getchar (); <br/> return 0; <br/>}

 

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.