c++ str比较

 传统的c字符串比较必须用strcmp函数:(不能用==,否则比较的只是两个地址)

函数名: strcmp
功  能: 串比较
用  法: int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0

上面的头文件为<string.h> strncasecmp()是忽略大小写的。


c++ string 类型的比较可以用string的函数compare()
int compare ( const string& str ) const;
int compare ( const char* s ) const;

或直接用==比较

复制代码
 //  比较两个字符串   比较规则同 c字符串比较规则
    string str7, str8;
    cout << "输入字符串 str7, str8 , 中间用空格格开" << endl;
    cin >> str7 >> str8;

    if( str7< str8 ) cout << str7 << "  小于 " << str8 << endl;
    else if( str7> str8 ) cout << str7 << "  大于 " << str8 << endl;
    else cout << str7 << "  等于 " << str8 << endl;
复制代码

 

可以直接把string类型和c字符串类型比较:

 string s("abc");
  s=="abc" //true,此时C风格字符串可以转换为string类型的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值