string c++ 详解 6 compare

比较两个字符串是否相同,按照字典顺序分别输出<为负,==为零,>为正

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;
Compare strings

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// comparing apples with apples
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string str1 ("green apple");
  string str2 ("red apple");

  if (str1.compare(str2) != 0)
    cout << str1 << " is not " << str2 << "\n";

  if (str1.compare(6,5,"apple") == 0)
    cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    cout << "therefore, both are apples\n";

  return 0;
}



Output:

green apple is not red apple
still, green apple is an apple
and red apple is also an apple
therefore, both are apples

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值