Stl_ algorithm _ Sort all elements (sort, stable_sort)

Source: Internet
Author: User

C + + Primer learning.

?

A simple record of my learning process? (Code-based)


Most containers apply and do not apply to the list container
Sort (b,e)
Sort (b,e,p)
Stable_sort (B,e)
Stable_sort (B,E,P)


/**------http://blog.csdn.net/u010579068------**/#include <iostream> #include <cstdio> #include < string> #include <vector> #include <list> #include <deque> #include <algorithm>using Namespace std;/*****************************************//Most containers are applicable and do not apply to the list container sort (b,e) sort (b,e,p) Stable_sort (b,e ) Stable_sort (b,e,p) *****************************************//**---------------------------------------------- ------------------------------------Note: Not available for list container, List has member function sort ()--------------------------------------------- -------------------------------------**//********************************************************************** Std::sort all sort containers for algorithm--------------------------------- -----------------------------------------------------template <class randomaccessiterator> void sort ( Randomaccessiterator First, Randomaccessiterator last), Template <class Randomaccessiterator, class compare> void sort (randomaccessiterator first, Randomaccessiterator last, Compare comp);//eg:******************************** *****************************************************//******************************************************** Std::stable_sort all sort containers for algorithm---------------- ----------------------------------------------------------------------Template <class Randomaccessiterator  > void Stable_sort (randomaccessiterator first, Randomaccessiterator last), template <class Randomaccessiterator, Class compare> void Stable_sort (randomaccessiterator first, Randomaccessiterator last, Compare comp);//eg:*************************************************************************************/bool MyFunction (int i,int j) {return (I&LT;J);}    struct myclass{bool operator () (int i,int j) {return (I&LT;J); }} Myobject;bool compare_as_ints (double i,double j) {return (int (i) <int (j));    int main () {int myints[] = {32,71,12,45,26,80,53,33};               Vector<int> Myvector (myints, myints+8);    The vector<int>::iterator it;           Using default comparison (operator <): sort (Myvector.begin (), Myvector.begin () +4); (+4) +//using function as Comp sort (Myvector.begin (), Myvector.end (), myfunction);     (+ +)//using object as Comp sort (Myvector.begin (), Myvector.end (), MyObject);    (from £ º)//print out Content:cout << "myvector contains:";    For (It=myvector.begin (); It!=myvector.end (); ++it) cout << "" << *it;    cout << Endl; /**---------------------------------------------------------------------------------------**/Double mydoubles[]    = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};    Deque<double> Mydeque;    Deque<double>::iterator ID; Mydeque.assiGN (MYDOUBLES,MYDOUBLES+8);    cout << "Using default comparison:";    Stable_sort (Mydeque.begin (), Mydeque.end ());    For (Id=mydeque.begin (); Id!=mydeque.end (); ++id) cout << "" << *id;    Mydeque.assign (MYDOUBLES,MYDOUBLES+8);    cout << "\nusing ' compare_as_ints ':";    Stable_sort (Mydeque.begin (), Mydeque.end (), compare_as_ints);    For (Id=mydeque.begin (); Id!=mydeque.end (); ++id) cout << "" << *id;    cout << Endl; return 0;}


Myvector contains:12 80using default comparison:1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67using ' compare_ As_ints ': 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67



Stl_ algorithm _ Sort all elements (sort, stable_sort)

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.