Original address: http://blog.csdn.net/factor2000/article/details/3936195
1#include"stdafx.h" 2#include <vector>3#include <algorithm>4#include <functional>5#include <iostream>6 7 using namespacestd;8 9 //Return Whether first element is greater than the secondTen BOOLUdgreater (intELEM1,intelem2) One { A returnElem1 >elem2; - } - classCsort the { - Public: - //Return Whether first element is less than the second - BOOL operator() (intAintbConst + { - returna<b; + }; A }; at - int_tmain (intARGC, _tchar*argv[]) - { -vector<int>v1; -vector<int>:: iterator Iter1; - intarr[ A]; in inti; - for(i =0; i<=5; i++) to { +V1.push_back (2*i); -arr[i]=2*i; the } * for(i =6; i<= One; i++) $ { Panax NotoginsengArr[i] = at-2*i; - } the + intII; A for(ii =0; ii<=5; ii++) the { +V1.push_back (2* II +1); - } $cout <<"Original Vector v1 = ("; $ for(Iter1 = V1.begin (); Iter1! = V1.end (); iter1++) -cout << *iter1<<","; -cout<<")"<<Endl; thecout <<"Original Array arr = ("; - for(intn =0;n< A; n++) Wuyicout << arr[n]<<","; thecout <<")"<<Endl; - Wu //std::sort default sort mode - sort (V1.begin (), V1.end ()); Aboutcout<<"Sorted Vector v1 = ("; $ for(Iter1 = V1.begin (); Iter1! = V1.end (); iter1++) -cout << *iter1 <<","; -cout<<")"<<Endl; - A + //use a custom function to sort the array, and the third argument is the function pointer udgreater the //The second custom parameter is the next pointer to the maximum data address &arr[12]; -Sort (&arr[0],&arr[ A],udgreater); $ thecout<<"resorted (udgreater) array arr = ("; the for(intn =0;n< A; n++) thecout << arr[n]<<","; thecout <<")"<<Endl; - in //Use the <functional> library to sort the vectors v1 greater<int> (); theSort (V1.begin (), V1.end (),greater<int>()); the //sort (V1.begin (), V1.end (). Csort ()); About thecout <<"resorted (greater) vector V1 = ("; the for(Iter1 = V1.begin (); Iter1! = V1.end (); iter1++) thecout << *iter1 <<","; +cout <<")"<<Endl; - the //A user-defined (UD) binary predicate can also be usedBayi //sort (V1.begin (), V1.end (), udgreater); the sort (V1.begin (), V1.end (), Csort ()); thecout <<"resorted (csort) Vector v1 = ("; - for(Iter1 = V1.begin (); iter1!= v1.end (); iter1++) -cout << *iter1 <<","; thecout <<")"<<Endl; the theSystem"Pause"); the //GetChar (); - return 0; the}
Std::sort (Vec.begin (), Vec.end (), Func); error C2780: "Void Std::sort (_ranit,_ranit)": You should enter 2 parameters, but provide 3 solutions: Func is a class member function that is changed to a static function at the class's definition. such as the static bool Func (const type& t1,const type& T2);
Std::sort ()