Frequency method for sorting class according to members of the class

zhaozj2021-02-16  55

// Recently, I have encountered such a problem: // There are more than a dozen member data in a class, I need to provide different sorting methods according to different members. As follows:

Class Cell {public: string name; string time; int tchdrop; double traffic; // more ...};

// Recently, STL is being learned, so I want to combine the knowledge you have learned, design a general method, just mention // for the members of the members and sorting criteria (ie, the function object), can // with the STL algorithm Match, sorted, compare, compare, etc.

Template > Class Comp_mem_Data: Public Binary_Function {Public: Comp_Mem_Data (T (C: * PMD), Pred PRED = PRED ()) / / PRED class uses the default constructor and does not necessarily apply. pay attention. : M_PMD (PMD), M_PRED (PRED) {};

/ / Compare the member BOOL Operator () (Const C & A, Const C & B) const {return m_pred (a. * M_pmd, b. * M_pmd);};

Private: t (c :: * m_pmd); // class member pointer PRED M_PRED; / / comparison function};

/ / 照 照 照 照 照 形 形 形 形 形 形 形 形 形 形RETURN COMP_MEM_DATA > (PMD, Less ());} Template Comp_mem_Data Comper_Mem_Data (T (C :: * PMD), PRED PRED {RETURN COMP_MEM_DATA (PMD, PRED);

// The following can be started .// sort (vcell.begin (), vcell.end (), comper_mem_data (& cell :: name)); // sort (vCell.begin (), vcell.end (), COMPER_MEM_DATA (& Cell :: Traffic, Greater ()); //, etc.

// The following is the test program is passed in BCB5.5, MSVC6.0.

#include #include #include #include #include #include #include #include using namespace std;

Inline Ostream & Operator << (Ostream & Out, Const Cell & Cell) {OUT << Cell.Name << "| << Cell.Time <<" | << Cell.tchdrop << "| << Cell.Traft << "|" // more ... << end1; return out;} template void output (container cont) {cout << "Cell Name | TIME | TCHDROP | Traffic" << Endl; for (int J = 0; j

INT main () {Vector vcell; cell cl; srand (NULL)); for (int i = 0; i <10; i) {cl.name = static_cast i 'a'); cl.time = static_cast (RAND ()% 10 '1'); cl.tchdrop = rand ()% 100; cl.traft = rand () * 100.0 / rand_max; vCell. PUSH_BACK (CL);

Sort (vcell.begin (), vcell.end (), comper_mem_data (& cell :: tchdrop); Output (vCell); sort (vcell.begin (), vcell.end (), Comper_Mem_Data (& Cell :: Traffic, Greater ())); OUTPUT (VCELL);

Vector :: const_iterator it = find_if (vcell.begin (), vcell.end (), bind2nd (COMPER_MEM_DATA (& Cell :: Name, Equal_to ()), CL)); COUT << ((iter) ! = vcell.end ())? ("CL IS Find"): ("CL ISN't Find")) << ENDL;

CHAR LC; CIN >> LC; RETURN 0;}

The output of the program is as follows: / * Cell Name | TIME | TCHDROP | TrafficA | 3 | 2 | 47.0168 | C | 1 | 2 | 66.5151 | D | 5 | 19 | 86.5017 | F | 9 | 23 | 11.1087 | | 5 | 78 | 75.7897 | G | 7 | 84 | 54.2253 | H | 7 | 88 | 61.1774 | I | 1 | 90 | 59.2578 | J | 3 | 94 | 89.9625 | B | 6 | 99 | 89.2514 | Cell Name | TIME | TCHDROP | TrafficJ | 3 | 94 | 89.9625 | B | 6 | 99 | 89.2514 | D | 5 | 19 | 86.5017 | E | 5 | 78 | 75.7897 | C | 1 | 2 | 66.5151 | H | 7 | 88 | 61.1774 | I | 1 | 90 | 59.2578 | G | 7 | 84 | 54.2253 | A | 54.2253 | A | 3 | 2 | 47.0168 | F | 9 | 23 | 11.1087 | Cl IS Find // /// Use the same method, Produces generic classes that compare two class member functions return values, // class data members are compared with the same type of constant parameters. // This is not listed here.

// The above improper, please also ask prawns to advice.

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

New Post(0)