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 :

+ Performant : Rechercher struc ou map


Sujet :

C++

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 116
    Points : 40
    Points
    40
    Par défaut + Performant : Rechercher struc ou map
    Bonjour,
    je voudrais savoir quelle est la solution la plus performante pour une recherche assez fréquente.
    Faut-il mieux utiliser un structure ou une map de ce type :

    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
    Struct Objets
    {
    Int lrcTn;		
    Int localTn;		
    Int deltaT ;		
    Int nat ;		
    char scr [8] ;		
    int azimuth ;
    float range;
    char latitude [9] ;
    char longitude [9] ;
    int course ;
    int speed;
    char name [10];
    char environment [15];
    char trackIndentity [15];
    char trackPriId [30];
    char trackSecId [30];
    } objets[100];
    ou

    typedef std::map <Int ,Int ,Int ,Int ,char , int, float, float, char , char , ...>

    Merci

  2. #2
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Une map ne prenant que 2 paramètres (type des clés et type des valeurs), je ne pense pas que ce soit une solution envisageable si tu souhaites pouvoir changer le critère de recherche.

    A vérifier, mais je crois que boost::multi_index est fait pour cela.

    La recherche porte sur quels critères exactement ?

  3. #3
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Tu as ta structure d'objets, tu as une clé de classement, donc tu peux faire une map.

  4. #4
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 116
    Points : 40
    Points
    40
    Par défaut
    Dans ce tableau ou dans cette structure je vais rechercher:

    Je dispose de la valeur lrcTn.
    Je dois récupérer la valeur des autres champs.

    Donc je me demander si pour une recherche de ce type la structure si dessus était la solution la plus efficace sachant qu'il y aura environ 100 object dans le tableau, ou si alors une autre solution peut etre plus performante.

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 116
    Points : 40
    Points
    40
    Par défaut
    Citation Envoyé par Miles
    Tu as ta structure d'objets, tu as une clé de classement, donc tu peux faire une map.
    Comment ça?

  6. #6
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Ah ok... Dans ce cas c'est facile, utilise un map<int, Objets>. L'int en question sera le champ lrcTn avec lequel tu veux indexer tes objets.

  7. #7
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 116
    Points : 40
    Points
    40
    Par défaut
    Merci, de vos conseil

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 116
    Points : 40
    Points
    40
    Par défaut
    encore une question :
    Comment déclare-t-on l'objet dans la map. J'ai déclaré de cette façon :

    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
     
    class CMobile  
    {
    public:
    	CMobile();
    	virtual ~CMobile();
     
    struct Objets
    	{
    		long	ma_lrcTn;		
    		long	ma_localTn;
    		long	ma_azimut;
    		long	ma_range;
    		char *	latitude;
    		char *	longitude;
    		long	ma_course ;
    		double	ma_speed;
    		char *	ma_name;
    		char *	ma_environment;
    		char *	ma_trackIndentity;
    		char *	ma_trackPriId;
    		char *	ma_trackSecId;
    		long	ma_deltaT;		
    		char *	ma_nat;		
    		char *  ma_scr;	
    	} objets[NB_OBJECT];
     
    	typedef std::map <int, objets> mapObjets;
    }
    et j'obtiens les erreurs suivantes :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    error C2327: 'CMobile::objetsHLA' : member from enclosing class is not a type name, static, or enumerator
    error C2065: 'objetsHLA' : undeclared identifier
    A quoi est-ce du?

  9. #9
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    C'est quoi objetsHLA ? ...

    Sinon, je pense que tu as mal saisi le principe. La map est ton conteneur, tu n'as plus besoin de ton tableau de 100 Objets.

    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
    map<int, Objets> objets;
     
    Objets Obj;
     
    // Insersion -- version 1
    objets.insert(map<int, Objets>::value_type(5, Obj));
     
    // Insertion -- version 2 (plus simple mais un poil moins performant)
    objets[5] = Obj;
     
    // Recherche -- version 1
    Objets& Obj = objets[5];
    Obj.ma_lrcTn = ...;
    Obj.ma_localTn = ...;
     
    // Recherche -- version 2 (gère le cas où l'objet ne s'y trouve pas)
    map<int, Objets>::iterator It = objets.find(5);
    if (It != objets.end()
    {
       Objets& Obj = It->second;
       Obj.ma_lrcTn = ...;
       Obj.ma_localTn = ...;
    }
    else
    {
       // Aucun objet associé à l'identifiant 5
    }

  10. #10
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 116
    Points : 40
    Points
    40
    Par défaut
    j'aurais encore une question sur le fonctionnement des map:
    je voudrais savoir comment faire ceci :
    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
     
    struct Objets
    {
          long   ma_lrcTn;         
    };
     
    typedef std::map <int, objets> mapObjets;
     
    mapObjets my_mapObject;
     
    Int2String::iterator i = my_mapObject.begin();
    while (i != my_mapObject.end())
    {
          //COMMENT FAIRE CELA?????
          if(my_mapObject[???].ma_lrcTn = 41) return ???;
          ++i;
    }
    encore Merci

  11. #11
    Rédacteur

    Avatar de Matthieu Brucher
    Profil pro
    Développeur HPC
    Inscrit en
    Juillet 2005
    Messages
    9 810
    Détails du profil
    Informations personnelles :
    Âge : 42
    Localisation : France, Pyrénées Atlantiques (Aquitaine)

    Informations professionnelles :
    Activité : Développeur HPC
    Secteur : Industrie

    Informations forums :
    Inscription : Juillet 2005
    Messages : 9 810
    Points : 20 970
    Points
    20 970
    Par défaut
    Ca sera (*i.ma_lrcTn == 41) car *i est un struct Objects.
    D'ailleurs, fais attention, dans ton code, tu n'as pas de type "objets" alors que tu l'utilises.

  12. #12
    Membre du Club
    Profil pro
    Inscrit en
    Avril 2003
    Messages
    116
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2003
    Messages : 116
    Points : 40
    Points
    40
    Par défaut
    j'ai essayé ce que tu m'as dit

    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
    //.h
    struct Objets
    {
    	int		m_Handle;
    };
    typedef std::map <int, Objets> mapObjets;
    mapObjets m_Objets;
     
    //-----------------------------------
    //.ccp
    int CMobile::findIdxHandle(int Handler)
    {
            mapObjets::iterator It = m_Objets.begin();
            while(It != m_Objets.end() )
            {
    	        if(*It.m_Handle == Handler) return *It;
     
    	        It++;
            }
    }

    Mais il me dit que :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    error C2039: 'm_Handle' : is not a member of 'iterator'
    c:\program files\microsoft visual studio\vc98\include\xtree(118) : see declaration of 'iterator'
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    error C2440: 'return' : cannot convert from 'struct std::pair<int const ,struct CMobile::ObjetsHLA>' to 'int'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    En fait je voudrais retourner le int de mon map (map <int, Objets>)

  13. #13
    Rédacteur
    Avatar de Laurent Gomila
    Profil pro
    Développeur informatique
    Inscrit en
    Avril 2003
    Messages
    10 651
    Détails du profil
    Informations personnelles :
    Âge : 39
    Localisation : France, Moselle (Lorraine)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2003
    Messages : 10 651
    Points : 15 920
    Points
    15 920
    Par défaut
    Les éléments d'une map sont des std::pair<TypeCle, TypeValeur>. On accède au premier via le membre first, et au second via le membre second.

    Donc :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    int CMobile::findIdxHandle(int Handler)
    {
            mapObjets::iterator It = m_Objets.begin();
            while(It != m_Objets.end() )
            {
               if(It->second.m_Handle == Handler) return It->first;
     
               It++;
            }
    }

Discussions similaires

  1. Performance - Recherche dans une arborescence rapidement.
    Par BobbyWeb dans le forum Zend Framework
    Réponses: 3
    Dernier message: 07/04/2011, 15h56
  2. Réponses: 2
    Dernier message: 14/03/2011, 21h20
  3. Réponses: 1
    Dernier message: 04/12/2007, 16h36
  4. Réponses: 8
    Dernier message: 10/09/2004, 17h30

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