//sortAry.cpp -- by tsaiwn@csie.nctu.edu.tw //use sort( ) in to sort an array of some Object #include #include #include #include using namespace std; class ST { public: ST( ) { name="Unknown"; } ST(string a, int b, double c) { name=a; height=b; wet=c; } bool operator< (const ST& b) const { // Java: int compareTo( cout<<". "; return this->height < b.height; // Ascending on height } string name; int height; double wet; };//ST class God { public: bool operator( ) (const ST& aa, const ST& bb){ return aa.wet > bb.wet; // Descending on wet } };//class God void print(ST x[ ], int n) { // print array of ST, 但故意少印兩個元素 cout <<" "<g++ sortAry.cpp C:\testc\STL>a Original s: abc 168 88.5, bbs 158 75, aaaaa 180 76.5, cat 175 66, hat 173 68 . . . . . . . . . . . . . . . . . . . . . . . after sort(s, s+sizeof s/sizeof s[0]); : bbs 158 75, god 163 60, dog 165 70, abc 168 88.5, hat 173 68 after sort with arben: abc 168 88.5, aaaaa 180 76.5, bbs 158 75, dog 165 70, hat 173 68 after sort with compf: god 163 60, cat 175 66, hat 173 68, dog 165 70, bbs 158 75 *************************/