Using the STL library sort function to sort vectors, the contents of a vector are pointers to objects, not objects.
The code is as follows
1#include <stdio.h>2#include <vector>3#include <algorithm>4 5 using namespacestd;6 7 classELM8 {9 Public:Ten intM_isortproof; One A Private: - int__m_ivalue; - Static int__m_icnt; the - Public: - Elm (); - intGetValue (intIX); + voidPrintelm (); - }; + A intelm::__m_icnt =0; at - Elm::elm () - { -__M_ICNT + +; -__m_ivalue =__m_icnt; -M_isortproof =GetValue (__m_icnt); in } - to /*(x-10.3) ^2 + 0.6*/ + intElm::getvalue (intIX) - { the floatFX = (float) IX-10.3; * $ floatFY = FX * FX +0.6;Panax Notoginseng - return(int) FY; the } + A voidElm::p Rintelm () the { +printf"value:%3d, proof:%3d\n", __m_ivalue, m_isortproof); - } $ $ /*Z-A*/ - BOOLCompareConstELM * A,ConstELM *b) - { the returnA->m_isortproof > B->M_isortproof; - }Wuyi the intMainintargcChar*argv[]) - { WuVector<elm *>vecplist; - inti =0; About for(i =0; I < -; i++) $ { -ELM * Pelm =NewElm; - Vecplist.push_back (Pelm); - } A for(Vector<elm *>::iterator PE = vecplist.begin (); PE! = Vecplist.end (); pe++) + { the(*PE)Printelm (); - } $ /*sort vectors by using sort*/ the sort (Vecplist.begin (), vecplist.end (), compare); theprintf"\033[0;34m----------------sorted----------------\033[0m\n"); the for(Vector<elm *>::iterator PE = vecplist.begin (); PE! = Vecplist.end (); pe++) the { -(*PE)Printelm (); in } the the return 0; About}
The operation results are as follows
1. Before sorting
Value:1, Proof: theValue:2, Proof: theValue:3, Proof: -Value:4, Proof: +Value:5, Proof: -Value:6, Proof: +Value:7, Proof: OneValue:8, Proof:5Value:9, Proof:2Value:Ten, Proof:0Value: One, Proof:1Value: A, Proof:3Value: -, Proof:7Value: -, Proof: -Value: the, Proof: AValue: -, Proof: -Value: -, Proof: $Value: -, Proof: -Value: +, Proof: theValue: -, Proof:94
After sorting
Value: -, Proof:94Value:1, Proof: theValue: +, Proof: theValue:2, Proof: theValue: -, Proof: -Value:3, Proof: -Value: -, Proof: $Value:4, Proof: +Value: -, Proof: -Value:5, Proof: -Value: the, Proof: AValue:6, Proof: +Value: -, Proof: -Value:7, Proof: OneValue: -, Proof:7Value:8, Proof:5Value: A, Proof:3Value:9, Proof:2Value: One, Proof:1Value:Ten, Proof:0
Sort vectors by using the STL library sort function