std::string::compare() en C++
comparer() est une fonction membre publique de la classe string. Il compare la valeur de l'objet chaîne (ou d'une sous-chaîne) à la séquence de caractères spécifiée par ses arguments.
Le compare() peut traiter plus d'un argument pour chaque chaîne afin que l'on puisse spécifier une sous-chaîne par son index et par sa longueur.
Type de retour : compare() renvoie une valeur entière plutôt qu'une valeur booléenne.
Différentes syntaxes pour string::compare() :
- Syntaxe 1 : compare la chaîne *this avec la chaîne str.
int string::compare (const string& str) const Returns: 0 : if both strings are equal. A value <0 : if *this is shorter than str or, first character that doesn't match is smaller than str. A value>0 : si *ceci est plus long que str ou, le premier caractère qui ne correspond pas est plus grand
RPC
// CPP code for demonstrating> // string::compare (const string& str) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> > // returns a value <0 (s1 is smaller than s2)> > if> ((s1.compare(s2)) <0)> > cout < < s1 < < ' is smaller than ' < < s2 < < endl;> > // returns 0(s1, is being compared to itself)> > if> ((s1.compare(s1)) == 0)> > cout < < s1 < < ' is equal to ' < < s1 < < endl;> > else> > cout < < 'Strings didn't match ';> > }> // Driver Code> int> main()> {> > string s1('Geeks');> > string s2('forGeeks');> > compareOperation(s1, s2);> > > return> 0;> }> |
Sortir:
Geeks is smaller than forGeeks Geeks is equal to Geeks
- Syntaxe 2 : compare au plus les caractères len de la chaîne *this, commençant par l'index idx avec la chaîne str.
int string::compare (size_type idx, size_type len, const string& str) const Throws out_of_range if index>taille().
RPC
// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const string& str) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> > // Compares 5 characters from index number 3 of s2 with s1> > if> ((s2.compare(3, 5, s1)) == 0)> > cout < < 'Here, ' < < s1 < < ' are ' < < s2;> > else> > cout < < 'Strings didn't match ';> }> // Driver Code> int> main()> {> > string s1('Geeks');> > string s2('forGeeks');> > compareOperation(s1, s2);> > > return> 0;> }> |
Sortir:
Here, Geeks are forGeeks
- Syntaxe 3 : Compare au plus les caractères len de la chaîne *ceci commençant par l'index idx avec au plus les caractères str_len de la chaîne str commençant par l'index str_idx.
int string::compare (size_type idx, size_type len, const string& str, size_type str_idx, size_type str_len) const Throws out_of_range if idx>taille(). Lance out_of_range si str_idx> str.size().
RPC
// CPP code to demonstrate> // int string::compare (size_type idx, size_type len, const string&> // str, size_type str_idx, size_type str_len) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> > // Compares 5 characters from index number 0 of s1 with> > // 5 characters from index 3 of s2> > if> ((s1.compare(0, 5, s2, 3, 5)) == 0)> > cout < < 'Welcome to ' < < s1 < < s2 < < ' World';> > else> > cout < < 'Strings didn't match ';> }> // Driver Code> int> main()> {> > string s1('Geeks');> > string s2('forGeeks');> > compareOperation(s1, s2);> > > return> 0;> }> |
Sortir:
Welcome, to techcodeview.com World
- Syntaxe 4 : Compare les caractères de la chaîne *this avec les caractères de la chaîne C cstr.
int string::compare (const char* cstr) const
RPC
// CPP code to demonstrate> // int string::compare (const char* cstr) const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> > // returns <0 (s1 < 'techcodeview.com')> > if> ((s1.compare('techcodeview.com')) <0)> > cout < < s1 < < ' is smaller than string ' < < 'techcodeview.com';> > //returns 0 (s2 is 'forgeeks')> > if> ((s2.compare('forGeeks')) == 0)> > cout < < endl < < s2 < < ' is equal to string ' < < s2;> > else> > cout < < 'Strings didn't match ';> > }> // Driver Code> int> main()> {> > string s1('Geeks');> > string s2('forGeeks');> > compareOperation(s1, s2);> > > return> 0;> }> |
Sortir:
Geeks is smaller than string techcodeview.com forGeeks is equal to string forGeeks
- Syntaxe 5 : compare au plus les caractères len de la chaîne *this, commençant par l'index idx avec tous les caractères de la chaîne C cstr.
int string::compare (size_type idx, size_type len, const char* cstr) const
Notez que cstr ne peut pas être un pointeur nul (NULL).
RPC
// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const char* cstr) const> #include> using> namespace> std;> void> compareOperation(string s1)> {> > // Compares 5 characters from 0 index of s1 with 'Geeks'> > if> ((s1.compare(0, 5, 'Geeks')) == 0)> > cout < < s1 < < ' are ' < < 'awesome people';> > > else> > cout < < 'Strings didn't match ';> > }> // Driver Code> int> main()> {> > string s1('Geeks');> > compareOperation(s1);> > > return> 0;> }> |
Sortir:
Geeks are awesome people
- Syntaxe 6 : compare au plus les caractères len de la chaîne *this, en commençant par l'index idx avec les caractères chars_len du tableau de caractères chars.
int string::compare (size_type idx, size_type len, const char* chars, size_type chars_len)const
Notez que les caractères doivent avoir au moins des caractères chars_len. Les caractères peuvent avoir des valeurs arbitraires. Ainsi, « » n’a aucune signification particulière.
RPC
// CPP code to demonstrate> // int string::compare (size_type idx, size_type len,> // const char* chars, size_type chars_len)const> #include> using> namespace> std;> void> compareOperation(string s1, string s2)> {> > // Compares 5 characters from 0 index of s1 with> > // 5 characters of string 'Geeks'> > if> ((s1.compare(0, 5, 'Geeks', 5)) == 0)> > cout < < 'This is ' < < s1 < < s2 ;> > > else> > cout < < 'Strings didn't match ';> }> // Driver Code> int> main()> {> > string s1('Geeks');> > string s2('forGeeks');> > compareOperation(s1, s2);> > > return> 0;> }> |