2016-04-16 40 views
-3

在下面顯示的代碼中,函數void printExpensiveThanT(..)我應該打印出目標,距離和價格比在函數中提供T,按距離值升序排列。 我不知道我應該用什麼來排序他們,我嘗試了一些載體,但它沒有解決,所以我刪除了它。 任何幫助,將不勝感激。[C++]按類成員的值對對象排序

#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; 
} 
+2

*我嘗試用向量的東西,但它沒有工作,所以我刪除它* - 我敢打賭,你留下的代碼是更多的越野車,並且比實際使用正確的矢量更長。 – PaulMcKenzie

+0

你知道什麼是http://sscce.org/嗎?對不起,但我不知道如何在完整的代碼中找到問題的一部分。所以請準備一個包含您的問題的* short *示例! – Klaus

回答

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 <= 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; 
    } 
} 
+0

感謝您的回答。雖然,我正在編譯的編譯器無法識別'for(auto it:sortedVect)'循環,那麼使用普通的'for'循環可以嗎?如果是,它會是什麼樣子。問這個原因我不熟悉庫。同樣在'[](Transport * left,Transport * right){return * left <* right; });',它期望在[]中有一個表達式。在此先感謝 –

+1

我編輯答案與非C + + 11版本。但是,您應該認真考慮升級編譯器以支持C++ 11語法。 – PaulMcKenzie