【发布时间】:2017-01-25 19:21:49
【问题描述】:
我有一个程序可以创建一个指向对象的指针数组。指针指向的类存储一个项目的净价,并具有计算该对象的总价的函数。
Product *p[10];
Book *b;
Software *s;
double price;
cout << "Enter the price of the book: " << endl;
cin >> price;
b = new Book(price);
cout << "Enter the price of the software: " << endl;
cin >> price;
s = new Software(price);
cout << "Gross price of book: " << b->getGrossPrice() << endl;
cout << "Gross price of software: " << s->getGrossPrice() << endl;
p[0] = b;
p[1] = s;
有没有办法按照毛价升序对数组进行排序?
我知道 STL 中有一种排序算法,但我认为它对我不起作用,或者至少我不确定如何使它起作用。
网站上有类似问题的解决方案使用排序,但我找不到任何显示它在这种情况下使用。
【问题讨论】:
-
getGrossPrice()是虚拟的吗?那么你可以用一个自定义函子调用 sort 来调用它