C/C++基础 string::compare的使用

//description: 演示String::compare函数的用法,比较两个字符串是否相等?
//compile: g++ -g compare_string.cc -o compare_string
 
#include <string>
#include <iostream>
using namespace std;
 
int main(int argc, char* argv[])
{
    string str1("green apple");
    string str2("red apple");
    string str3("apple");
 
    if(str3.compare("apple") == 0)
        cout << str3 << " is an apple!" << endl;
 
    if(str1.compare(str2) !=0)
        cout << str1 << " is not " << str2 << endl;
 
    if(str1.compare(6, 5, "apple") == 0)
        cout << "still, " << str1 << " is an apple!" << endl;
                                                                                                                                                                         
    if(str2.compare(str2.size() - 5, 5, "apple") == 0)
        cout << "and " << str2 << " is also an apple!" << endl;
 
    if(str1.compare(6, 5, str2, 4, 5) == 0)
        cout << "therefore, both are apples!" << endl;
 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值