STL-Container-run time Specify sorting criteria

Source: Internet
Author: User

Runtimecmp.hpp

#include <Set>using namespacestd;//type for runtime sorting criterionclassruntimecmp { Public:    enumCmp_mode {normal, reverse};Private: Cmp_mode mode; Public:    //constructor for sorting criterion//-default criterion uses value normalRUNTIMECMP (Cmp_mode m =normal): Mode (m) {}//Comparison of elements//-member function for any element typeTemplate <typename t>BOOL operator() (Constt& T1,Constt& T2)Const     {        returnmode = = Normal? t1<T2:t2<T1; }    //Comparison of sorting criteria    BOOL operator== (Construntimecmp& RC)Const     {        returnmode = =Rc.mode; }};//type of a set that uses this sorting criteriontypedefSet<int, runtimecmp> Intset;

#include <iostream>#include<Set>#include<algorithm>#include<iterator>#include<functional>#include"SetTest.h"#include".. /.. /CORE/RUNTIMECMP.HPP"#include".. /.. /CORE/PRINT.HPP"using namespacestd;voidSettest::runtimecompare () {//Create, fill, and print set with normal element order//-Uses default sorting criterionIntset coll1 = {4,7,5,1,6,2,5 }; Print_elements (COLL1,"coll1:"); //create sorting criterion with reverse element orderruntimecmp Reverse_order (runtimecmp::reverse); //Create, fill, and print set with reverse element orderintset coll2 (Reverse_order); Coll2= {4,7,5,1,6,2,5 }; Print_elements (Coll2,"coll2:"); //assign elements and sorting criterionCOLL1 =coll2; Coll1.insert (3); Print_elements (COLL1,"coll1:"); //just to make sure ...    if(Coll1.value_comp () = =Coll2.value_comp ()) {cout<<"Coll1 and Coll2 have the same sorting criterion"<<Endl; }    Else{cout<<"Coll1 and Coll2 have a different sorting criterion"<<Endl; }}voidSettest::run () {Printstart ("Runtimecompare ()");    Runtimecompare (); Printend ("Runtimecompare ()");}

Operation Result:

----------------Runtimecompare (): Run Start----------------
Coll1:1 2 4 5 6 7
Coll2:7 6 5 4 2 1
Coll1:7 6 5 4 3 2 1
Coll1 and Coll2 have the same sorting criterion
----------------Runtimecompare (): Run End----------------

STL-Container-run time Specify sorting criteria

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.