2016-04-16 29 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/が何であるか知っていますか?申し訳ありませんが、完全なコードで質問の一部を見つける方法がわかりません。だからあなたの問題を含んでいる*短い*例を準備してください! – 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が間違っていた)すべきであるよりも、1以上のループ。

編集:あなたのコンパイラが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

関連する問題