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