2013-02-19 57 views
2

這可能是一個noob問題,但這是我的問題;我有一個有兩個不同int值的類卡,看起來像這樣。如何用兩個變量對向量進行排序?

#pragma once 
class Card 
{ 
public: 
int value; 
int suite; 

Card(int v, int s); 
~Card(void); 

void printCard(); 
int getScore(); 
}; 

現在我想根據它們的值來分類5張卡片。我試圖把它們放入一個向量,然後使用std :: sort,但我無法讓它工作。所以我的問題是做這件事的最好方法是什麼?

+0

看看這裏:http://www.cplusplus.com/reference/algorithm/sort/ – 2013-02-19 15:32:17

回答

2

您有兩種使用std::sort的選項。一個是重載operator <爲類:

bool operator< (const Card &lhs, const Card &rhs) { 
    return lhs.value < rhs.value; 
} 

然而,只有這樣做,如果真的很有意義總是比較Card對象是這樣的。如果您只需要進行特定排序,則可以使用接受自定義比較器的版本sort

定義謂詞有多種方法。對於可重複使用的,但簡單的標準,在類的靜態功能,可用於:

class Card 
{ 
public: 
    // ... as before 
    static bool lesserValue(const Card &lhs, const Card &rhs) { 
    return lhs.value < rhs.value; 
    } 
}; 

用法:

std::sort(from, to, &Card::lesserValue); 

對於一次性的東西(或對於需要保持內部狀態複雜的標準),請使用從std::binary_function派生的類並在其operator()中實現comaprison邏輯。在C++ 11,你還可以使用lambda函數這個:

std::sort(from, to, [](const Card &lhs, const Card &rhs) { return lhs.value < rhs.value; }); 
+0

非常感謝,並對重複的問題抱歉。如果我可能會更麻煩一點,但我想問一下這行static bool lesserValue(const Card&lhs,const Card&rhs)實際上做了什麼,因爲我自己並不完全理解它。 – 2013-02-20 14:20:38

+0

@AlexanderGranell它在類中定義了一個靜態成員函數。靜態成員函數「在類而不是實例上運行」 - 即它們內部沒有'this'指針,並且可以在沒有類的實例的情況下調用它們(例如,'返回Card :: lesserValue(myCard1,myCard2 );' – Angew 2013-02-20 14:25:12

+0

啊,我看到了,謝謝。 – 2013-02-20 19:26:43

3

你需要重載operator<

bool operator<(const Card& lhs, const Card& rhs){ 
.... logic here ... 
} 

然後用std::sort。您可能需要爲您的班級製作此操作員朋友。您也可以在類定義中執行:

class Card { 
.... 
public: 
    bool operator<(const Card& other) const { 
    .... logic here... 
    } 
}; // end of class Card 
0

我想出了這個答案,它使用比較類,這也使得它很容易比較套房爲例。我還改善了班上的公共和私人成員,因爲有些價值觀不應該公開。而你的課程目前不需要析構函數。

#include<iostream> 
#include<algorithm> 
#include<vector> 

class Card { 

    public: 

     Card(int v, int s) 
      : 
       value(v), 
       suite(s) 
     { 
     } 

     /*No need for destructor*/ 

     int getValue(void) const { return value;} 

     int getSuite(void) const { return suite;} 

     void printCard(void) const { 
      std::cout << "Value = " << value << "\tSuite = " << suite << std::endl; 
     } 

    private: 
     /*these shouldn't be public*/ 
     int value; 
     int suite; 
}; 

class CompareCardValue { 

    public: 

     bool operator() (const Card& c1, const Card& c2) 
     { 
      return c1.getValue() < c2.getValue(); 
     } 
}; 

class CompareCardSuite{ 

    public: 

     bool operator() (const Card& c1, const Card& c2) 
     { 
      return c1.getSuite() < c2.getSuite(); 
     } 
}; 

int main(){ 

    std::vector<Card> deck; 
    deck.push_back(Card(2,4)); 
    deck.push_back(Card(1,3)); 
    deck.push_back(Card(12,3)); 
    deck.push_back(Card(8,2)); 

    CompareCardSuite comp_suite; //compares for suite 
    CompareCardValue comp_value; //compares for value 

    /*We would like to use a const iterator since we are 
    *not interested in changing the Card intances 
    */ 
    std::vector<Card>::const_iterator it; 
    for (it = deck.begin(); it < deck.end(); it++){ 
     it->printCard(); 
    } 
    std::cout << std::endl; 

    /*sorting on value*/ 
    std::sort(deck.begin(), deck.end(), comp_value); 

    for (it = deck.begin(); it < deck.end(); it++){ 
     it->printCard(); 
    } 

    std::cout << std::endl; 
    /*sorting on suite*/ 
    std::sort(deck.begin(), deck.end(), comp_suite); 

    for (it = deck.begin(); it < deck.end(); it++){ 
     it->printCard(); 
    } 
} 
相關問題