std::string::compare() in C++

Syntax 1: Compares the string *this with the string str.
int string::compare (const string& str) const
Returns:
0 : if both strings are equal.
A value < 0 : if *this is shorter than str or,
first character that doesn't match is smaller than str.
A value > 0 : if *this is longer than str or,
first character that doesn't match is greater
复制代码
// CPP code for demonstrating
// string::compare (const string& str) const

#include<iostream>
using namespace std;

void compareOperation(string s1, string s2)
{
    // returns a value < 0 (s1 is smaller then s2)
    if((s1.compare(s2)) < 0)
        cout << s1 << " is smaller than " << s2 << endl;

    // returns 0(s1, is being comapared to itself)
    if((s1.compare(s1)) == 0)
        cout << s1 << " is equal to " << s1 << endl;
    else
        cout << "Strings didn't match ";
    
}

// Driver Code
int main()
{
    string s1("Geeks");
    string s2("forGeeks");
    compareOperation(s1, s2);
    
    return 0;
}
View Code
复制代码

output:

Geeks is smaller than forGeeks
Geeks is equal to Geeks

Syntax 2: Compares at most, len characters of string *this, starting with index idx with the string str.

复制代码
// CPP code to demonstrate
// int string::compare (size_type idx, size_type len,
// const string& str) const

#include<iostream>
using namespace std;

void compareOperation(string s1, string s2)
{
    // Compares 5 characters from index number 3 of s2 with s1
    if((s2.compare(3, 5, s1)) == 0)
        cout << "Here, "<< s1 << " are " << s2;

    else
        cout << "Strings didn't match ";
}
// Driver Code
int main()
{
    string s1("Geeks");
    string s2("forGeeks");
    compareOperation(s1, s2);
    
return 0;
}
View Code
复制代码

output:

Here, Geeks are forGeeks

 

Syntax 3: Compares at most, len characters of string *this starting with index idx with at most, str_len characters of string str starting with index str_idx.

int string::compare (size_type idx, size_type len, const string& 
str, size_type str_idx, size_type str_len) const
Throws out_of_range if idx > size().
Throws out_of_range if str_idx > str.size().
复制代码
// CPP code to demonstrate
// int string::compare (size_type idx, size_type len, const string&
// str, size_type str_idx, size_type str_len) const

#include<iostream>
using namespace std;

void compareOperation(string s1, string s2)
{
    // Compares 5 characters from index number 0 of s1 with
    // 5 characters from index 3 of s2
    if((s1.compare(0, 5, s2, 3, 5)) == 0)
        cout << "Welcome to " << s1 << s2 << " World";

    else
        cout << "Strings didn't match ";
}
// Driver Code
int main()
{
    string s1("Geeks");
    string s2("forGeeks");
    compareOperation(s1, s2);
    
return 0;
}
复制代码

Output:

Welcome, to GeeksforGeeks World

 

 

 

 

posted @   PiaYie  阅读(175)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示