© D. Mathieu
mathieu@romarin.univ-aix.fr
I.U.T.d'Aix en Provence - Département Informatique
Créé le 13/01/2000 -
Dernière mise à jour : 12/01/2001
ConstructeursFonctions et opérateurs non membres, utilisables avec des strings
Accesseurs de la classe string
Modifieurs de la classe string
Méthodesappend()Algorithmes de recherche
assign()
erase()
insert()
replace()
swap()
c_str()
OpérateursAffectation : =
Concaténation : +=
Opérateur de concaténation : +Voir aussi
Opérateurs de comparaison : == != < > <= >=
Entrées/Sorties
Divers
Remarques préliminaire :
string ident;
string ident ("..." );
string ident (S);
string ident (Nbre, C); |
"..." : de type NTCTSRemarques :
Pos, Nbre : des entiers non signés (en principe de type string::size_type)
S : de type string
C : caractère (type "générique" charT)
// Affichage :
string S01; cout << '#" << S01 << '#' << endl; // ## string S02 ("ABCDE");
string S03 ("ABCDE", /* Nbre = */ 3);
string S04 ("ABCDE", /* Nbre = */ 6);
string S05 (S02);
string S06 (S02, /* Pos = */ 2);
string S07 (S02, /* Pos = */ 5);
string S08 (S02, /* Pos = */ 2, /* Nbre = */ 2);
string S09 (S02, /* Pos = */ 2, /* Nbre = */ 0);
string S10 (S02, /* Pos = */ 2, /* Nbre = */ 10);
|
Remarques :.
ident.size ()
ident.length () ident.max_size () ident.capacity () ident.empty () |
Toutes les fonctions renvoient une valeur de type string::size_type (en pratique un entier non signé) sauf empty() qui renvoie un booléen.
Exemples :
// Affichage
string S01 ("ABCDE"); string::size_type Size = S01.size (); cout << Size << endl; // 5 cout << S01.size () << endl;
// 5
if (S01.empty ()) ... |
La capacité d'une chaîne vide (par ex. constructeur par défaut) est indéfinie
ident.resize (Nbre, C);
ident.resize (Nbre); ident.clear (void); // non implémenté ici ident.reserve (Nbre = 0); // implémenté ici mais vide |
Nbre doit être de type string::size_type
Exemples :
// Affichage voir plus loin l'opérateur
<<
string S01 ("ABCDE"); S01.resize (/* Nbre = */ 3); cout << '#' << S01 << '#' << endl; // #ABC# cout << S01.size () << endl; // 3 cout << S01.capacity () << endl; // 8 dépend de l'implémentation S01.resize (/* Nbre = */ 2, /* C = */ '.');
S01.resize (/* Nbre = */ 9, /* C = */ '.');
S01.clear; // non implémenté
ici
string S02 ("Coucou");
|
ident.append ("..." )
ident.append ("...", Nbre) ident.append (Nbre, C) ident.append (S)
|
Exemples :
// Affichage
string S0; string S1 ("perché sur un arbre mort"); string S2 ("perché"); cout << S0.append ("Maitre " ) .append ("corbeau et renard", /* Nbre = */ 8) .append (/* Nbre = */ 3, /* C = */ '.') .append (S1, /* Pos = */ 7, /* Nbre = */ 13) .append (S2) << endl; // Maitre corbeau ... sur un arbre perché |
Remarques :
// Affichage
string S3, S4; cout << S3.append ("abcdef", 12) << endl; // abcdef string::size_type Pos = S3.size (); cout << '#' << S4.append (S3, Pos, /* Nbre = */ 10) << '#' << endl; // ## // cout << S4.append (S3, Pos + 1, /* Nbre = */ 10) << endl; // Pos + 1 invalide |
ident.assign ("...")
ident.assign ("...", Nbre) ident.assign (Nbre, C) ident.assign (S)
|
ident.erase (Pos = 0, Nbre = npos) |
Exemples :
// Affichage
string S0 ("1234567890"); cout << '#' << S0.erase (/* Pos = */ 4, /* Nbre = */ 2) << '#' << endl; // #12347890# cout << '#' << S0.erase (/* Pos = */ 4) << '#' << endl; // #1234# cout << '#' << S0.erase () << '#' << endl; // ## |
ident.insert (Pos, "...")
ident.insert (Pos, "...", Nbre) ident.insert (Pos, Nbre, C) ident.insert (Pos, S)
|
Exemples :
// Affichage
string S0 ("1234567890"); string S1 ("ABCDEF"); cout << S0.insert (/* Pos = */ 3, "abc") .insert (/* Pos = */ 3, "def", /* Nbre = */ 2) .insert (/* Pos = */ 3, "ghi", /* Nbre = */ 12) .insert (/* Pos = */ 9, /* Nbre = */ 2, '.') .insert (/* Pos = */ 3, S1) .insert (/* Pos1 = */ 15, S1, /* Pos2 = */ 4, /* Nbre = */ 2) << endl; // 123ABCDEFghideaEF..bc4567890 |
ident.replace (Pos, Nbre, "...")
ident.replace (Pos, Nbre, "...", Nbre2) ident.replace (Pos, Nbre, Nbre2, C) ident.replace (Pos, Nbre, S)
|
Remarques :
ident.swap (S); |
permute les chaînes ident et S.
ident.c_str () |
Le constructeur permet de faire l'opération inverse (obtenir un string à partir d'une NTCTS).
Toutes ces fonctions renvoient un entier (en fait, de type size_type) qui est la position recherchée (première ou dernière) ou la constante string::npos si non trouvé.find(), rfind() : recherchent la première (ou dernière) apparition d'une sous-chaîne dans une chaîne et renvoient la position du premier caractère de la sous chaîne dans la chaîne, quel que soit le sens du parcours. find_first_of(), find_last_of() : recherchent la première (ou dernière) apparition de n'importe lequel des caractères d'une sous-chaîne dans une chaîne et renvoient la position du premier caractère de la sous-chaîne dans la chaîne, quel que soit le sens du parcours. find_first_not_of(), find_last_not_of() : recherchent dans la chaîne initiale le premier (ou dernier) caractère n'appartenant pas à la seconde chaîne.
ident.find
(C, Pos = 0)
ident.find (S, Pos = 0) ident.find ("...", Pos = 0) ident.find ("...", Pos, Nbre) ident.rfind
(C, Pos = npos)
ident.find_first_of (C,
Pos = 0)
ident.find_last_of (C,
Pos = npos)
ident.find_first_not_of (C, Pos =
0)
ident.find_last_not_of (C,
Pos = npos)
|
Remarques :
// Affichage
string S0 ("Une phrase composée de\nmots"); cout << S0.erase (S0.find_last_of (" \t\n")) << endl; // "Une phrase composée de" cout << S0.erase (S0.find_first_of (" \t\n")) << endl; // "Une" |
ident = expression_de_type_string |
Exemples :
// Affichage
string S0; string S1 ("abcdef"); cout << (S0 = S1) << endl; // abcdef cout << (S0 = S1 + "ghi") << endl; // abcdefghi {1} cout << (S0 = string ("ABC")) << endl; // ABC cout << (S0 = "ABC") << endl; // ABC cout << (S0 = '\t' + S1) << endl; // abcdef |
Remarques :
ident += expression_de_type_string |
S1 + S2
"..." + S S + "..." C + S S + C |
Bien entendu, la propriété d'associativité de l'opérateur + est respectée : S + C + "..." est équivalent à (S + C) + "...".
expression1_de_type_string == expression2_de_type_string |
idem pour autres opérateurs
os << S ...
is >> S ... getline (is, S, delim); getline (is, S); // delim = '\n' |
delim : caractère (type "générique" charT)Au cours de l'exécution de l'instruction : getline (is, S, delim); le délimiteur est extrait du flux mais n'est pas rangé dans la chaîne.
is : flux d'entrée (input stream)
os : flux de sortie (output stream)
S1, S2 : de type string
swap (S1, S2); |
S1, S2 : de type stringSommaire
SGI - Bibliothèque STL :
classe basic_string
Thinking in C++ - B. Eckel :
strings
© D. Mathieu
mathieu@romarin.univ-aix.fr
I.U.T.d'Aix en Provence - Département Informatique