关于c++的strcmp函数实例讲解

编程入门 行业动态 更新时间:2024-10-28 04:16:29

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

函数名: strcmp功 能: 串比较用 法: int strcmp(char *str1, char *str2);看asic码,str1>str2,返回值 > 0;两串相等,返回0上面的头文件为 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
  • 0
  • 0
  • 0
  • 0

更多推荐

关于c++的strcmp函数实例讲解

本文发布于:2023-06-10 16:25:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/614994.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   实例   strcmp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!