string compare用法

string (1)
int compare (const string& str) const;
substrings (2)
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str,
             size_t subpos, size_t sublen) const;
c-string (3)
int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;
buffer (4)
int compare (size_t pos, size_t len, const char* s, size_t n) const;
Compare strings
Compares the value of the  string object (or a substring) to the sequence of characters specified by its arguments.

The  compared string is the value of the  string object or -if the signature used has a  pos and a  len parameters- the substring that begins at its character in position  pos and spans  len characters.

This string is compared to a  comparing string, which is determined by the other arguments passed to the function.

Parameters

str
Another  string object, used entirely (or partially) as the  comparing string.
pos
Position of the first character in the  compared string.
If this is greater than the  string length, it throws  out_of_range.
Note: The first character is denoted by a value of  0 (not  1).
len
Length of  compared string (if the string is shorter, as many characters as possible).
A value of  string::npos indicates all characters until the end of the string.
subpos, sublen
Same as  pos and  len above, but for the  comparing string.
s
Pointer to an array of characters.
If argument  n is specified  (4), the first  n characters in the array are used as the  comparing string.
Otherwise  (3), a null-terminated sequence is expected: the length of the sequence with the characters to use as comparing string is determined by the first occurrence of a null character.
n
Number of characters to compare.
size_t is an unsigned integral type (the same as member type  string::size_type).

Return Value

Returns a signed integral indicating the relation between the strings:
valuerelation between compared string and comparing string
0They compare equal
<0Either the value of the first character that does not match is lower in the compared string, or all compared characters match but the compared string is shorter.
>0Either the value of the first character that does not match is greater in the compared string, or all compared characters match but the compared string is longer.

Example

// comparing apples with apples 
#include <iostream> 
#include <string> 
int main () 
{ 
  std::string str1 ("green apple"); 
  std::string str2 ("red apple"); 
  if (str1.compare(str2) != 0) 
     std::cout << str1 << " is not " << str2 << '\n'; 
  if (str1.compare(6,5,"apple") == 0) 
     std::cout << "still, " << str1 << " is an apple\n"; 
  if(str2.compare(str2.size()-5,5,"apple") == 0) 
     std::cout << "and " << str2 << " is also an apple\n"; 
  if (str1.compare(6,5,str2,4,5) == 0) 
     std::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
例子:
#include <iostream>
#include <string>
using namespace std;
int main(void){
	string str1="hi,test,hello";
	string str2="hi,test";
	//字符串比较
	if(str1.compare(str2)>0)
		printf("str1>str2\n");
	else if(str1.compare(str2)<0)
		printf("str1<str2\n");
	else
		printf("str1==str2\n");
	
	//str1的子串(从索引3开始,包含4个字符)与str2进行比较
	if(str1.compare(3,4,str2)==0)
		printf("str1的指定子串等于str2\n");
	else
		printf("str1的指定子串不等于str2\n");
	
	//str1指定子串与str2的指定子串进行比较
	if(str1.compare(3,4,str2,3,4)==0)
		printf("str1的指定子串等于str2的指定子串\n");
	else
		printf("str1的指定子串不等于str2的指定子串\n");
	
	//str1指定子串与字符串的前n个字符进行比较
	if(str1.compare(0,2,"hi,hello",2)==0)
		printf("str1的指定子串等于指定字符串的前2个字符组成的子串\n");
	else
		printf("str1的指定子串不等于指定字符串的前2个字符组成的子串\n");
	return 0;
	
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值