c ++中stl_比较C ++中的两个字符串对象 C ++ STL

C++中的String类提供了一种高效管理字符串的方式。std::string::compare()函数用于比较两个字符串对象,若相等返回0,字符串较短返回负整数,字符串较长返回正整数。该函数有多种用法,包括指定起始位置和长度进行比较。文章通过实例展示了其工作原理。
摘要由CSDN通过智能技术生成

c ++中stl

String class in C++ is a container used to manage strings and it has many advantages than ordinary c-string. In many programming problems, we require to compare two strings, in this article we discuss library function used to compare two string class objects in C++.

C ++中的字符串类是用于管理字符串的容器,它比普通的c字符串具有许多优点。 在许多编程问题中,我们需要比较两个字符串 ,在本文中,我们讨论用于比较C ++中两个字符串类对象的库函数。

We use std::string::compare() function to comparing two string objects. It returns '0' if both the strings are equal, integer < 0 if the compared string is shorter and an integer > 0 if the compared string is longer. We can use std::string::compare() function in various forms according to need, some methods are discussed below.

我们使用std :: string :: compare()函数比较两个字符串对象。 如果两个字符串相等,则返回“ 0”;如果比较的字符串较短,则返回整数<0;如果比较的字符串较长,则返回整数> 0。 我们可以根据需要以各种形式使用std :: string :: compare()函数 ,下面讨论一些方法。

References: std::string::compare()

引用: std :: string :: compare()

Example 1:

范例1:

Syntax:

句法:

    string::compare (const string& str) const

#include<iostream> 
using namespace std; 

int main() 
{ 
	string s1("includehelp"); 
	string s2("include");
	string s3("includehelp"); 

	if((s1.compare(s2)) == 0) 
		cout << s1 << " is equal to " << s2 << endl; 
	else
		cout << s1 << " didn't match to " << s2 << endl; 

	if((s1.compare(s3)) == 0) 
		cout << s1 << " is equal to " << s3 << endl; 
	else
		cout << s1 << " didn't match to " << s3 << endl; 

	return 0;  
} 

Output

输出量

includehelp didn't match to include
includehelp is equal to includehelp


Example 2:

范例2:

    compare (size_t pos, size_t len, const string& str) const;

Here, pos is the index of string from which you want to compare and len denotes the length of the compared string after pos. following code shows the working of this syntax :

在这里, pos是要从中进行比较的字符串的索引, len表示在pos之后要比较的字符串的长度。 以下代码显示了此语法的工作方式:

#include<iostream> 
using namespace std;

int main() 
{ 
	string s1("help"); 
	string s2("includehelp");

	//Compares 4 characters from index 7 of s2 with s1 
	if((s2.compare(7, 4, s1)) == 0) 
		cout << s1 << " is equal to " << s2.substr(7) << endl; 
	else
		cout << s1 << " didn't match to " << s2.substr(7) << endl; 

	return 0;  
} 

Output

输出量

    help is equal to help


Example 3:

范例3:

It compares len characters from index number pos of first string with sublen characters from index subpos of string compared string i.e str. following code shows the working of this syntax :

它比较第一个字符串的索引编号pos中的len个字符与比较字符串的索引subpos中的sublen个字符,即str 。 以下代码显示了此语法的工作方式:

#include<iostream> 
using namespace std;

int main() 
{
	string s1("includenothelp"); 
	string s2("helpinclude");

	cout<<"s1 = "<<s1<<endl;
	cout<<"s2 = "<<s2<<endl;
	cout<<"Compares 7 characters from index 0 of s1 with 7 characters from index 4 of s2 "<<endl;

	// Compares 7 characters from index 0 of s1 with 7 characters from index 4 of s2 
	if((s1.compare(0, 7, s2, 4, 7)) == 0) 
		cout << s1.substr(0,7) <<" of "<< s1 << " is equal to " << s2.substr(4) <<" of "<<s2 << endl; 
	else
		cout <<" didn't match "<< endl; 

	return 0;  
} 

Output

输出量

Compares 7 characters from index 0 of s1 with 7 characters from index 4 of s2
include of includenothelp is equal to include of helpinclude


翻译自: https://www.includehelp.com/stl/compare-two-string-objects.aspx

c ++中stl

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值