[C++]按类成员的值对对象进行排序

[C++]Sorting objects by class member's value

本文关键字:对象 排序 C++ 成员      更新时间:2023-10-16

在下面显示的代码中,在函数void printExpensiveThanT(..)中,我应该打印出比函数中的报价T更贵的报价的目的地、距离和价格,按距离值升序排序。我不确定应该用什么来排序,我用向量做了一些实验,但没有成功,所以我删除了它。如有任何帮助,我们将不胜感激。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
class Transport {
protected:
    char destination[100];
    int basePrice;
    int distance;
public:
    Transport() {}
    Transport(char *destination, int basePrice, int distance) {
        strcpy(this->destination, destination);
        this->basePrice = basePrice;
        this->distance = distance;
    }
     virtual ~Transport() {}
    virtual int priceTransport() = 0;
    friend bool operator<(const Transport &t1, const Transport &t2) {
        return t1.distance<t2.distance;
    }
    int getDistance(){ return distance; }
    char *getDestination() { return destination; }
    int getPrice() { return basePrice; }
};
class AutomobileTransport : public Transport {
private:
    bool ifDriver;
public:
    AutomobileTransport() {}
    AutomobileTransport(char *destination, int basePrice,int distance, bool ifDriver) : Transport(destination,basePrice,distance) {
        this->ifDriver = ifDriver;
    }
    void setIfDriver(bool ifDriver) {
        this->ifDriver = ifDriver;
    }
    bool getIfDriver() {
        return ifDriver;
    }
    int priceTransport() {
        if(ifDriver) {
            basePrice+=basePrice*20/100;
        }
        return basePrice;
    }
    friend bool operator<(const AutomobileTransport &a1, const AutomobileTransport &a2) {
        return a1.distance<a2.distance;
    }
};
class VanTransport: public Transport {
private:
    int passengers;
public:
    VanTransport() {}
    VanTransport(char *destination, int basePrice, int distance, int passengers) : Transport(destination, basePrice, distance) {
        this->passengers = passengers;
    }
    void setPassengers(int passengers) {
        this->passengers = passengers;
    }
    int getPassengers() {
        return passengers;
    }
    int priceTransport() {
        for(int i = 0; i < passengers; i++) {
            basePrice-=200;
        }
        return basePrice;
    }
    friend bool operator<(const VanTransport &k1, const VanTransport &k2) {
        return k1.distance<k2.distance;
    }
};
void printExpensiveThanT(Transport **offers,int n,AutomobileTransport &T) {
    Transport *tmp;
    for(int i = 0; i <= n; i++){
        if(offers[i]->priceTransport() > T.priceTransport())
            cout<<offers[i]->getDestination()<<" "<<offers[i]->getDistance()<<" "<<offers[i]->getPrice()<<endl;    
        }       
}
int main() {
    char destination[20];
    int type,price,distance,passengers;
    bool driver;
    int n;
    cin>>n;
    Transport  **offers;
    offers=new Transport *[n];
    for (int i=0; i<n; i++) {
        cin>>type>>destination>>price>>distance;
        if (type==1) {
            cin>>driver;
            offers[i]=new AutomobileTransport(destination,price,distance,driver);
        } else {
            cin>>passengers;
            offers[i]=new VanTransport(destination,price,distance,passengers);
        }
    }
    AutomobileTransport at("Ohrid",2000,600,false);
    printExpensiveThanT(offers,n,at);
    for (int i=0; i<n; i++) delete offers[i];
    delete [] offers;
    return 0;
}

由于处理指针,最简单的方法是使用std::vectorstd::sort:

#include <vector>
//...
void printExpensiveThanT(Transport **offers, int n, AutomobileTransport &T) 
{
    std::vector<Transport*> sortedVect;
    for (int i = 0; i < n; i++)
    {
        if (offers[i]->priceTransport() > T.priceTransport())
            sortedVect.push_back(offers[i]);  // add this item to the vector
    }
    // sort the vector based on the dereferenced pointers and their respective 
    // operator <
    std::sort(sortedVect.begin(), sortedVect.end(), 
    [](Transport* left, Transport* right) { return *left < *right; });
    // print out the values
    for (auto it : sortedVect)
        cout << (*it).getDestination() << " " << (*it).getDistance() << " " << (*it).getPrice() << "n";
}

此外,您的原始代码比它应该循环的多了一个(i <= n是错误的)。

编辑:

如果您的编译器不支持C++11语法,这里有一个替代解决方案:

#include <vector>
//...
bool Sorter(Transport* left, Transport* right)
{ return *left < *right; }
void printExpensiveThanT(Transport **offers, int n, AutomobileTransport &T) 
{
    std::vector<Transport*> sortedVect;
    for (int i = 0; i < n; i++)
    {
        if (offers[i]->priceTransport() > T.priceTransport())
            sortedVect.push_back(offers[i]);  // add this item to the vector
    }
    // sort the vector based on the dereferenced pointers and their respective 
    // operator <
    std::sort(sortedVect.begin(), sortedVect.end(), Sorter);
    // print out the values
    std::vector<Transport*>::iterator it = sortedVect.begin();
    while (it != sortedVect.end()) 
    {
        cout << (*it).getDestination() << " " << (*it).getDistance() << " " << (*it).getPrice() << "n";
        ++it;
    }
}