STL-Funktion sort/stable_sort



  • Hallo,
    ich habe da folgenden code-ausschnitt (fuern Kartenspiel). Jedoch erzeugt mein Hauptprogramm nicht das, was ich erwarte (Karten sollen gemischt, ausgegeben, sortiert und ausgegeben werden). Die Sortierung der Karten funzt anscheinend nicht, jedoch weiss ich nicht warum. Waere dankbar, wenn jemand nen tipp haette. Danke im voraus.

    typedef enum {Diamond, Club, Heart, Spade} Suit;
    
    class Card{
    public:
      Card():m_rank(1), m_suit(Spade){}
      Card(Suit sv, int rv):m_rank(rv),m_suit(sv){}
    
      int getRank() const {return m_rank;}
      Suit getSuit() const {return m_suit;}
    
    protected:
      int m_rank;
      Suit m_suit;
    
    // Output a card to an output stream
    // Implementierung gebe ich hier mal der einfachheit halber nicht an ...
      friend ostream& operator<<(ostream&, const Card&);
    
    // Define ordering relationships on cards
      friend bool operator<(const Card& c1, const Card& c2){return (c1.m_rank < c2.m_rank);}
      friend bool operator>(const Card& c1, const Card& c2){return (c1.m_rank > c2.m_rank);}
      friend bool operator==(const Card& c1, const Card& c2){return (c1.m_rank == c2.m_rank);}
    };
    
    class Deck{
    public:
      Deck(){
             for(int i = 1; i <= 13; ++i){
               m_cards.push_back(new Card(Diamond, i));
    	  m_cards.push_back(new Card(Spade, i));
    	  m_cards.push_back(new Card(Heart, i));
    	  m_cards.push_back(new Card(Club, i));
             }
      }
      void shuffle(){random_shuffle(m_cards.begin(), m_cards.end());}
      void sortDeck(){sort(m_cards.begin(), m_cards.end());}
      void showDeck(){
    	vector<Card *>::iterator c_begin = m_cards.begin();
    	vector<Card *>::iterator c_end = m_cards.end();
    	while(c_begin!=c_end){
                 Card *c = *c_begin; cout << (*c) << endl;	++c_begin;
             }
      }
    protected:
      vector<Card *> m_cards;
    };
    
    int main(int argc, char *argv[]){
    
      Deck stapel;                  // PUNKT 1
      stapel.showDeck();
      cout << endl;
      stapel.shuffle();             // PUNKT 2
      stapel.showDeck();
      cout << endl;
      stapel.sortDeck();            // PUNKT 3
      stapel.showDeck();
      cout << endl;
    
      return 0;
    }
    

    Obiges Programm liefert aber folgende Ausgabe (ich gebe nur Ausgabe nach PUNKT3 an):
    [Ace,D]
    [King,C]
    [King,H]
    [King,S]
    [King,D]
    [Queen,C
    [Queen,H
    [Queen,S
    [Queen,D
    [Jack,C]
    [Jack,H]
    [Jack,S]
    [Jack,D]
    [9,C]
    [9,D]
    [8,C]
    [8,H]
    [8,S]
    [8,D]
    [7,C]
    [7,H]
    [7,S]
    [7,D]
    [6,C]
    [6,H]
    [6,S]
    [6,D]
    [5,C]
    [5,H]
    [9,H]
    [9,S]
    [5,D]
    [4,C]
    [4,H]
    [4,S]
    [4,D]
    [3,C]
    [3,H]
    [5,S]
    [3,D]
    [2,C]
    [2,H]
    [3,S]
    [2,D]
    [2,S]
    [1,H]
    [1,C]
    [1,S]
    [1,D]
    [Ace,C]
    [Ace,H]
    [Ace,S]

    Warum ist das so?????



  • Hier geht's weiter


Anmelden zum Antworten