//---------------------------------------------------------------------------#include<vcl.h>#pragmaHdrstop#include"Unit1.h"#include<vector>#include<algorithm>using namespacestd;//---------------------------------------------------------------------------#pragmaPackage (Smart_init)#pragmaResource "*.DFM"TForm1*Form1;classca{ Public: intA; CA (inta) { This->a =A; } BOOL operator== (intt) {returnA = =T; } //cannot compile//friend bool operator = = (const ca* ca,int t);};//cannot compile/*bool operator = = (const ca* Ca,int t) {return ca->a = = t;}*/BOOLCompare (ca* F, ca*s) { returnF->a < s->A;} Template<classT1,classT2>Vector<t1*>::iterator Find (vector<t1*>:: Iterator begin, Vector<T1*>:: Iterator End,Constt2&var){ while(Begin! =end) { if(* (*begin) = =var) { returnbegin; } Begin++; }}template<classT1,classT2>Vector<T1*> Findrange (vector<t1*>:: Iterator begin, Vector<T1*>:: Iterator End,Constt2&var) {vector<T1*>result; while(Begin! =end) { if(* (*begin) = =var) {Result.push_back (*begin); } Begin++; } returnresult;}/** * use find_if*/Static intGloba =1;BOOLPrec (ca*CA) { if(Globa = = ca->a)return true; Else return false;} Vector<CA*>Arry;//---------------------------------------------------------------------------__fastcall Tform1::tform1 (tcomponent*owner): Tform (owner) {arry.push_back (NewDAO2)); Arry.push_back (NewDAO +)); Arry.push_back (NewDAO3)); Arry.push_back (NewDAO1)); Arry.push_back (NewDAO4)); Arry.push_back (NewDAO1)); Arry.push_back (NewDAO A));}//---------------------------------------------------------------------------void__fastcall Tform1::button1click (TObject *Sender) {Sort (Arry.begin (), Arry.end (), Compare); for(Unsigned i =0; I < arry.size (); i++) {showmessage (IntToStr (arry[i)-a)); } Vector<ca*>::iterator itor = Find (Arry.begin (), Arry.end (), +); if(Itor! =Arry.end ()) {ShowMessage ("find!"); } Vector<CA*> range = Findrange<ca,int> (Arry.begin (), Arry.end (),1); ShowMessage ("Find"+inttostr (Range.size ()) +" Number"); Globa=1; Vector<ca*>::iterator Itor2 =find_if (Arry.begin (), Arry.end (), PREC); if(Itor2! =Arry.end ()) {ShowMessage ("find!"); }}//---------------------------------------------------------------------------void__fastcall Tform1::formdestroy (TObject *Sender) { for(Unsigned i =0; I < arry.size (); i++) { DeleteArry[i]; Arry[i]=NULL; } arry.clear ();}//---------------------------------------------------------------------------
The above code uses a template function to find an array of vector<t*> classes.
Find comparisons in the standard library are in the following ways:
Template <class_randomaccessiter,class_tp>_stlp_inline_loop _randomaccessiter __find (_randomaccessiter __first, _randomaccessiter __last, Const_tp&__val,ConstRandom_access_iterator_tag &) {_stlp_difference_type (_randomaccessiter) __trip_count= (__last-__first) >>2; for(; __trip_count >0; --__trip_count) { if(*__first = = __val)return__first;//dereference and compare++__first; if(*__first = = __val)return__first; ++__first; if(*__first = = __val)return__first; ++__first; if(*__first = = __val)return__first; ++__first; } Switch(__last-__first) { Case 3: if(*__first = = __val)return__first; ++__first; Case 2: if(*__first = = __val)return__first; ++__first; Case 1: if(*__first = = __val)return__first; ++__first; Case 0: default: return__last; }}
As you can see, the implementation of the standard library is *__first = = __val, which requires the implementation of the = = operator.
However, because the elements inside the vector<t*> are pointers, it is not possible to satisfy the = = operator comparison anyway.
As for, why does the vector contain pointers? This is related to operational efficiency and low pointer copy costs.
So if vector<t*> such an array, basically consider 2 ways to find
Implement a Find generic method by itself, but it still requires the implementation of operator overloading.
template<class T1,class t2>vector<t1*>::iterator Find (vector<t1*> :: Iterator begin, vector<T1*>:: Iterator end, constvar) { while (Begin! = End) { ifvar) { return begin; } Begin+ +; }}
or using find_if
Template <class_randomaccessiter,class_predicate>_stlp_inline_loop _randomaccessiter __find_if (_randomaccessiter __first, _randomaccessiter __last, _predicate __pred,ConstRandom_access_iterator_tag &) {_stlp_difference_type (_randomaccessiter) __trip_count= (__last-__first) >>2; for(; __trip_count >0; --__trip_count) { if(__pred (*__first))return__first; ++__first; if(__pred (*__first))return__first; ++__first; if(__pred (*__first))return__first; ++__first; if(__pred (*__first))return__first; ++__first; } Switch(__last-__first) { Case 3: if(__pred (*__first))return__first; ++__first; Case 2: if(__pred (*__first))return__first; ++__first; Case 1: if(__pred (*__first))return__first; //++__first; I don't know why there is such a comment here, the code in C + + BUILDER 6 is like this. As can be seen, the standard library is also written by people. Case 0: default: return__last; }}
As can be seen from the implementation of Find_if, his idea is to pass in a class or function, substituting the element as a parameter, and if the function returns True, the condition is satisfied.
Therefore, a global variable is used (you should actually consider using class members, as appropriate).
/* */ static int Globa = 1 ; bool prec (ca* CA) { if (Globa = = Ca->a) return
true
;
else
return
false
;}
//When used
Globa = 1;
Vector<ca*>::iterator Itor2 = find_if (Arry.begin (), Arry.end (), PREC);
Comes with an example of sort ordering (this is the same way as in C#,java, except that C # and Java require an interface that implements Icompare, which is a function pointer that essentially passes "behavior")
In terms of element traversal, lookups, sorting, C # and Java are much more elegant. Maybe c++11 solved the problem and did not study it.
Finding and sorting vector<t*> arrays in C + +