1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
class Record{
//int count;
int price;
public:
Record(int price1):price(price1){}
friend bool operator>(const Record &r1, const Record &r2);
friend ostream &operator<<(ostream &os, const Record &r);
};
bool operator >(const Record &r1, const Record &r2)
{
if (r1.price > r2.price)
return true;
else
return false;
}
ostream &operator<<(ostream &os, const Record &r)
{
return os << "(" << r.price << ")" << endl;
}
int cmp(Record &r1, Record &r2)
{
if (r1 > r2)
return 1;
return 0;
}
template<class Iter, class functor>
void sorts(Iter beg, Iter end, functor f)
{
int size = end - beg;
for (int k = 0; k < size -1; k++){
for (int i = 0; i < size -1 -k; i++)
if (f(beg[i], beg[i + 1]))
swap(beg[i],beg[i + 1]);
}
}
int main()
{
vector<Record> rvec;
rvec.push_back(Record(144));
rvec.push_back(Record(45));
rvec.push_back(Record(12));
rvec.push_back(Record(54));
rvec.push_back(Record(90));
rvec.push_back(Record(43));
rvec.push_back(Record(112));
rvec.push_back(Record(1));
for (vector<Record>::iterator iter2 = rvec.begin(); iter2 != rvec.end(); iter2++)
cout << *iter2 << endl;
qsort(rvec, rvec.size(), sizeof(rvec), &cmp);
for (vector<Record>::iterator iter2 = rvec.begin(); iter2 != rvec.end(); iter2++)
cout << *iter2 << endl;
return 0;
}
|