Mettre un type"record" comme clé dans un map (C++)

Résolu
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010 - 25 mars 2008 à 13:20
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010 - 26 mars 2008 à 16:59
Bonjour,

Voilà j'ai un type fichier qui est un record (string nom, __int64 taille,FILETIME date) j'aimerai créer un map <Fichier,string>
mais il n'accepte pas que je lui passe le type Fichier au moment de l'insert

voici un bout de mon code pour y voir plus claire:




class Fichier{
      string nom;
      __int64 taille;
      FILETIME date;
public:
       Fichier (string nom, __int64 taille,FILETIME date){
               this->nom=nom;this->date=date;this->taille=taille;
               }
//.............
}

void mettreDansMap(map<Fichier,string> &fichierT,Fichier nom,string attribut){
     fichierT.insert(make_pair(nom,attribut));
     }



Merci Beaucoup de votre prècieuse aide.







Babyboo

10 réponses

luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 6
26 mars 2008 à 12:38
C'est pas si trivial ton pb. Tout vient de ton opérateur de comparaison. Remplace ca:

      friend int operator <(Fichier a,Fichier b){
           return ((a.taille


      friend bool operator <(const Fichier & a,const Fichier & b)
      {
            if (a.nom < b.nom)
                return true;
            else if (a.nom == b.nom)
                return ((a.date.dwLowDateTime < b.date.dwLowDateTime) && (a.date.dwHighDateTime < b.date.dwHighDateTime));
            else
                return false;
       }
3
luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 6
25 mars 2008 à 17:05
Il faut que ton type ait un constructeur par défaut. Il faut aussi que ton type dispose d'une fonction de tri. Exemple (un peu crade, mais explicite)

#include <map>

struct MyKey
{
    int value;
   
    // Fonction de tri utilisée par std::map
    bool operator() (const MyKey & _key1, const MyKey & _key2)
    {
        return (_key1.value < _key2.value);
    }
};

int main(int argc, char *argv[])
{
    std::map<MyKey, int, MyKey> lMap;
   
    MyKey lKey1 = { 4 };
    MyKey lKey2 = { 8 };
    lMap[lKey2] = 2; // On insère d'abord le 8
    lMap[lKey1] = 2; // puis le 4
   
    std::cout << lMap.begin()->first.value << std::endl;          // affiche bien 4
    std::cout << (++lMap.begin())->first.value << std::endl;   // affiche bien 8
   
    system("PAUSE");
   
    return 0;
};
0
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010
25 mars 2008 à 17:17
Merci mais ça n'allais pas trop mais tu m'as mises sur la piste des operator, après plusieurs rechercher j'ai ajouter ceci à ma classe Fichier et ça fonctionne mais maintenant j'ai encore d'autres problèmes ^^ il veux pas m'inserer tout les fichier certain oui et certain non lol
 
friend int operator ==(Fichier a,Fichier b){
           return ((a.taille==b.taille)&&(a.nom==b.nom));//reste la date
           }
          
      friend int operator <(Fichier a,Fichier b){
           return ((a.taille


Babyboo
0
luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 6
25 mars 2008 à 18:54
La raison est simple. Il utilise un test de comparaison pour savoir si deux fichiers sont identiques. Avec ton test, deux fichiers sont identiques si leur nom fait la meme longueur. Donc toto.txt écrasera tutu.txt ... Il faut un test plus complet.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010
26 mars 2008 à 11:26
mais j'ai tester la taille aussi?le probleme qu'il reste la date a tester mais je ne sais pas trop comment tester un Filetime
merci je me doutais qu'il y avait un problème dans mon test mais j'ai pas mal de lacunes encore en c++ alors ca fait beaucoup de problèmes à rèsoudre heureusement que tu m'as aidée c'est gentil de ta part :-)

Babyboo
0
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010
26 mars 2008 à 11:43
 friend int operator !=(Fichier a,Fichier b){
           return (!(a==b));//reste la date
           }
       friend int operator >(Fichier a,Fichier b){
           return (bb);//reste la date
           }
       friend int operator >=(Fichier a,Fichier b){
           return (a




J'ai ajouter ces opérator la mais ca ne change rien en principe s'il regarde la aille et le nom je pensais que cela était suffisant, je suis un peus perdue je joint le code complet peut être verez-vous des choses que je n'ai pas vue par manque d'expériences







Babyboo
0
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010
26 mars 2008 à 11:50
#include
#include
#include <windows.h>
#include<vector>
#include<string>
#include<map>
#include



using namespace std;



class Fichier{
      string nom;
      __int64 taille;
      FILETIME date;
public:
       Fichier (string nom, __int64 taille,FILETIME date){
               this->nom=nom;this->date=date;this->taille=taille;
               }
      
       friend int operator ==(Fichier a,Fichier b){
           return ((a.taille==b.taille)&&(a.nom==b.nom));//reste la date
           }
           
      friend int operator <(Fichier a,Fichier b){
           return ((a.taille(Fichier a,Fichier b){
           return (bb);
           }
       friend int operator >=(Fichier a,Fichier b){
           return (a &fichierT,Fichier nom,string attribut){
     
     map<Fichier,string>::iterator it;
     cout<<"=====dans mettreDans===========\n";//j'ai fais un test pour voir resultat à un certain moment il y a un
    //fichier  mais il n' l'insert pas certains oui d'autre non
     cout<<nom<<"av\n";
     for (it=fichierT.begin();it!=fichierT.end();++it)cout<first<<"\n";
     fichierT.insert(make_pair(nom,attribut));
     cout<<"ap\n";
     for (it=fichierT.begin();it!=fichierT.end();++it)cout<first<<"\n";
     }
//______________________________________________________________________________________
//______________________________________________________________________________________

void parcourir ( string attribut,map<Fichier,string> &fichierT){
   
     __int64 taille;
     FILETIME date;
     HANDLE hFind;
     WIN32_FIND_DATA wFindData;
     hFind = FindFirstFile("*.*", &wFindData);
     bool trouve = hFind!=INVALID_HANDLE_VALUE;
     char szFilePath[MAX_PATH+4];
     
     szFilePath + GetModuleFileName(0, szFilePath, MAX_PATH);
    
     while(trouve){
     
     if((wFindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)!=0){
                          if(strcmp(wFindData.cFileName,".")!=0 && strcmp(wFindData.cFileName,"..")!=0) {
                                            
                                            SetCurrentDirectory(wFindData.cFileName);//descendre dans le sous repertoire
                                            string Attributenv=attribut+"\"+wFindData.cFileName;
                                            parcourir(Attributenv,fichierT);
                                            SetCurrentDirectory(".."); //revenir                  
                                                         }           
                                      }
    else{
         taille=((__int64)wFindData.nFileSizeHigh<<32)+wFindData.nFileSizeLow;
         FILETIME fileTime1 = wFindData.ftLastWriteTime;
         FileTimeToLocalFileTime(&fileTime1,&date);
         Fichier f=Fichier(wFindData.cFileName,taille,date);
         mettreDansMap(fichierT,f,attribut);
         
    }
   
    trouve=FindNextFile(hFind,&wFindData);
}//fwhile
FindClose(hFind);

     }//fparcourir/
//-----------------------------------------------------------------------------------     
    string repertoireCourant(void)
    {
       const string s(__FILE__);
       return s.substr(0, s.rfind('\\\\'));
    }
//______________________________________________________________________________________
//_____________________________main_____________________________________________________
//______________________________________________________________________________________
int main(int argc, char *argv[])
{
map<Fichier,string>lol;
map<Fichier,string>::iterator it;
parcourir(repertoireCourant(),lol);
cout<<"*****************dans main******************\n";
for (it=lol.begin();it!=lol.end();++it)cout<first<<"  "<second<<"\n";

system("PAUSE");
}
0
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010
26 mars 2008 à 14:04
C'est super je pense pas que j'y arrais pensé il me semble que j'étais encore loin de la solution je te remerci je vais pour voir finir cet exercice : le but etant d'afficher tous les fichiers qui sont en double j'ai encore des choses à faire mais la tu m'as super bien aidée j'étais comme qui dirait bloquée merci encore c'est bête mais je suis très contente

Babyboo
0
luhtor Messages postés 2023 Date d'inscription mardi 24 septembre 2002 Statut Membre Dernière intervention 28 juillet 2008 6
26 mars 2008 à 16:27
Juste une petite correction, pour le test de date. Ceci me semble plus exacte que ce que je t'ai mis.

return (((__int64(a.date.dwHighDateTime) << 31) + __int64(a.date.dwLowDateTime)) <
((__int64(b.date.dwHighDateTime) << 31) + __int64(b.date.dwLowDateTime)))
0
babyboo1107 Messages postés 40 Date d'inscription samedi 22 mars 2008 Statut Membre Dernière intervention 9 septembre 2010
26 mars 2008 à 16:59
En tout cas ça fonstionnait ^^

Babyboo
0
Rejoignez-nous