比较两个字符串是否相同,按照字典顺序分别输出<为负,==为零,>为正
int compare ( const string& str ) const; 比较字符串
int compare ( const char* s ) const; 比较字符数组
int compare ( size_t pos1, size_t n1, const string& str ) const; 比较str字串从位置pos1开始的n1个字符
int compare ( size_t pos1, size_t n1, const char* s) const; 比较字符数组从位置pos1开始的n1个字符
int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const; 比较字符串从pos1开始的n1个字符与str字符串从pos2开始的n2个字符
int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const; 比较字符串从位置pos1开始的n1个字符与字符数组s从首地址开始的n2个字符
---------------------------------------------------------------------------------------------------------
int compare ( const string& str ) const; int compare ( const char* s ) const; int compare ( size_t pos1, size_t n1, const string& str ) const; int compare ( size_t pos1, size_t n1, const char* s) const; int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const; int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const;
Compares the content of this object (or a substring of it, known as compared (sub)string) to the content of acomparing string, which is formed according to the arguments passed.
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 thecomparing 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., itsASCII code), so the result has some limited alphabetical or numerical ordering meaning.
For other basic_string class instantiations, the comparison depends on the specifictraits::compare function, wheretraits 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 from0 is returned, with its sign indicating whether the object is considered greater than thecomparing 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 typeout_of_range is thrown.
Example
| |
Output:
green apple is not red apple still, green apple is an apple and red apple is also an apple therefore, both are apples |