IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

C++ Discussion :

Trier une liste contenant une paire ?


Sujet :

C++

  1. #1
    Membre régulier
    Inscrit en
    Septembre 2002
    Messages
    200
    Détails du profil
    Informations forums :
    Inscription : Septembre 2002
    Messages : 200
    Points : 120
    Points
    120
    Par défaut Trier une liste contenant une paire ?
    Bonjour à tous,

    Je pensais réussir ce que j'entreprends mais je me heurte à un problème.... Je veux simplement trier une liste qui contient une paire de (wxString, char). Pour l'exemple, considérons le type wxString comme un std::string, ca changera rien.

    Voici ma déclaration:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    	// List to keep words for sorting them
    	typedef std::pair< wxString, char > typ_pair_list ;
    	std::list< typ_pair_list > m_o_right_word_list ;
    Ensuite, je pensais tout simplement utiliser la fonction sort() de la liste en lui passant un prédicat binaire. Je déclare donc, toujours dans ma classe:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    // Binary predicat to sort a list according to the points
    	struct	Sort_List_By_Points : std::binary_function< const typ_pair_list&, const typ_pair_list&, bool >
    	{
    		bool operator() ( const typ_pair_list& par_o_element1, const typ_pair_list& par_o_element2 ) const ;
    	};
    OK, et maintenant, à l'utilisation, voici comme je fais mon tri:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    m_o_right_word_list.sort( Sort_List_By_Points() );
    Et voici la definition de mon opérateur() :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    bool
    CDisplayStats::Sort_List_By_Points::operator()( const typ_pair_list& par_o_element1, const typ_pair_list& par_o_element2 ) const
    {
    	return  par_o_element1.second < par_o_element2.second ;
    }
    Le résultat attendu est que ca me trie ma liste selon l'élément second, cet a dire le nombre de points. Mais ce que j'ai en définitive à l'execution c'est que ca ne fait rien du tout, pas d'erreur, mais la liste reste identique. Voici un exemple de liste:
    Manger, 2
    Boire, 1
    Tarte, 0
    Cousin, 6
    Et après execution je veux:
    Tarte, 0
    Boire, 1
    Manger, 2
    Cousin, 6
    Merci pour votre aide.

  2. #2
    Nouveau membre du Club
    Profil pro
    Inscrit en
    Mars 2009
    Messages
    31
    Détails du profil
    Informations personnelles :
    Localisation : France, Bas Rhin (Alsace)

    Informations forums :
    Inscription : Mars 2009
    Messages : 31
    Points : 35
    Points
    35
    Par défaut
    Vous avez peut-être appelé sort avant de populer la liste ?
    Ou affiché la liste avant l'appel à sort ?

  3. #3
    Membre émérite

    Inscrit en
    Mai 2008
    Messages
    1 014
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 1 014
    Points : 2 252
    Points
    2 252
    Par défaut
    Hum, serait-il possible d'avoir un exemple minimale et compilable qui reproduit le problème ?

  4. #4
    Membre régulier
    Inscrit en
    Septembre 2002
    Messages
    200
    Détails du profil
    Informations forums :
    Inscription : Septembre 2002
    Messages : 200
    Points : 120
    Points
    120
    Par défaut
    Je pensais avoir fourni le code suffisant. Je développe mn application avec wxWdigets, je peux fournir un code compilable, mais avec la lib wxWdigets.
    Sinon, faire une classe et un simple main() devrait faire l'affaire.

    Si je me lance, ca donnerait ca:

    .h

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    class CDisplayStats :
    {
    public:
     
        CDisplayStats( );
     
     
    protected :
     
    private:
     
    	// Event functions
    	void 		OnRadioButtonSort();
     
    	// List to keep words for sorting them
    	typedef std::pair< std::string, char > typ_pair_list ;
    	std::list< typ_pair_list > m_o_right_word_list ;
     
    	// Binary predicat to sort a list according to the points
    	struct	Sort_List_By_Points : std::binary_function< const typ_pair_list&, const typ_pair_list&, bool >
    	{
    		bool operator() ( const typ_pair_list& par_o_element1, const typ_pair_list& par_o_element2 ) const ;
    	};
    };
    .cpp

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
     
    #include "CDisplayStats.h"
     
    CDisplayStats::CDisplayStats(){
    m_o_right_word_list.push_back( std::make_pair( "MANGER", 2 ));
    m_o_right_word_list.push_back( std::make_pair( "BOIRE", 1 ));
    m_o_right_word_list.push_back( std::make_pair( "TARTE", 0 ));
    m_o_right_word_list.push_back( std::make_pair( "COUSIN", 6 ));
    }
     
    void
    CDisplayStats::OnRadioButtonSort( /*wxCommandEvent& WXUNUSED(event)*/ )
    {
    	// Action to do : RIGHT SORT BY POINTS
    	m_o_right_word_list.sort( Sort_List_By_Points() );
    }
     
    /*
     * Binary predicat to sort a list according to the points
     * */
    bool
    CDisplayStats::Sort_List_By_Points::operator()( const typ_pair_list& par_o_element1, const typ_pair_list& par_o_element2 ) const
    {
    	return  par_o_element1.second < par_o_element2.second ;
    }

    Et le main pourquoi pas:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    int main()
    {
       CDisplayStat stat ;
       stat.OnRadioButtonSort();
    }

  5. #5
    Rédacteur

    Avatar de Davidbrcz
    Homme Profil pro
    Ing Supaéro - Doctorant ONERA
    Inscrit en
    Juin 2006
    Messages
    2 307
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 32
    Localisation : Suisse

    Informations professionnelles :
    Activité : Ing Supaéro - Doctorant ONERA

    Informations forums :
    Inscription : Juin 2006
    Messages : 2 307
    Points : 4 732
    Points
    4 732
    Par défaut
    Ce code (ton code en fait, à s/wxString/std::string près ) marche chez moi (Debian SID, g++ 4.3.2):
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    #include <list>
    #include <iostream>
     
    class CDisplayStats 
    {
    public:
     
        CDisplayStats( );
     
     // Event functions
    	void 		OnRadioButtonSort();
     
    protected :
     
    private:
     
     
    	// List to keep words for sorting them
        typedef std::pair< std::string, int > typ_pair_list ;
        std::list< typ_pair_list > m_o_right_word_list ;
     
     
    	// Binary predicat to sort a list according to the points
    	struct	Sort_List_By_Points : std::binary_function< const typ_pair_list&, const typ_pair_list&, bool >
    	{
    		bool operator() ( const typ_pair_list& par_o_element1, const typ_pair_list& par_o_element2 ) const ;
    	};
    };
     
    CDisplayStats::CDisplayStats(){
    m_o_right_word_list.push_back( std::make_pair( "MANGER", 2 ));
    m_o_right_word_list.push_back( std::make_pair( "BOIRE", 1 ));
    m_o_right_word_list.push_back( std::make_pair( "TARTE", 0 ));
    m_o_right_word_list.push_back( std::make_pair( "COUSIN", 6 ));
    }
     
    void
    CDisplayStats::OnRadioButtonSort( /*wxCommandEvent& WXUNUSED(event)*/ )
    {
    	// Action to do : RIGHT SORT BY POINTS
    	m_o_right_word_list.sort( Sort_List_By_Points() );
     
    	typedef std::list< typ_pair_list >::iterator IT;
    	for(IT it=m_o_right_word_list.begin();it!=m_o_right_word_list.end();++it)
    	    std::cout<<it->first<<"  "<<it->second<<std::endl;
    }
     
    /*
     * Binary predicat to sort a list according to the points
     * */
    bool
    CDisplayStats::Sort_List_By_Points::operator()( const typ_pair_list& par_o_element1, const typ_pair_list& par_o_element2 ) const
    {
     
        return  par_o_element1.second < par_o_element2.second ;
    }
     int main()
    {
       CDisplayStats stat ;
       stat.OnRadioButtonSort();
    }

  6. #6
    Membre régulier
    Inscrit en
    Septembre 2002
    Messages
    200
    Détails du profil
    Informations forums :
    Inscription : Septembre 2002
    Messages : 200
    Points : 120
    Points
    120
    Par défaut
    Salut, oui excusez moi tous de m'etre alarmé et pointé du doigt la STL, je viens de me rendre compte qu'il s'agissait d'un problème de mise a jour graphique des données dans ma grille wxWidgets (en fait, mes données étaient empilées à la suite de ma grille, c'est pour ca que je croyais que la liste n'était pas triée).

    Merci pour votre aide !
    A la prochaine.

  7. #7
    Membre émérite

    Inscrit en
    Mai 2008
    Messages
    1 014
    Détails du profil
    Informations forums :
    Inscription : Mai 2008
    Messages : 1 014
    Points : 2 252
    Points
    2 252
    Par défaut
    Edit : Ok Bonne nuit.

    Je pensais avoir fourni le code suffisant. [...] Sinon, faire une classe et un simple main() devrait faire l'affaire.
    Oui mais justement, un code simple et minimale, chez moi, donne bien le résultat attendu.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
     
    #include <string>
    #include <iostream>
    int main(int argc, char* argv[])
    {
       // List to keep words for sorting them
       typedef std::pair< std::string, char > typ_pair_list ;
       std::list< typ_pair_list > m_o_right_word_list ;
     
       struct Sort_List_By_Points : std::binary_function< const typ_pair_list&, const typ_pair_list&, bool >
       {
          bool operator() ( const typ_pair_list& par_o_element1, const typ_pair_list& par_o_element2 ) const
          {
              return  par_o_element1.second < par_o_element2.second ;
          }
    };
     
       m_o_right_word_list.push_back(std::make_pair("manger", 1)); 
       m_o_right_word_list.push_back(std::make_pair("boire", 0)); 
     
       std::cout << m_o_right_word_list.front().first << std::endl; // manger
       std::cout << m_o_right_word_list.back().first << std::endl; // boire
     
       m_o_right_word_list.sort( Sort_List_By_Points() );
     
       std::cout << m_o_right_word_list.front().first << std::endl; // boire 
       std::cout << m_o_right_word_list.back().first << std::endl;  //manger
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [AC-2003] [VBA]Trier un formulaire contenant une liste déroulante
    Par c4rr3r4 dans le forum VBA Access
    Réponses: 4
    Dernier message: 13/04/2015, 09h29
  2. Liste contenant une liste?
    Par BruceBoc dans le forum VB.NET
    Réponses: 5
    Dernier message: 12/03/2015, 16h52
  3. Développement d'une WP contenant une liste renvoyant le groupe AD
    Par diabli73 dans le forum Développement Sharepoint
    Réponses: 1
    Dernier message: 07/04/2011, 15h07
  4. <logic:iterate> d'une Hashmap contenant une liste
    Par kenshiroseifu dans le forum Struts 1
    Réponses: 3
    Dernier message: 20/04/2009, 18h09
  5. Réponses: 12
    Dernier message: 03/12/2007, 18h53

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo