【问题标题】:[C++]Sorting objects by class member's value[C++]按类成员的值对对象进行排序
【发布时间】:2016-04-16 00:22:33
【问题描述】:

在下面显示的代码中,在函数 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;
}

【问题讨论】:

  • 我用向量做了一些实验,但没有成功,所以我删除了它 -- 我敢打赌,你留下的代码比如果你真的正确使用了矢量。
  • 你知道sscce.org 是什么吗?抱歉,但我不知道如何在您的完整代码中找到您问题的一部分。所以请准备一个包含您的问题的简短示例!

标签: c++ sorting object


【解决方案1】:

由于您正在处理指针,最简单的方法是使用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 &lt;= 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;
    }
}

【讨论】:

  • 感谢您的回答。虽然,我正在使用的编译器无法识别 for (auto it : sortedVect) 循环,但是否可以使用普通的 for 循环?如果是的话,它会是什么样子。问这个原因我对 库不太熟悉。同样在[](Transport* left, Transport* right) { return *left &lt; *right; });,它需要 [] 中的表达式。提前致谢
  • 我用非 C++ 11 版本编辑了答案。但是,您应该认真考虑升级编译器以支持 C++ 11 语法。
猜你喜欢
  • 1970-01-01
  • 2011-06-02
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-18
相关资源
最近更新 更多