我有一个程序可以创建一个指向对象的指针数组。指针指向的类存储一个项目的净价格,并具有计算对象总价格的功能。使用C++中的对象函数的结果对对象数组排序
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()'虚拟?那么你可以调用一个自定义函数来调用排序 –
[矢量排序与对象?](http://stackoverflow.com/questions/14081335/algorithm-vector-sort-with-objects) –