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 :

string , obtenir qu'une seul lettre


Sujet :

C++

  1. #1
    Membre habitué

    Profil pro
    Inscrit en
    Avril 2004
    Messages
    421
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2004
    Messages : 421
    Points : 189
    Points
    189
    Par défaut string , obtenir qu'une seul lettre
    bonjour, voila, j'ai un string (tout l'alphabetà et j'aimerais qu'a chaque tour de boucle, j''ai une lettre. comme le code suivant.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     
        for (int i = 0; i < ASCII; i++){
     
            std::string letter =  Alphabet.substr(i,i+1);
            std::cout<<i<<"   "<< i+1<<"     " <<letter << std::endl;

    or ce code la me sort

    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
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
     
    0   1
    1   2     
    2   3     
    3   4     
    4   5     
    5   6     
     
    6   7     
     
     
     
    7   8
     
     
     
    8   9
     
     
    
    9   10
     
     
    
    10   11
     
     
    
    11   12
     
    
    12   13
    
    
    14   15     
    15   16     
    16   17     
    17   18      !"
    18   19      !"#$
    19   20      !"#$%&
    20   21      !"#$%&'(
    21   22      !"#$%&'()*
    22   23      !"#$%&'()*+,
    23   24      !"#$%&'()*+,-.
    24   25      !"#$%&'()*+,-./0
    25   26      !"#$%&'()*+,-./012
    26   27      !"#$%&'()*+,-./01234
    27   28      !"#$%&'()*+,-./0123456
    28   29      !"#$%&'()*+,-./012345678
    29   30      !"#$%&'()*+,-./0123456789:
    30   31      !"#$%&'()*+,-./0123456789:;<
    31   32      !"#$%&'()*+,-./0123456789:;<=>
    32   33      !"#$%&'()*+,-./0123456789:;<=>?@
    33   34     !"#$%&'()*+,-./0123456789:;<=>?@AB
    34   35     "#$%&'()*+,-./0123456789:;<=>?@ABCD
    35   36     #$%&'()*+,-./0123456789:;<=>?@ABCDEF
    36   37     $%&'()*+,-./0123456789:;<=>?@ABCDEFGH
    37   38     %&'()*+,-./0123456789:;<=>?@ABCDEFGHIJ
    38   39     &'()*+,-./0123456789:;<=>?@ABCDEFGHIJKL
    39   40     '()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMN
    40   41     ()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOP
    41   42     )*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQR
    42   43     *+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRST
    43   44     +,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUV
    44   45     ,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
    45   46     -./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ
    46   47     ./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\
    47   48     /0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^
    48   49     0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`
    49   50     123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`ab
    50   51     23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
    51   52     3456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdef
    52   53     456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefgh
    53   54     56789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghij
    54   55     6789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijkl
    55   56     789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmn
    56   57     89:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnop
    57   58     9:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqr
    58   59     :;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrst
    59   60     ;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuv
    60   61     <=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwx
    61   62     =>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
    62   63     >?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|
    63   64     ?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    64   65     @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    65   66     ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    66   67     BCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    67   68     CDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    68   69     DEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    69   70     EFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    70   71     FGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    71   72     GHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    72   73     HIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    73   74     IJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    74   75     JKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    75   76     KLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    76   77     LMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    77   78     MNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    78   79     NOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    79   80     OPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    80   81     PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    81   82     QRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    82   83     RSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    83   84     STUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    84   85     TUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    85   86     UVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    86   87     VWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    87   88     WXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    88   89     XYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    89   90     YZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    90   91     Z[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    91   92     [\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    92   93     \]^_`abcdefghijklmnopqrstuvwxyz{|}~
    93   94     ]^_`abcdefghijklmnopqrstuvwxyz{|}~
    94   95     ^_`abcdefghijklmnopqrstuvwxyz{|}~
    95   96     _`abcdefghijklmnopqrstuvwxyz{|}~
    96   97     `abcdefghijklmnopqrstuvwxyz{|}~
    97   98     abcdefghijklmnopqrstuvwxyz{|}~
    98   99     bcdefghijklmnopqrstuvwxyz{|}~
    99   100     cdefghijklmnopqrstuvwxyz{|}~
    100   101     defghijklmnopqrstuvwxyz{|}~
    101   102     efghijklmnopqrstuvwxyz{|}~
    102   103     fghijklmnopqrstuvwxyz{|}~
    103   104     ghijklmnopqrstuvwxyz{|}~
    104   105     hijklmnopqrstuvwxyz{|}~
    105   106     ijklmnopqrstuvwxyz{|}~
    106   107     jklmnopqrstuvwxyz{|}~
    107   108     klmnopqrstuvwxyz{|}~
    108   109     lmnopqrstuvwxyz{|}~
    109   110     mnopqrstuvwxyz{|}~
    110   111     nopqrstuvwxyz{|}~
    111   112     opqrstuvwxyz{|}~
    112   113     pqrstuvwxyz{|}~
    113   114     qrstuvwxyz{|}~
    114   115     rstuvwxyz{|}~
    115   116     stuvwxyz{|}~
    116   117     tuvwxyz{|}~
    117   118     uvwxyz{|}~
    118   119     vwxyz{|}~
    119   120     wxyz{|}~
    120   121     xyz{|}~
    121   122     yz{|}~
    122   123     z{|}~
    123   124     {|}~
    124   125     |}~
    125   126     }~
    126   127     ~

    et je ne sais pas pourquoi.

    moi ce que j'aimerais c'est copier dans letter Alphabet[i];

    mais si je fait ca, il me met l'erreur suivante.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
        for (int i = 0; i < ASCII; i++){
     
            std::string letter =  Alphabet[i];
            std::cout<<i<<"   "<< i+1<<"     " <<letter << std::endl;
     
            }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     
    elekis@carla:~/Eletris-0.2/Tools$ g++ -o CreateFont CreateFont.cpp `sdl-config --cflags` -lSDL_ttf
    CreateFont.cpp: In function «int main(int, char**)»:
    CreateFont.cpp:52: erreur: invalid conversion from «char» to «const char*»
    CreateFont.cpp:52: erreur:   initializing argument 1 of «std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]»
    elekis@carla:~/Eletris-0.2/Tools$
    qqn aurait il une idée??

    merci

    a++

  2. #2
    Membre éclairé
    Avatar de doudoustephane
    Homme Profil pro
    Gérant
    Inscrit en
    Août 2005
    Messages
    1 129
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Gérant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 129
    Points : 862
    Points
    862
    Par défaut
    montre la declaration de ton Alphabet...
    développeur en folie cherche à ... développer encore plus

  3. #3
    Membre expert
    Avatar de Pragmateek
    Homme Profil pro
    Formateur expert .Net/C#
    Inscrit en
    Mars 2006
    Messages
    2 635
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Formateur expert .Net/C#
    Secteur : Conseil

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 635
    Points : 3 958
    Points
    3 958
    Par défaut
    Comme ça:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    std::string letter =  Alphabet.substr(i,1);
    Formateur expert .Net/C#/WPF/EF Certifié MCP disponible sur Paris, province et pays limitrophes (enseignement en français uniquement).
    Mon blog : pragmateek.com

  4. #4
    Membre éclairé
    Avatar de doudoustephane
    Homme Profil pro
    Gérant
    Inscrit en
    Août 2005
    Messages
    1 129
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Gérant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 129
    Points : 862
    Points
    862
    Par défaut
    il te renvoie quel type dans ta doc?
    un char ou un *char?
    développeur en folie cherche à ... développer encore plus

  5. #5
    Membre expert
    Avatar de Pragmateek
    Homme Profil pro
    Formateur expert .Net/C#
    Inscrit en
    Mars 2006
    Messages
    2 635
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Formateur expert .Net/C#
    Secteur : Conseil

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 635
    Points : 3 958
    Points
    3 958
    Par défaut
    Citation Envoyé par doudouallemand
    il te renvoie quel type dans ta doc?
    un char ou un *char?
    La méthode "substr" renvoie un objet de type "string".
    Formateur expert .Net/C#/WPF/EF Certifié MCP disponible sur Paris, province et pays limitrophes (enseignement en français uniquement).
    Mon blog : pragmateek.com

  6. #6
    Membre éclairé
    Avatar de doudoustephane
    Homme Profil pro
    Gérant
    Inscrit en
    Août 2005
    Messages
    1 129
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 39
    Localisation : France, Indre et Loire (Centre)

    Informations professionnelles :
    Activité : Gérant

    Informations forums :
    Inscription : Août 2005
    Messages : 1 129
    Points : 862
    Points
    862
    Par défaut
    c'est vrai, honte sur moi la...

    tu le trouve dans quoi Alphabet que je teste...?
    développeur en folie cherche à ... développer encore plus

  7. #7
    Rédacteur
    Avatar de bigboomshakala
    Homme Profil pro
    Consultant Web .NET
    Inscrit en
    Avril 2004
    Messages
    2 077
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 44
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Consultant Web .NET
    Secteur : Finance

    Informations forums :
    Inscription : Avril 2004
    Messages : 2 077
    Points : 2 757
    Points
    2 757
    Par défaut
    déjà la fonction string::substr[FONT=Courier New](size_type pos, size_type n)[/FONT]
    pos = position de début
    n = taille de la chaîne

    std::string letter = Alphabet.substr(i,i+1);
    là tu n'extrais pas un seul caractère, mais i+1. ce qui peut d'ailleurs conduire à des débordements si (2*i+1) dépasse la taille du string

  8. #8
    Membre expert
    Avatar de Pragmateek
    Homme Profil pro
    Formateur expert .Net/C#
    Inscrit en
    Mars 2006
    Messages
    2 635
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Val de Marne (Île de France)

    Informations professionnelles :
    Activité : Formateur expert .Net/C#
    Secteur : Conseil

    Informations forums :
    Inscription : Mars 2006
    Messages : 2 635
    Points : 3 958
    Points
    3 958
    Par défaut
    ce qui peut d'ailleurs conduire à des débordements si (2*i+1) dépasse la taille du string
    Il me semble que la méthode "substr" est "sécurisée", comme la classe "string", et donc il n'y a pas de débordement possible contrairement aux "char*".
    Formateur expert .Net/C#/WPF/EF Certifié MCP disponible sur Paris, province et pays limitrophes (enseignement en français uniquement).
    Mon blog : pragmateek.com

  9. #9
    Membre éclairé Avatar de HanLee
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    738
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Mai 2004
    Messages : 738
    Points : 871
    Points
    871
    Par défaut
    Citation Envoyé par seriousme
    Il me semble que la méthode "substr" est "sécurisée", comme la classe "string", et donc il n'y a pas de débordement possible contrairement aux "char*".
    Oui ya pas de débordement. Si on dépasse le nombre de caractères restants, il ne prend que le reste de la chaîne.

    http://www.bgsu.edu/departments/comp...cs/string.html

  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
    Pour recentrer le débat (puisqu'il n'y a pas besoin de substr) :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    for (int i = 0; i < ASCII; i++)
    {
        char letter = Alphabet[i];
        std::cout << i << "   " << i + 1 << "     " << letter << std::endl;
    }

  11. #11
    Membre habitué

    Profil pro
    Inscrit en
    Avril 2004
    Messages
    421
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Avril 2004
    Messages : 421
    Points : 189
    Points
    189
    Par défaut
    merci a tous, en fait, c'est 1 et pas i+1.

    laurent, en fait, je dois avoir un string car apres ca doit aller dans une fonction c qui prend un const char *

    merci

    a++

  12. #12
    Expert confirmé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Décembre 2003
    Messages
    3 549
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Essonne (Île de France)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels

    Informations forums :
    Inscription : Décembre 2003
    Messages : 3 549
    Points : 4 625
    Points
    4 625
    Par défaut
    laurent, en fait, je dois avoir un string car apres ca doit aller dans une fonction c qui prend un const char *
    Dans ce cas là passe &letter comme argument à ta fonction.
    Boost ftw

  13. #13
    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
    Sauf s'il attend une vraie chaîne de caractères avec un \0 à la fin.

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

Discussions similaires

  1. Réponses: 3
    Dernier message: 09/08/2012, 08h37
  2. Réponses: 13
    Dernier message: 11/03/2012, 22h40
  3. String avec une seule '\' ?
    Par Gregory.M dans le forum Windows Forms
    Réponses: 2
    Dernier message: 11/06/2009, 14h02
  4. [1.1>2.0] Comment obtenir à la compilation une seule DLL ?
    Par SoaB dans le forum Framework .NET
    Réponses: 6
    Dernier message: 22/07/2008, 18h03
  5. Réponses: 3
    Dernier message: 03/06/2008, 15h44

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