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

SL & STL C++ Discussion :

supprimer un element d'un tableau vector


Sujet :

SL & STL C++

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut supprimer un element d'un tableau vector
    hello all

    j'ai un gros probléme que je n'arrive pas a résoudre pouvez vous m'aidez svp ?

    voila mon probleme:

    je doit inserer un client dans un tableau en respectant l'ordre alphabetique

    voila mes structure:
    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
     
    struct Identite
    {
           string nomCli;
           string prenomCli;
           int age;
           char statut;
    };
     
    struct DateEmp
    {
           int jour;
           int mois;
           int annee;
    };
     
    struct Client
    {
           DateEmp dateE;
           Identite id;
           int montantEmp;
           int tauxInteret;
           int duree;
           int montantInteret;
    };
    le trie doit se faire par le nom donc par:
    Client tableau;

    tableau[0].id.nom;

    trop besoin d'aide svp

    une derniere question comment puis je retourné un tableau de structure ?
    par exemple:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    int main
    {
    fonction1();
     
    fonction2(tab);
    }
     
    fonction1()
    {
    Client tab;
    tab[0].prenom= ...;
    tab[0].age=..;
    reurn tab;
    }
    donc je voudrai pouvoir utilisé les donnée contenu dans tab dans la fonction2 comment puis je faire ?
    j'avais essayé de declaré le tab dans main et donc de le passé en parametre a la fonction1 mais cela ne fonctionné pas peut etre paceque je passe aussi un string en parametre
    ...

  2. #2
    Membre du Club
    Inscrit en
    Novembre 2002
    Messages
    51
    Détails du profil
    Informations forums :
    Inscription : Novembre 2002
    Messages : 51
    Points : 57
    Points
    57
    Par défaut
    Quand tu passes un tableau à une foncion, tu dois aussi passer sa taille.

    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
     
     
    int cmpId( Identite i1, Identite i2){
         // Comparaison des 2 identitées
    }
     
    void tri( Client* clientTab, int tabSize ){
        int i,j;
        for( i=0 ; i<tabSize ; ++i )
             for(j=i; j<tabSize ;++j )
                  if( cmpId( clientTab[i].id, clientTab[j].id ) > 0 ){
                        client tmp=clientTab[i];
                        clientTab[i]=clientTab[j];
                        clientTab[j]=tmp;
                  } 
    }
     
    int main(void){
         Client tableau[8];
         tri( tableau , 8 );
    }
    Voila je sais pas si ça va t'aider, mais c'est comme que je structurerai mon programme. C'est pas optimal, mais c'est simple.
    Bonne chance

  3. #3
    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
    Je te conseillerai bien d'aller voir la FAQ, il y a ce qu'il faut pour répondre à ta question, mais vu ton autre post apparemment tu ne dois pas utiliser de vector.

    Enfin si tu peux utiliser std::sort, va quand même faire un tour (dans la FAQ C++) du côté des foncteurs.

    une derniere question comment puis je retourné un tableau de structure ?
    Question déjà traitée, va faire un tour par la recherche avancée.

  4. #4
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    616
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 616
    Points : 164
    Points
    164
    Par défaut
    Tant qu'on y est, si tu veut en apprendre ou que tu as tout simplement besoin d'un tri plus performant, voici un exelent site:

    http://www.dailly.info/algorithmes-de-tri/index.php

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    ok merci !

    mais en utilisant les vector on fait comment svp?
    j'ai pas trouvé dans la faq meme le std::sort
    vous pouvez m'aidez svp ?

  6. #6
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    616
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 616
    Points : 164
    Points
    164
    Par défaut
    Pour le std::sort il faut penser a inclure <algorithm> ( de mémoire)
    et encore de mémoire, ca s'utilise avec en paramétre 2 itérateurs ( un de début et de fin par exemple )

    du style mon_vector.sort(mon_vector.begin(),mon_vector.end());

    Enfin c'est a prendre avec des pincettes ce que je dit

  7. #7
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    Bonjour a tous

    donc voila ce que j'ai fais mais malheureusement ca compile tres bien mais ca ne m'affiche pas le tableau trier quelqu'un peut m'aider svp ? je desespere

    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
    62
    63
    64
    65
    66
    67
    68
     
         struct Identite
         {
                string nom;
                string prenom;
                int age;
                char statut;
     
                bool operator<(const Identite & compar) const
                { return nom < compar.nom; }
         };
     
         struct Date
         {
            int jour;
            int mois;
            int annee;
         };
     
         struct Client
         {
                Date date;
                Identite id;
                int montantEmprunt;
                int tauxInteret;
                int duree;
                int montantInteret;
     
                 bool operator<(const Client & compar) const
                 { return id < compar.id; }
         };
         ...
         vector<Client> TrierClients(vector<Client> listeClient);
         void Affiche(vector<Client> listeClient, const string cli);
     
         int main()
         {
         ...
         cout << "Entrez le nom du client recerhce: ";
         cin >> nomClient;             
         Affiche(listeClients, nomClient);
     
         listeClients = TrierClients(listeClients);
     
         cout << "Entrez le nom du client recerhce: ";
         cin >> nomClient;             
         Affiche(listeClients, nomClient);
         return 0;
         }
     
         vector<Client> TrierClients(vector<Client> listeClient)
         {   
              sort(listeClient.begin(),listeClient.end());
     
              return listeClient;
         }
     
         void Affiche(vector<Client> listeClient,const string cli)
         {         
              cout <<endl <<endl;
         for (int i = 0; i < listeClient.size(); i++)
              {
                   if(cli == listeClient[i].id.nom)
                   {
                   cout << listeClient[i] << endl;
                   }
              }     
         }

  8. #8
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    j'ai quelque surcharge d'operateur de fait:
    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
     
    ostream& operator << (ostream& flux, Date & D)
    {
       flux << D.jour << " " << D.mois << " " << D.annee;
       return flux;
    }
     
    istream& operator >> (istream& flux, Date & D)
    {
       flux >> D.jour >> D.mois >> D.annee;
       return flux;
    }
     
    ostream& operator << (ostream& flux, Identite & I)
    {
       flux << I.nom << " " << I.prenom << " " << I.age << " " << I.statut;
       return flux;
    }
     
    istream& operator >> (istream& flux, Identite & I)
    {
       flux >> I.nom >> I.prenom >> I.age >> I.statut;
       return flux;
    }
     
    ostream& operator << (ostream& flux, Client & C)
    {
       flux << C.id << " " << C.date << " " << C.montantEmprunt << " ";
       flux << C.tauxInteret << " " << C.duree << " " << C.montantInteret;
       return flux;
    }
     
    istream& operator >> (istream& flux, Client & C)
    {
       flux >> C.id >> C.date >> C.montantEmprunt >> C.tauxInteret >> C.duree >> C.montantInteret;
       return flux;
    }

  9. #9
    tut
    tut est déconnecté
    Membre averti
    Avatar de tut
    Inscrit en
    Juillet 2002
    Messages
    373
    Détails du profil
    Informations forums :
    Inscription : Juillet 2002
    Messages : 373
    Points : 394
    Points
    394
    Par défaut
    il faut que tu définisses l'opérateur < sur les structures que tu veux trier, sinon comment veux-tu que std::sort sache sur quel critère tu veux trier ?

  10. #10
    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
    Citation Envoyé par tut
    il faut que tu définisses l'opérateur < sur les structures que tu veux trier, sinon comment veux-tu que std::sort sache sur quel critère tu veux trier ?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    struct Client
         {
              // ...
     
              bool operator<(const Client & compar) const
                 { return id < compar.id; }
         };
    De toute façon sans ça son code n'aurait même pas compilé.

  11. #11
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    ben il est defini dans ma structure Client

  12. #12
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    desolé de faire en core chier mais j'en peu plus là je suis trop perdu en plus...

    donc je laisse tomber ma fonction de trie pour l'instant car c'est trop la merde...

    Et la j'ai un nouveau preobléme avec une fonction qui doit supprimer un client (l'indice et passé en parametre)
    voila ce que j'ai fais cela compile bien mais lorsque je mon programme s'execute et arrive au niveau de cette fonction là j'ai bug et tout ce ferme
    quelqu'un pourrait-il me dire pourquoi j'ai ce probléme svp ?

    ma fonction qui permet de supprimer le client:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
     
    vector<Client> SuppClient(vector<Client> listeClient, int &indicateur)
    {
          for (indicateur ; indicateur < listeClient.size() ; indicateur++)
         {
              listeClient[indicateur] = listeClient[indicateur+1];
         }
         return listeClient;
    }
    dans mon main:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    cout << "Entrez le nom du client a supprimer: ";
              cin >> nomClient;
     
              incClient = RechClient(tabClients, nomClient);
              tabClients = SuppClient(tabClients,incClient);
    pour info la fonction recherche (elle fonctionne puisqu'elle me permet de recherhcer puis d'afficher (a l'aide d'une autre fonction) un client dont le nom et passé en parametre et j'ai testé ca fonctionne nickel)
    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
     
    int RechClient(vector<Client> listeClient,const string nomCli)
    {
        int indice;
     
          for (int i = 0; i < listeClient.size(); i++)
         {
              if(listeClient[i].id.nom == nomCli)
              {
              indice = i;
              break;
              }
              else
              {
                  indice = -1;
              }
         }
         return indice;
    }

  13. #13
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    616
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 616
    Points : 164
    Points
    164
    Par défaut
    Ta fonction supprimer client me parait bizzaire ( ou alors je suis pas trés reveillé, ce qui est possible lol )
    On dirai que tu decale toute case a partir de ton rang "indice"
    Peut-etre que quand tu tape dans indicateur +1 a la derniére boucle il n'aime pas

    Enfin bon j'ai jms été trés doué la dedans :p

  14. #14
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    oui, je decale tout ce qui est a droite de la valeur a supprimer pour ecraser celle ci j'ai pas trouvé d'autre moyen

  15. #15
    Membre habitué
    Inscrit en
    Octobre 2004
    Messages
    616
    Détails du profil
    Informations forums :
    Inscription : Octobre 2004
    Messages : 616
    Points : 164
    Points
    164
    Par défaut
    hum, je pense qu'il doit y avoir une methode plus simple, du style erase(), enfin regarde la doc de la stl, du devra touver ton bonheur .

    Edit: si tu n'est pas alergique a l'anglais je doit avoir un lien ou tu trouvera ce qu'il te faut.

    http://www.geocities.com/mikerisan/module27.html

  16. #16
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    c 'est bon j'ai reussi c'etait en effet la derniere boucle qui n'aimé pas
    merci beaucoup !

  17. #17
    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
    Il faurt aller jusqu'à size() - 1 (et non size()) puisque tu prends indicateur + 1, sinon tu vas aller un élément trop loin.

    A part ça, tout existe dans la STL :

    - find ou find_if pour la recherche
    - vector::erase + remove ou remove_if pour la suppression

    Avec comme d'hab les foncteurs qui vont bien, cela se fait sans peine en une ou deux lignes de code.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    void SuppClient(vector<Client>& listeClient, int Indice)
    {
          listeClient.erase(listeClients.begin() + Indice);
    }
    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
    struct FindClient
    {
        FincClient(const string& n) : nom(n) {}
     
        bool operator()(const Client& c) const
        {
            return c.id.nom == nom;
        }
     
    private :
     
        string nom;
    };
     
    int RechClient(const vector<Client>& listeClient,const string& nomCli)
    {
        vector<Client>::iterator it = find_if(listeClients.begin(), listeClients.end(), FindClient(nomClient));
     
        if (it == listeClient.end())
            return -1; // ou throw machin
        else
            return it - listeClients.begin();
    }
    Ou encore, le tout en une ligne :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    listeClients.erase(remove_if(listeClients.begin(), listeClients.end(), FindClient(nomClient)), listeClients.end());

  18. #18
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    ok merci beaucoup

  19. #19
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    16
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 16
    Points : 4
    Points
    4
    Par défaut
    J'ai les surcharge d'operateur suivant:

    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
     
    ostream& operator << (ostream& flux, Date & D)
    {
       flux << D.jour << " " << D.mois << " " << D.annee;
       return flux;
    }
     
    istream& operator >> (istream& flux, Date & D)
    {
       flux >> D.jour >> D.mois >> D.annee;
       return flux;
    }
     
    ostream& operator << (ostream& flux, Identite & I)
    {
       flux << I.nom << " " << I.prenom << " " << I.age << " " << I.statut;
       return flux;
    }
     
    istream& operator >> (istream& flux, Identite & I)
    {
       flux >> I.nom >> I.prenom >> I.age >> I.statut;
       return flux;
    }
     
    ostream& operator << (ostream& flux, Client & C)
    {
       flux << C.id << " " << C.date << " " << C.montantEmprunt << " ";
       flux << C.tauxInteret << " " << C.duree << " " << C.montantInteret;
       return flux;
    }
     
    istream& operator >> (istream& flux, Client & C)
    {
       flux >> C.id >> C.date >> C.montantEmprunt >> C.tauxInteret >> C.duree >> C.montantInteret;
       return flux;
    }
    j'ai donc ecrit une fonction pour telecharger le fichier et copier son contenu dans un tableau:
    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
     
    vector<Client> Telecharger_Fichier(string filename)
    {
       ifstream file(filename.c_str());
       Client client;
       vector<Client> listeClient;
     
       if (file)
          while (file >> client)
             listeClient.push_back(client);
     
       cout << "Nombre de clients : " << listeClient.size() << endl <<endl;
     
       return listeClient;
    }
    et là il me faut une fonction pour enregistrer les donnés du tableau dans le fichier comment puis je faire svp ?

    voila ce que j'ai fais mais ça ne fonctionne pas:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    vector<Client> Enregistrer(string filename)
    {
       ofstream file(filename.c_str());
       Client client;
       vector<Client> listeClient;
     
       if (file)
          while (file << client)
             listeClient.push_back(client);
     
       return listeClient;
    }

  20. #20
    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
    Le mécanisme d'écriture ne sera pas le même que celui de lecture, il ne suffit pas de changer le >> en <<.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    void Enregistrer(const vector<Client>& listeClients, string filename)
    {
       ofstream file(filename.c_str());
     
       if (file)
          for (vector<Client>::iterator i = listeClients.begin(); i != listeClients.end(); ++i)
             file << *i << endl;
    }

+ Répondre à la discussion
Cette discussion est résolue.
Page 1 sur 2 12 DernièreDernière

Discussions similaires

  1. Supprimer un element d'un tableau de structure
    Par Fatima.zahra dans le forum Débuter
    Réponses: 1
    Dernier message: 11/06/2012, 12h42
  2. Réponses: 5
    Dernier message: 07/06/2010, 13h12
  3. Réponses: 5
    Dernier message: 23/11/2009, 15h49
  4. Supprimer un element du tableau
    Par Javamar dans le forum Collection et Stream
    Réponses: 13
    Dernier message: 02/05/2009, 13h57
  5. [langage] supprimer le premier élément d'un tableau
    Par Kinethe dans le forum Langage
    Réponses: 2
    Dernier message: 20/07/2004, 15h39

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