Type traits instance, improve efficiency

zhaozj2021-02-16  48

#include "iostream" using namespace std;

Class SimpleComprait {}; class complexcomparetrait {};

Template class complexcompare {public: typedef t valueetype; typef complexCompareTrait CompareTrait;

Template Class SimpleCompare {public: typedef t valueetype; typedef simpleComprait CompareTrait;

template Iterator __LowerBound (Iterator first, Iterator last, T val, Compare comp, ComplexCompareTrait) {size_t len ​​= 0; size_t half; Iterator middle; len = last - first; while (len> 0) {HALF = LEN >> 1; middle = first; middle = HALF; IF (Comp (* middle, val) <0) {first = middle; first; len = len - half - 1;} else Len = HALF;} Return First;

template Iterator __LowerBound (Iterator first, Iterator last, T val, Compare comp, SimpleCompareTrait) {size_t len ​​= 0; size_t half; Iterator middle; len = last - first; while (len> 0) {HALF = LEN >> 1; middle = first; middle = HALF; IF (CoMP (* middle, val)) {first = middle; first; len = len - half - 1;} else len = Half;} Return First;

Template Iterator LowerBound (Iterator First, T Val, Compare Comp) {Return __LowerBound (First, Last, Val, Comp, Compare :: Comparet ();

template Iterator __BinSearch (Iterator first, Iterator last, T val, Compare comp, ComplexCompareTrait) {size_t len ​​= 0; size_t half; Iterator middle; len = last - first; while (len> 0) {HALF = LEN >> 1; middle = first; middle = HALF; INT iComp = Comp (* middle, val); if (iComp <0) {first = middle; first; len = le - HALF - 1;} else if (iComp> 0) {len = HALF;} Else Return middle;} return last;} template Iterator __binsearch (Iterator First, Iterator Last, T Val, Compare Comp, SimpleComprait) {size_t len ​​= 0; size_t hald; item middle; len = last - first; while (len> 0) {half = len >> 1; middle = first; middle = half; if (Comp (* Middle, val)) {first = middle; first; len = len - half - 1;} else if (value) {len = half;} else return middle;} Return Last;}

Template Iterator BINSearch (Iterator First, Iterator Last, T Val, Compare Comp) {Return__binsearch (First, Last, Val, Comp, Compare :: Comparet ();}

template class TheSimpleCompare: public SimpleCompare {public: bool operator () (const T & lv, const T & rv) const {return lv class TheComplexCompare: public ComplexCompare < T> {public: int operator () (const T & lv, const t & rv) const {return lv - rv;}}

typedef TheSimpleCompare SimpleIntCompare; typedef TheComplexCompare ComplexIntCompare; / * class SimpleIntCompare: public SimpleCompare {public: bool operator () (int lv, int rv) const {return lv {public: int operator () (int LV, int RV) const {return lv - rv;}}; * / # define Dimof (x) (sizeof (x) / sizeof (x [0] ))

INT Main (int Argc, char * argv []) {Int Val = 8; int Tab [] = {1, 2, 3, 4, 5, 6, 7, 9, 10, 1000};

INT * SLOW = LowerBound (Tab, Tab Dimof (Tab) - 1, Val, SimpleintCompare ()); INT * CLOW = LowerBound (Tab, Tab Dimof (Tab) - 1, Val, ComplexintCompare ());

INT * S = BINSEARCH (Tab, Tab Dimof (Tab) - 1, Val, SimpleintCompare ()); INT * C = Binsearch (Tab, Tab Dimof (Tab) - 1, Val, ComplexintCompare ());

COUT << "* SLOW =" << * Slow << ", * CLOW =" << * Clow << Endl; cout << "* s =" << * s << ", * c = << * c << Endl << endl;

Return 0;}

转载请注明原文地址:https://www.9cbs.com/read-28316.html

New Post(0)