The member function returns 0 if all the characters in the compared contents compare equal, a negative value if the first character that does not match compares to less in the object than in the comparing string, and a positive value in the opposite case.
Notice that for string objects, the result of a character comparison depends only on its character code (i.e., its ASCII code), so the result has some limited alphabetical or numerical ordering meaning.
For other basic_string class instantitations, the comparison depends on the specific traits::compare function, where traits is one of the class template parameters.
Parameters
- str
- string object with the content to be used as comparing string.
- s
- Array with a sequence of characters to be used as comparing string.
Except for the last member version, this is a null-terminated character sequence whose length is determined by the first occurrence of a null-character.
In the last member version, the length is not determined by any occurrence of null-characters but by parameter n2. - pos1
- Position of the beginning of the compared substring, i.e. the first character in the object (in *this) to be compared against the comparing string.
- n1
- Length of the compared substring.
- pos2
- Position of a character in object str which is the beginning of the comparing string.
- n2
- Length in characters of the comparing string.
Return Value
0 if the compared characters sequences are equal, otherwise a number different from 0 is returned, with its sign indicating whether the object is considered greater than the comparing string passed as parameter (positive sign), or smaller (negative sign).If either pos1 or pos2 is specified with a position greater than the size of the corresponding string object, an exception of type out_of_range is thrown.
Example
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Output:
green apple is not red apple still, green apple is an apple and red apple is also an apple therefore, both are apples |
Basic template member declarations
( basic_string<charT,traits,Allocator> )| 1 2 3 4 5 6 7 8 9 10 11 12 |
|
See also
| string::find | Find content in string (public member function) |
| string::rfind | Find last occurrence of content in string (public member function) |
| string::substr | Generate substring (public member function) |
